2019-01-28 06:27:04 +00:00
const path = require ( 'path' )
const sgit = require ( 'simple-git/promise' )
const fs = require ( 'fs-extra' )
const _ = require ( 'lodash' )
const repoPath = path . join ( WIKI . ROOTPATH , 'data/repo' )
/ * *
* Get file extension based on content type
* /
const getFileExtension = ( contentType ) => {
switch ( contentType ) {
case 'markdown' :
return 'md'
case 'html' :
return 'html'
default :
return 'txt'
}
}
/ * *
* Inject page metadata into contents
* /
const injectMetadata = ( page ) => {
let meta = [
[ 'title' , page . title ] ,
[ 'description' , page . description ]
]
let metaFormatted = ''
switch ( page . contentType ) {
case 'markdown' :
metaFormatted = meta . map ( mt => ` [//]: # ${ mt [ 0 ] } : ${ mt [ 1 ] } ` ) . join ( '\n' )
break
case 'html' :
metaFormatted = meta . map ( mt => ` <!-- ${ mt [ 0 ] } : ${ mt [ 1 ] } --> ` ) . join ( '\n' )
break
default :
metaFormatted = meta . map ( mt => ` #WIKI ${ mt [ 0 ] } : ${ mt [ 1 ] } ` ) . join ( '\n' )
break
}
return ` ${ metaFormatted } \n \n ${ page . content } `
}
2018-07-08 05:12:43 +00:00
module . exports = {
2018-07-22 20:25:39 +00:00
async activated ( ) {
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async deactivated ( ) {
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async init ( ) {
2019-01-28 06:27:04 +00:00
await fs . ensureDir ( repoPath )
const git = sgit ( repoPath )
// Initialize repo (if needed)
const isRepo = await git . checkIsRepo ( )
if ( ! isRepo ) {
await git . init ( )
}
// Checkout branch
await git . checkout ( this . config . branch )
2018-07-08 05:12:43 +00:00
2019-01-28 06:27:04 +00:00
// Add remote
await git . raw ( [ 'config' , '--local' , '--bool' , 'http.sslVerify' , _ . toString ( this . config . verifySSL ) ] )
switch ( this . config . authType ) {
case 'ssh' :
await git . addConfig ( 'core.sshCommand' , ` ssh -i " ${ this . config . sshPrivateKeyPath } " -o StrictHostKeyChecking=no ` )
await git . addRemote ( 'origin' , this . config . repoUrl )
break
default :
await git . addRemote ( 'origin' , ` https:// ${ this . config . basicUsername } : ${ this . config . basicPassword } @ ${ this . config . repoUrl } ` )
break
}
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async created ( ) {
2019-01-28 06:27:04 +00:00
const fileName = ` ${ this . page . path } . ${ getFileExtension ( this . page . contentType ) } `
const filePath = path . join ( repoPath , fileName )
await fs . outputFile ( filePath , injectMetadata ( this . page ) , 'utf8' )
2018-07-08 05:12:43 +00:00
2019-01-28 06:27:04 +00:00
const git = sgit ( repoPath )
await git . add ( ` ./ ${ fileName } ` ) . commit ( ` docs: create ${ this . page . path } ` , fileName , {
'--author' : ` " ${ this . page . authorName } < ${ this . page . authorEmail } >" `
} )
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async updated ( ) {
2019-01-28 06:27:04 +00:00
const fileName = ` ${ this . page . path } . ${ getFileExtension ( this . page . contentType ) } `
const filePath = path . join ( repoPath , fileName )
await fs . outputFile ( filePath , injectMetadata ( this . page ) , 'utf8' )
2018-07-08 05:12:43 +00:00
2019-01-28 06:27:04 +00:00
const git = sgit ( repoPath )
await git . add ( ` ./ ${ fileName } ` ) . commit ( ` docs: update ${ this . page . path } ` , fileName , {
'--author' : ` " ${ this . page . authorName } < ${ this . page . authorEmail } >" `
} )
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async deleted ( ) {
2019-01-28 06:27:04 +00:00
const fileName = ` ${ this . page . path } . ${ getFileExtension ( this . page . contentType ) } `
2018-07-08 05:12:43 +00:00
2019-01-28 06:27:04 +00:00
const git = sgit ( repoPath )
await git . rm ( ` ./ ${ fileName } ` ) . commit ( ` docs: delete ${ this . page . path } ` , fileName , {
'--author' : ` " ${ this . page . authorName } < ${ this . page . authorEmail } >" `
} )
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async renamed ( ) {
2019-01-28 06:27:04 +00:00
const sourceFilePath = ` ${ this . page . sourcePath } . ${ getFileExtension ( this . page . contentType ) } `
const destinationFilePath = ` ${ this . page . destinationPath } . ${ getFileExtension ( this . page . contentType ) } `
2018-07-08 05:12:43 +00:00
2019-01-28 06:27:04 +00:00
const git = sgit ( repoPath )
await git . mv ( ` ./ ${ sourceFilePath } ` , ` ./ ${ destinationFilePath } ` ) . commit ( ` docs: rename ${ this . page . sourcePath } to ${ destinationFilePath } ` , destinationFilePath , {
'--author' : ` " ${ this . page . authorName } < ${ this . page . authorEmail } >" `
} )
2018-07-08 05:12:43 +00:00
}
}