2
2
3
3
const os = require ( 'os' )
4
4
const path = require ( 'path' )
5
+ const fs = require ( 'fs' )
5
6
const 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 ) ;
6
12
7
13
const chalk = require ( 'chalk' )
8
14
9
15
const repoVersion = require ( './repo/version' )
10
16
const migrator = require ( './index' )
17
+ const templates = require ( './migration-templates' )
18
+ const migrations = require ( '../migrations' )
11
19
12
20
function asyncClosure ( fnc ) {
13
21
return function asyncWrapper ( { resolve, ...options } ) {
14
22
resolve ( fnc ( options ) )
15
23
}
16
24
}
17
25
18
- function reportingClosure ( action ) {
26
+ function reportingClosure ( action ) {
19
27
return ( migration , currentlyMigrated , totalToMigrate ) =>
20
28
process . stdout . write ( `${ chalk . green ( `[${ currentlyMigrated } /${ totalToMigrate } ]` ) } Successfully ${ action } ${ chalk . bold ( migration . version ) } : ${ migration . description } \n` )
21
29
}
@@ -41,6 +49,44 @@ async function status({repoPath}) {
41
49
return `${ statusString } \nCurrent repo version: ${ version } \nLast migration's version: ${ lastMigrationVersion } `
42
50
}
43
51
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
+
44
90
module . exports = {
45
91
migrate : {
46
92
command : 'migrate' ,
@@ -74,5 +120,15 @@ module.exports = {
74
120
command : 'status' ,
75
121
describe : 'Display status of IPFS repo' ,
76
122
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
+ } ,
78
134
}
0 commit comments