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' )
2019-04-06 23:05:47 +00:00
const stream = require ( 'stream' )
const Promise = require ( 'bluebird' )
const pipeline = Promise . promisify ( stream . pipeline )
const klaw = require ( 'klaw' )
2019-11-17 00:11:37 +00:00
const os = require ( 'os' )
2019-10-20 22:34:38 +00:00
const pageHelper = require ( '../../../helpers/page' )
const assetHelper = require ( '../../../helpers/asset' )
const commonDisk = require ( '../disk/common' )
2019-01-28 06:27:04 +00:00
2019-03-17 02:10:01 +00:00
/* global WIKI */
2018-07-08 05:12:43 +00:00
module . exports = {
2019-02-03 22:08:06 +00:00
git : null ,
2019-10-25 22:20:02 +00:00
repoPath : path . resolve ( WIKI . ROOTPATH , WIKI . config . dataPath , 'repo' ) ,
2018-07-22 20:25:39 +00:00
async activated ( ) {
2019-02-03 22:08:06 +00:00
// not used
2018-07-08 05:12:43 +00:00
} ,
2018-07-22 20:25:39 +00:00
async deactivated ( ) {
2019-02-03 22:08:06 +00:00
// not used
2018-07-08 05:12:43 +00:00
} ,
2019-02-25 04:48:28 +00:00
/ * *
* INIT
* /
2018-07-22 20:25:39 +00:00
async init ( ) {
2019-02-03 07:48:30 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Initializing...' )
2019-02-03 22:08:06 +00:00
this . repoPath = path . resolve ( WIKI . ROOTPATH , this . config . localRepoPath )
await fs . ensureDir ( this . repoPath )
this . git = sgit ( this . repoPath )
2019-01-28 06:27:04 +00:00
2019-03-17 02:10:01 +00:00
// Set custom binary path
if ( ! _ . isEmpty ( this . config . gitBinaryPath ) ) {
this . git . customBinary ( this . config . gitBinaryPath )
}
2019-01-28 06:27:04 +00:00
// Initialize repo (if needed)
2019-02-03 07:48:30 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Checking repository state...' )
2019-02-03 22:08:06 +00:00
const isRepo = await this . git . checkIsRepo ( )
2019-01-28 06:27:04 +00:00
if ( ! isRepo ) {
2019-02-03 07:48:30 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Initializing local repository...' )
2019-02-03 22:08:06 +00:00
await this . git . init ( )
2019-01-28 06:27:04 +00:00
}
2022-05-10 01:39:08 +00:00
// Disable quotePath
// Link https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath
await this . git . raw ( [ 'config' , '--local' , 'core.quotepath' , false ] )
2019-02-03 07:48:30 +00:00
// Set default author
2019-02-03 22:08:06 +00:00
await this . git . raw ( [ 'config' , '--local' , 'user.email' , this . config . defaultEmail ] )
await this . git . raw ( [ 'config' , '--local' , 'user.name' , this . config . defaultName ] )
2019-02-03 07:48:30 +00:00
// Purge existing remotes
WIKI . logger . info ( '(STORAGE/GIT) Listing existing remotes...' )
2019-02-03 22:08:06 +00:00
const remotes = await this . git . getRemotes ( )
2019-02-03 07:48:30 +00:00
if ( remotes . length > 0 ) {
WIKI . logger . info ( '(STORAGE/GIT) Purging existing remotes...' )
2019-03-17 02:10:01 +00:00
for ( let remote of remotes ) {
2019-02-03 22:08:06 +00:00
await this . git . removeRemote ( remote . name )
2019-02-03 07:48:30 +00:00
}
}
2018-07-08 05:12:43 +00:00
2019-01-28 06:27:04 +00:00
// Add remote
2019-02-03 07:48:30 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Setting SSL Verification config...' )
2019-02-03 22:08:06 +00:00
await this . git . raw ( [ 'config' , '--local' , '--bool' , 'http.sslVerify' , _ . toString ( this . config . verifySSL ) ] )
2019-01-28 06:27:04 +00:00
switch ( this . config . authType ) {
case 'ssh' :
2019-02-03 07:48:30 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Setting SSH Command config...' )
2019-07-14 22:55:12 +00:00
if ( this . config . sshPrivateKeyMode === 'contents' ) {
try {
2019-10-25 22:20:02 +00:00
this . config . sshPrivateKeyPath = path . resolve ( WIKI . ROOTPATH , WIKI . config . dataPath , 'secure/git-ssh.pem' )
2019-11-17 00:11:37 +00:00
await fs . outputFile ( this . config . sshPrivateKeyPath , this . config . sshPrivateKeyContent + os . EOL , {
2019-07-14 22:55:12 +00:00
encoding : 'utf8' ,
mode : 0o600
} )
} catch ( err ) {
2022-02-19 00:48:22 +00:00
WIKI . logger . error ( err )
2019-07-14 22:55:12 +00:00
throw err
}
}
2020-04-25 18:04:14 +00:00
await this . git . addConfig ( 'core.sshCommand' , ` ssh -i " ${ this . config . sshPrivateKeyPath } " -o StrictHostKeyChecking=no ` )
2019-02-03 07:48:30 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Adding origin remote via SSH...' )
2019-02-03 22:08:06 +00:00
await this . git . addRemote ( 'origin' , this . config . repoUrl )
2019-01-28 06:27:04 +00:00
break
default :
2019-04-27 04:08:43 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Adding origin remote via HTTP/S...' )
let originUrl = ''
if ( _ . startsWith ( this . config . repoUrl , 'http' ) ) {
2020-04-22 03:35:49 +00:00
originUrl = this . config . repoUrl . replace ( '://' , ` :// ${ encodeURI ( this . config . basicUsername ) } : ${ encodeURI ( this . config . basicPassword ) } @ ` )
2019-04-27 04:08:43 +00:00
} else {
2020-04-22 03:35:49 +00:00
originUrl = ` https:// ${ encodeURI ( this . config . basicUsername ) } : ${ encodeURI ( this . config . basicPassword ) } @ ${ this . config . repoUrl } `
2019-04-27 04:08:43 +00:00
}
2019-04-28 01:42:04 +00:00
await this . git . addRemote ( 'origin' , originUrl )
2019-01-28 06:27:04 +00:00
break
}
2019-02-03 07:48:30 +00:00
// Fetch updates for remote
WIKI . logger . info ( '(STORAGE/GIT) Fetch updates from remote...' )
2019-02-03 22:08:06 +00:00
await this . git . raw ( [ 'remote' , 'update' , 'origin' ] )
2019-02-03 07:48:30 +00:00
// Checkout branch
2019-02-03 22:08:06 +00:00
const branches = await this . git . branch ( )
2019-02-03 07:48:30 +00:00
if ( ! _ . includes ( branches . all , this . config . branch ) && ! _ . includes ( branches . all , ` remotes/origin/ ${ this . config . branch } ` ) ) {
throw new Error ( 'Invalid branch! Make sure it exists on the remote first.' )
}
WIKI . logger . info ( ` (STORAGE/GIT) Checking out branch ${ this . config . branch } ... ` )
2019-02-03 22:08:06 +00:00
await this . git . checkout ( this . config . branch )
// Perform initial sync
await this . sync ( )
2019-02-03 07:48:30 +00:00
2019-02-03 22:08:06 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Initialization completed.' )
} ,
2019-02-25 04:48:28 +00:00
/ * *
* SYNC
* /
2019-02-03 22:08:06 +00:00
async sync ( ) {
2019-02-25 04:48:28 +00:00
const currentCommitLog = _ . get ( await this . git . log ( [ '-n' , '1' , this . config . branch ] ) , 'latest' , { } )
2019-10-15 03:44:37 +00:00
const rootUser = await WIKI . models . users . getRootUser ( )
2019-02-03 07:48:30 +00:00
// Pull rebase
if ( _ . includes ( [ 'sync' , 'pull' ] , this . mode ) ) {
WIKI . logger . info ( ` (STORAGE/GIT) Performing pull rebase from origin on branch ${ this . config . branch } ... ` )
2019-02-03 22:08:06 +00:00
await this . git . pull ( 'origin' , this . config . branch , [ '--rebase' ] )
2019-02-03 07:48:30 +00:00
}
// Push
if ( _ . includes ( [ 'sync' , 'push' ] , this . mode ) ) {
WIKI . logger . info ( ` (STORAGE/GIT) Performing push to origin on branch ${ this . config . branch } ... ` )
let pushOpts = [ '--signed=if-asked' ]
if ( this . mode === 'push' ) {
pushOpts . push ( '--force' )
}
2019-02-03 22:08:06 +00:00
await this . git . push ( 'origin' , this . config . branch , pushOpts )
2019-02-03 07:48:30 +00:00
}
2019-02-25 04:48:28 +00:00
// Process Changes
if ( _ . includes ( [ 'sync' , 'pull' ] , this . mode ) ) {
const latestCommitLog = _ . get ( await this . git . log ( [ '-n' , '1' , this . config . branch ] ) , 'latest' , { } )
2019-03-16 22:47:00 +00:00
const diff = await this . git . diffSummary ( [ '-M' , currentCommitLog . hash , latestCommitLog . hash ] )
2019-02-25 04:48:28 +00:00
if ( _ . get ( diff , 'files' , [ ] ) . length > 0 ) {
2019-10-20 22:34:38 +00:00
let filesToProcess = [ ]
for ( const f of diff . files ) {
2021-09-01 16:11:33 +00:00
const fMoved = f . file . split ( ' => ' )
const fName = fMoved . length === 2 ? fMoved [ 1 ] : fMoved [ 0 ]
const fPath = path . join ( this . repoPath , fName )
2019-10-20 22:34:38 +00:00
let fStats = { size : 0 }
try {
fStats = await fs . stat ( fPath )
} catch ( err ) {
if ( err . code !== 'ENOENT' ) {
WIKI . logger . warn ( ` (STORAGE/GIT) Failed to access file ${ f . file } ! Skipping... ` )
continue
}
}
filesToProcess . push ( {
... f ,
file : {
path : fPath ,
stats : fStats
} ,
2021-09-01 16:11:33 +00:00
oldPath : fMoved [ 0 ] ,
relPath : fName
2019-10-20 22:34:38 +00:00
} )
}
await this . processFiles ( filesToProcess , rootUser )
2019-04-06 23:05:47 +00:00
}
}
} ,
/ * *
* Process Files
*
* @ param { Array < String > } files Array of files to process
* /
2019-10-15 03:44:37 +00:00
async processFiles ( files , user ) {
2019-04-06 23:05:47 +00:00
for ( const item of files ) {
2019-10-20 22:34:38 +00:00
const contentType = pageHelper . getContentType ( item . relPath )
2021-09-01 16:11:33 +00:00
const fileExists = await fs . pathExists ( item . file . path )
2019-10-20 22:34:38 +00:00
if ( ! item . binary && contentType ) {
// -> Page
2021-10-09 17:59:47 +00:00
if ( fileExists && ! item . importAll && item . relPath !== item . oldPath ) {
2021-09-01 16:11:33 +00:00
// Page was renamed by git, so rename in DB
WIKI . logger . info ( ` (STORAGE/GIT) Page marked as renamed: from ${ item . oldPath } to ${ item . relPath } ` )
const contentPath = pageHelper . getPagePath ( item . oldPath )
const contentDestinationPath = pageHelper . getPagePath ( item . relPath )
await WIKI . models . pages . movePage ( {
user : user ,
path : contentPath . path ,
destinationPath : contentDestinationPath . path ,
locale : contentPath . locale ,
destinationLocale : contentPath . locale ,
skipStorage : true
} )
2021-10-09 17:59:47 +00:00
} else if ( ! fileExists && ! item . importAll && item . deletions > 0 && item . insertions === 0 ) {
2019-10-20 22:34:38 +00:00
// Page was deleted by git, can safely mark as deleted in DB
WIKI . logger . info ( ` (STORAGE/GIT) Page marked as deleted: ${ item . relPath } ` )
const contentPath = pageHelper . getPagePath ( item . relPath )
await WIKI . models . pages . deletePage ( {
2020-07-03 21:44:42 +00:00
user : user ,
2019-04-06 23:05:47 +00:00
path : contentPath . path ,
locale : contentPath . locale ,
skipStorage : true
} )
2019-10-20 22:34:38 +00:00
continue
2019-04-06 23:05:47 +00:00
}
2019-02-25 04:48:28 +00:00
2019-10-20 22:34:38 +00:00
try {
await commonDisk . processPage ( {
user ,
relPath : item . relPath ,
fullPath : this . repoPath ,
contentType : contentType ,
moduleName : 'GIT'
2019-04-06 23:05:47 +00:00
} )
2019-10-20 22:34:38 +00:00
} catch ( err ) {
WIKI . logger . warn ( ` (STORAGE/GIT) Failed to process ${ item . relPath } ` )
WIKI . logger . warn ( err )
}
} else {
// -> Asset
2021-10-09 17:59:47 +00:00
if ( fileExists && ! item . importAll && ( ( item . before === item . after ) || ( item . deletions === 0 && item . insertions === 0 ) ) ) {
2021-09-01 16:11:33 +00:00
// Asset was renamed by git, so rename in DB
WIKI . logger . info ( ` (STORAGE/GIT) Asset marked as renamed: from ${ item . oldPath } to ${ item . relPath } ` )
const fileHash = assetHelper . generateHash ( item . relPath )
const assetToRename = await WIKI . models . assets . query ( ) . findOne ( { hash : fileHash } )
if ( assetToRename ) {
await WIKI . models . assets . query ( ) . patch ( {
filename : item . relPath ,
hash : fileHash
} ) . findById ( assetToRename . id )
await assetToRename . deleteAssetCache ( )
} else {
WIKI . logger . info ( ` (STORAGE/GIT) Asset was not found in the DB, nothing to rename: ${ item . relPath } ` )
}
continue
2021-10-09 17:59:47 +00:00
} else if ( ! fileExists && ! item . importAll && ( ( item . before > 0 && item . after === 0 ) || ( item . deletions > 0 && item . insertions === 0 ) ) ) {
2019-10-20 22:34:38 +00:00
// Asset was deleted by git, can safely mark as deleted in DB
WIKI . logger . info ( ` (STORAGE/GIT) Asset marked as deleted: ${ item . relPath } ` )
const fileHash = assetHelper . generateHash ( item . relPath )
const assetToDelete = await WIKI . models . assets . query ( ) . findOne ( { hash : fileHash } )
if ( assetToDelete ) {
await WIKI . models . knex ( 'assetData' ) . where ( 'id' , assetToDelete . id ) . del ( )
await WIKI . models . assets . query ( ) . deleteById ( assetToDelete . id )
await assetToDelete . deleteAssetCache ( )
} else {
WIKI . logger . info ( ` (STORAGE/GIT) Asset was not found in the DB, nothing to delete: ${ item . relPath } ` )
}
continue
}
try {
await commonDisk . processAsset ( {
user ,
relPath : item . relPath ,
file : item . file ,
contentType : contentType ,
moduleName : 'GIT'
} )
} catch ( err ) {
WIKI . logger . warn ( ` (STORAGE/GIT) Failed to process asset ${ item . relPath } ` )
2019-04-06 23:05:47 +00:00
WIKI . logger . warn ( err )
2019-02-25 04:48:28 +00:00
}
}
}
2018-07-08 05:12:43 +00:00
} ,
2019-02-25 04:48:28 +00:00
/ * *
* CREATE
*
* @ param { Object } page Page to create
* /
2019-02-03 22:08:06 +00:00
async created ( page ) {
2019-10-19 00:23:10 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Committing new file [ ${ page . localeCode } ] ${ page . path } ... ` )
2019-10-13 23:59:50 +00:00
let fileName = ` ${ page . path } . ${ pageHelper . getFileExtension ( page . contentType ) } `
2019-06-25 02:13:41 +00:00
if ( WIKI . config . lang . namespacing && WIKI . config . lang . code !== page . localeCode ) {
fileName = ` ${ page . localeCode } / ${ fileName } `
}
2019-02-03 22:08:06 +00:00
const filePath = path . join ( this . repoPath , fileName )
2019-02-10 23:06:03 +00:00
await fs . outputFile ( filePath , page . injectMetadata ( ) , 'utf8' )
2019-02-03 22:08:06 +00:00
2022-05-27 00:42:14 +00:00
const gitFilePath = ` ./ ${ fileName } `
if ( ( await this . git . checkIgnore ( gitFilePath ) ) . length === 0 ) {
await this . git . add ( gitFilePath )
await this . git . commit ( ` docs: create ${ page . path } ` , fileName , {
'--author' : ` " ${ page . authorName } < ${ page . authorEmail } >" `
} )
}
2018-07-08 05:12:43 +00:00
} ,
2019-02-25 04:48:28 +00:00
/ * *
* UPDATE
*
* @ param { Object } page Page to update
* /
2019-02-03 22:08:06 +00:00
async updated ( page ) {
2019-10-19 00:23:10 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Committing updated file [ ${ page . localeCode } ] ${ page . path } ... ` )
2019-10-13 23:59:50 +00:00
let fileName = ` ${ page . path } . ${ pageHelper . getFileExtension ( page . contentType ) } `
2019-06-25 02:13:41 +00:00
if ( WIKI . config . lang . namespacing && WIKI . config . lang . code !== page . localeCode ) {
fileName = ` ${ page . localeCode } / ${ fileName } `
}
2019-02-03 22:08:06 +00:00
const filePath = path . join ( this . repoPath , fileName )
2019-02-10 23:06:03 +00:00
await fs . outputFile ( filePath , page . injectMetadata ( ) , 'utf8' )
2019-02-03 22:08:06 +00:00
2022-05-27 00:42:14 +00:00
const gitFilePath = ` ./ ${ fileName } `
if ( ( await this . git . checkIgnore ( gitFilePath ) ) . length === 0 ) {
await this . git . add ( gitFilePath )
await this . git . commit ( ` docs: update ${ page . path } ` , fileName , {
'--author' : ` " ${ page . authorName } < ${ page . authorEmail } >" `
} )
}
2018-07-08 05:12:43 +00:00
} ,
2019-02-25 04:48:28 +00:00
/ * *
* DELETE
*
* @ param { Object } page Page to delete
* /
2019-02-03 22:08:06 +00:00
async deleted ( page ) {
2019-10-19 00:23:10 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Committing removed file [ ${ page . localeCode } ] ${ page . path } ... ` )
2019-10-13 23:59:50 +00:00
let fileName = ` ${ page . path } . ${ pageHelper . getFileExtension ( page . contentType ) } `
2019-06-25 02:13:41 +00:00
if ( WIKI . config . lang . namespacing && WIKI . config . lang . code !== page . localeCode ) {
fileName = ` ${ page . localeCode } / ${ fileName } `
}
2018-07-08 05:12:43 +00:00
2022-05-27 00:42:14 +00:00
const gitFilePath = ` ./ ${ fileName } `
if ( ( await this . git . checkIgnore ( gitFilePath ) ) . length === 0 ) {
await this . git . rm ( gitFilePath )
await this . git . commit ( ` docs: delete ${ page . path } ` , fileName , {
'--author' : ` " ${ page . authorName } < ${ page . authorEmail } >" `
} )
}
2018-07-08 05:12:43 +00:00
} ,
2019-02-25 04:48:28 +00:00
/ * *
* RENAME
*
* @ param { Object } page Page to rename
* /
2019-02-03 22:08:06 +00:00
async renamed ( page ) {
2019-10-19 00:23:10 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Committing file move from [ ${ page . localeCode } ] ${ page . path } to [ ${ page . destinationLocaleCode } ] ${ page . destinationPath } ... ` )
2020-04-25 00:30:08 +00:00
let sourceFileName = ` ${ page . path } . ${ pageHelper . getFileExtension ( page . contentType ) } `
let destinationFileName = ` ${ page . destinationPath } . ${ pageHelper . getFileExtension ( page . contentType ) } `
2019-06-25 02:13:41 +00:00
2019-10-13 23:59:50 +00:00
if ( WIKI . config . lang . namespacing ) {
if ( WIKI . config . lang . code !== page . localeCode ) {
2020-04-25 00:30:08 +00:00
sourceFileName = ` ${ page . localeCode } / ${ sourceFileName } `
2019-10-13 23:59:50 +00:00
}
if ( WIKI . config . lang . code !== page . destinationLocaleCode ) {
2020-04-25 00:30:08 +00:00
destinationFileName = ` ${ page . destinationLocaleCode } / ${ destinationFileName } `
2019-10-13 23:59:50 +00:00
}
2019-06-25 02:13:41 +00:00
}
2019-02-03 22:08:06 +00:00
2020-04-25 00:30:08 +00:00
const sourceFilePath = path . join ( this . repoPath , sourceFileName )
const destinationFilePath = path . join ( this . repoPath , destinationFileName )
await fs . move ( sourceFilePath , destinationFilePath )
await this . git . rm ( ` ./ ${ sourceFileName } ` )
await this . git . add ( ` ./ ${ destinationFileName } ` )
2019-10-19 21:39:45 +00:00
await this . git . commit ( ` docs: rename ${ page . path } to ${ page . destinationPath } ` , [ sourceFilePath , destinationFilePath ] , {
2019-10-13 23:59:50 +00:00
'--author' : ` " ${ page . moveAuthorName } < ${ page . moveAuthorEmail } >" `
2019-01-28 06:27:04 +00:00
} )
2019-04-06 23:05:47 +00:00
} ,
2019-10-07 04:06:47 +00:00
/ * *
* ASSET UPLOAD
*
* @ param { Object } asset Asset to upload
* /
async assetUploaded ( asset ) {
WIKI . logger . info ( ` (STORAGE/GIT) Committing new file ${ asset . path } ... ` )
const filePath = path . join ( this . repoPath , asset . path )
2019-10-19 21:39:45 +00:00
await fs . outputFile ( filePath , asset . data , 'utf8' )
2019-10-07 04:06:47 +00:00
await this . git . add ( ` ./ ${ asset . path } ` )
await this . git . commit ( ` docs: upload ${ asset . path } ` , asset . path , {
'--author' : ` " ${ asset . authorName } < ${ asset . authorEmail } >" `
} )
} ,
/ * *
* ASSET DELETE
*
* @ param { Object } asset Asset to upload
* /
async assetDeleted ( asset ) {
WIKI . logger . info ( ` (STORAGE/GIT) Committing removed file ${ asset . path } ... ` )
await this . git . rm ( ` ./ ${ asset . path } ` )
await this . git . commit ( ` docs: delete ${ asset . path } ` , asset . path , {
'--author' : ` " ${ asset . authorName } < ${ asset . authorEmail } >" `
} )
} ,
/ * *
* ASSET RENAME
*
* @ param { Object } asset Asset to upload
* /
async assetRenamed ( asset ) {
2019-10-19 21:39:45 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Committing file move from ${ asset . path } to ${ asset . destinationPath } ... ` )
2019-04-06 23:05:47 +00:00
2019-10-19 21:39:45 +00:00
await this . git . mv ( ` ./ ${ asset . path } ` , ` ./ ${ asset . destinationPath } ` )
await this . git . commit ( ` docs: rename ${ asset . path } to ${ asset . destinationPath } ` , [ asset . path , asset . destinationPath ] , {
'--author' : ` " ${ asset . moveAuthorName } < ${ asset . moveAuthorEmail } >" `
2019-10-07 04:06:47 +00:00
} )
} ,
2020-07-12 16:19:01 +00:00
async getLocalLocation ( asset ) {
return path . join ( this . repoPath , asset . path )
} ,
2019-04-06 23:05:47 +00:00
/ * *
* HANDLERS
* /
async importAll ( ) {
WIKI . logger . info ( ` (STORAGE/GIT) Importing all content from local Git repo to the DB... ` )
2019-10-15 03:44:37 +00:00
const rootUser = await WIKI . models . users . getRootUser ( )
2019-04-06 23:05:47 +00:00
await pipeline (
klaw ( this . repoPath , {
filter : ( f ) => {
return ! _ . includes ( f , '.git' )
}
} ) ,
new stream . Transform ( {
objectMode : true ,
transform : async ( file , enc , cb ) => {
const relPath = file . path . substr ( this . repoPath . length + 1 )
2019-10-20 22:34:38 +00:00
if ( file . stats . size < 1 ) {
// Skip directories and zero-byte files
return cb ( )
} else if ( relPath && relPath . length > 3 ) {
2019-04-06 23:05:47 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Processing ${ relPath } ... ` )
await this . processFiles ( [ {
2019-10-15 03:44:37 +00:00
user : rootUser ,
2019-10-20 22:34:38 +00:00
relPath ,
file ,
2019-04-06 23:05:47 +00:00
deletions : 0 ,
2021-10-09 17:59:47 +00:00
insertions : 0 ,
importAll : true
2019-10-15 03:44:37 +00:00
} ] , rootUser )
2019-04-06 23:05:47 +00:00
}
cb ( )
}
} )
)
2019-10-20 22:34:38 +00:00
commonDisk . clearFolderCache ( )
2019-04-06 23:05:47 +00:00
WIKI . logger . info ( '(STORAGE/GIT) Import completed.' )
} ,
async syncUntracked ( ) {
WIKI . logger . info ( ` (STORAGE/GIT) Adding all untracked content... ` )
2019-10-15 03:44:37 +00:00
2019-10-19 21:39:45 +00:00
// -> Pages
2019-04-06 23:05:47 +00:00
await pipeline (
2021-01-29 19:15:22 +00:00
WIKI . models . knex . column ( 'id' , 'path' , 'localeCode' , 'title' , 'description' , 'contentType' , 'content' , 'isPublished' , 'updatedAt' , 'createdAt' , 'editorKey' ) . select ( ) . from ( 'pages' ) . where ( {
2019-04-06 23:05:47 +00:00
isPrivate : false
} ) . stream ( ) ,
new stream . Transform ( {
objectMode : true ,
transform : async ( page , enc , cb ) => {
2021-01-29 19:15:22 +00:00
const pageObject = await WIKI . models . pages . query ( ) . findById ( page . id )
page . tags = await pageObject . $relatedQuery ( 'tags' )
2021-09-01 16:11:33 +00:00
2019-09-27 18:17:12 +00:00
let fileName = ` ${ page . path } . ${ pageHelper . getFileExtension ( page . contentType ) } `
2019-06-25 02:13:41 +00:00
if ( WIKI . config . lang . namespacing && WIKI . config . lang . code !== page . localeCode ) {
fileName = ` ${ page . localeCode } / ${ fileName } `
}
2019-10-19 21:39:45 +00:00
WIKI . logger . info ( ` (STORAGE/GIT) Adding page ${ fileName } ... ` )
2019-04-06 23:05:47 +00:00
const filePath = path . join ( this . repoPath , fileName )
await fs . outputFile ( filePath , pageHelper . injectPageMetadata ( page ) , 'utf8' )
await this . git . add ( ` ./ ${ fileName } ` )
cb ( )
}
} )
)
2019-10-19 21:39:45 +00:00
// -> Assets
const assetFolders = await WIKI . models . assetFolders . getAllPaths ( )
await pipeline (
WIKI . models . knex . column ( 'filename' , 'folderId' , 'data' ) . select ( ) . from ( 'assets' ) . join ( 'assetData' , 'assets.id' , '=' , 'assetData.id' ) . stream ( ) ,
new stream . Transform ( {
objectMode : true ,
transform : async ( asset , enc , cb ) => {
const filename = ( asset . folderId && asset . folderId > 0 ) ? ` ${ _ . get ( assetFolders , asset . folderId ) } / ${ asset . filename } ` : asset . filename
WIKI . logger . info ( ` (STORAGE/GIT) Adding asset ${ filename } ... ` )
await fs . outputFile ( path . join ( this . repoPath , filename ) , asset . data )
await this . git . add ( ` ./ ${ filename } ` )
cb ( )
}
} )
)
2019-10-20 22:34:38 +00:00
2019-04-06 23:05:47 +00:00
await this . git . commit ( ` docs: add all untracked content ` )
WIKI . logger . info ( '(STORAGE/GIT) All content is now tracked.' )
2020-02-04 04:39:00 +00:00
} ,
async purge ( ) {
WIKI . logger . info ( ` (STORAGE/GIT) Purging local repository... ` )
await fs . emptyDir ( this . repoPath )
WIKI . logger . info ( '(STORAGE/GIT) Local repository is now empty. Reinitializing...' )
await this . init ( )
2018-07-08 05:12:43 +00:00
}
}