22
33const os = require ( 'os' )
44const path = require ( 'path' )
5+ const fs = require ( 'fs' )
56const process = require ( 'process' )
7+ const util = require ( 'util' ) ;
8+
9+ const writeFile = util . promisify ( fs . writeFile )
10+ const mkdir = util . promisify ( fs . mkdir )
11+ const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
612
713const chalk = require ( 'chalk' )
814
915const repoVersion = require ( './repo/version' )
1016const migrator = require ( './index' )
17+ const templates = require ( './migration-templates' )
18+ const migrations = require ( '../migrations' )
1119
1220function asyncClosure ( fnc ) {
1321 return function asyncWrapper ( { resolve, ...options } ) {
1422 resolve ( fnc ( options ) )
1523 }
1624}
1725
18- function reportingClosure ( action ) {
26+ function reportingClosure ( action ) {
1927 return ( migration , currentlyMigrated , totalToMigrate ) =>
2028 process . stdout . write ( `${ chalk . green ( `[${ currentlyMigrated } /${ totalToMigrate } ]` ) } Successfully ${ action } ${ chalk . bold ( migration . version ) } : ${ migration . description } \n` )
2129}
@@ -41,6 +49,44 @@ async function status({repoPath}) {
4149 return `${ statusString } \nCurrent repo version: ${ version } \nLast migration's version: ${ lastMigrationVersion } `
4250}
4351
52+ async function getAuthor ( ) {
53+ try {
54+ const name = ( await exec ( 'git config --get user.name' ) ) [ 'stdout' ]
55+ const email = ( await exec ( 'git config --get user.email' ) ) [ 'stdout' ]
56+ return `${ name . replace ( '\n' , '' ) } <${ email . replace ( '\n' , '' ) } >`
57+ } catch ( e ) {
58+ return ''
59+ }
60+ }
61+
62+ async function add ( { repoPath, empty} ) {
63+ const newMigrationVersion = migrator . getLatestMigrationVersion ( ) + 1
64+ const newMigrationFolder = path . join ( __dirname , '..' , 'migrations' , 'migration-' + newMigrationVersion )
65+
66+ const migrationsImport = migrations . map ( ( migration ) => migration . empty ? ` Object.assign({version: ${ migration . version } }, emptyMigration),` : ` require('./migration-${ migration . version } '),` )
67+ if ( empty ) {
68+ migrationsImport . push ( ` Object.assign({version: ${ newMigrationVersion } }, emptyMigration),` )
69+ } else {
70+ migrationsImport . push ( ` require('./migration-${ newMigrationVersion } '),` )
71+ }
72+ const migrationsIndexJsContent = templates . migrationsIndexJs
73+ . replace ( '{{imports}}' , migrationsImport . join ( '\n' ) )
74+ ; await writeFile ( path . join ( newMigrationFolder , '..' , 'index.js' ) , migrationsIndexJsContent )
75+
76+ if ( empty ) return
77+
78+ await mkdir ( newMigrationFolder )
79+
80+ const packageJsonContent = templates . packageJson
81+ . replace ( / { { version} } / gi, newMigrationVersion )
82+ . replace ( / { { author} } / gi, await getAuthor ( ) )
83+ ; await writeFile ( path . join ( newMigrationFolder , 'package.json' ) , packageJsonContent )
84+
85+ const indexJsContent = templates . indexJs
86+ . replace ( / { { version} } / gi, newMigrationVersion )
87+ ; await writeFile ( path . join ( newMigrationFolder , 'index.js' ) , indexJsContent )
88+ }
89+
4490module . exports = {
4591 migrate : {
4692 command : 'migrate' ,
@@ -74,5 +120,15 @@ module.exports = {
74120 command : 'status' ,
75121 describe : 'Display status of IPFS repo' ,
76122 handler : asyncClosure ( status ) ,
77- }
123+ } ,
124+ add : {
125+ command : 'add' ,
126+ describe : 'Bootstrap new migration' ,
127+ handler : asyncClosure ( add ) ,
128+ builder : yargv => yargv
129+ . option ( 'empty' , {
130+ describe : 'Creates empty migration' ,
131+ type : 'boolean'
132+ } )
133+ } ,
78134}
0 commit comments