@@ -8,8 +8,10 @@ const fs = require('fs');
88const path = require ( 'path' ) ;
99const cp = require ( 'child_process' ) ;
1010const dbmUtil = require ( 'db-migrate-shared' ) . util ;
11+ const mkdirp = Promise . promisify ( require ( 'mkdirp' ) ) ;
1112
1213const rmdir = Promise . promisify ( require ( 'rimraf' ) ) ;
14+ const writeFile = Promise . promisify ( fs . writeFile ) ;
1315
1416function wipeMigrations ( ) {
1517 const dir = path . join ( __dirname , 'migrations' ) ;
@@ -49,9 +51,48 @@ lab.experiment('create', function () {
4951
5052 lab . test ( 'will create a new migration' , ( ) => {
5153 const files = fs . readdirSync ( path . join ( __dirname , 'migrations' ) ) ;
52- Code . expect ( files . length ) . to . equal ( 1 ) ;
53- const file = files [ 0 ] ;
54- Code . expect ( file ) . to . match ( / f i r s t - m i g r a t i o n \. j s $ / ) ;
54+ const file = files . find ( file => / f i r s t - m i g r a t i o n \. j s $ / . test ( file ) ) ;
55+ Code . expect ( file ) . to . exist ( ) ;
56+ } ) ;
57+
58+ lab . test ( 'will create migrations/package.json' , ( ) => {
59+ const files = fs . readdirSync ( path . join ( __dirname , 'migrations' ) ) ;
60+ const packageJson = files . find ( file => / p a c k a g e \. j s o n $ / . test ( file ) ) ;
61+ Code . expect ( packageJson ) . to . exist ( ) ;
62+ } )
63+ } ) ;
64+
65+ lab . experiment (
66+ 'with existing migrations/package.json file' , ( ) => {
67+ let exitCode ;
68+
69+ lab . before ( async ( ) => {
70+ await wipeMigrations ( ) ;
71+
72+ await mkdirp ( path . join ( __dirname , 'migrations' ) ) ;
73+
74+ await writeFile ( path . join ( __dirname , 'migrations' , 'package.json' ) , '{"name": "test", "type": "module"}' ) ;
75+
76+ const db = dbMigrate ( 'create' , 'first migration' ) ;
77+ // db.stderr.on('data', data => console.log(data.toString()));
78+ // db.stdout.on('data', data => console.log(data.toString()));
79+
80+ const exitCodePromise = new Promise ( ( resolve ) => db . on ( 'exit' , resolve ) ) ;
81+ exitCode = await exitCodePromise ;
82+ } ) ;
83+
84+ lab . test ( 'does not cause an error' , ( ) => {
85+ Code . expect ( exitCode ) . to . equal ( 0 ) ;
86+ } ) ;
87+
88+ lab . test ( 'will modify an existing migrations/package.json\'s type field' , ( ) => {
89+ const packageJson = JSON . parse ( fs . readFileSync ( path . join ( __dirname , 'migrations' , 'package.json' ) ) )
90+ Code . expect ( packageJson . type ) . to . equal ( "commonjs" ) ;
91+ } ) ;
92+
93+ lab . test ( 'will preserve other properties in an existing package.json' , ( ) => {
94+ const packageJson = JSON . parse ( fs . readFileSync ( path . join ( __dirname , 'migrations' , 'package.json' ) ) )
95+ Code . expect ( packageJson . name ) . to . equal ( "test" ) ;
5596 } ) ;
5697 } ) ;
5798
@@ -84,7 +125,7 @@ lab.experiment('create', function () {
84125 for ( let i = 0 ; i < files . length ; i ++ ) {
85126 const file = files [ i ] ;
86127 const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
87- if ( stats . isFile ( ) ) {
128+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
88129 Code . expect ( file ) . to . match ( / s e c o n d - m i g r a t i o n \. j s $ / ) ;
89130 }
90131 }
@@ -97,9 +138,8 @@ lab.experiment('create', function () {
97138
98139 lab . test ( 'will create a new migration sql up file' , ( ) => {
99140 const files = fs . readdirSync ( path . join ( __dirname , 'migrations/sqls' ) ) ;
100- Code . expect ( files . length ) . to . equal ( 2 ) ;
101- const file = files [ 1 ] ;
102- Code . expect ( file ) . to . match ( / s e c o n d - m i g r a t i o n - u p \. s q l $ / ) ;
141+ const file = files . find ( file => / s e c o n d - m i g r a t i o n - u p \. s q l $ / . test ( file ) ) ;
142+ Code . expect ( file ) . to . exist ( ) ;
103143 } ) ;
104144 }
105145 ) ;
@@ -129,7 +169,7 @@ lab.experiment('create', function () {
129169 for ( let i = 0 ; i < files . length ; i ++ ) {
130170 const file = files [ i ] ;
131171 const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
132- if ( stats . isFile ( ) ) {
172+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
133173 Code . expect ( file ) . to . match ( / t h i r d - m i g r a t i o n \. j s $ / ) ;
134174 }
135175 }
@@ -142,9 +182,8 @@ lab.experiment('create', function () {
142182
143183 lab . test ( 'will create a new migration sql up file' , ( ) => {
144184 const files = fs . readdirSync ( path . join ( __dirname , 'migrations/sqls' ) ) ;
145- Code . expect ( files . length ) . to . equal ( 2 ) ;
146- const file = files [ 1 ] ;
147- Code . expect ( file ) . to . match ( / t h i r d - m i g r a t i o n - u p \. s q l $ / ) ;
185+ const file = files . find ( file => / t h i r d - m i g r a t i o n - u p \. s q l $ / . test ( file ) ) ;
186+ Code . expect ( file ) . to . exist ( ) ;
148187 } ) ;
149188 }
150189 ) ;
@@ -177,7 +216,7 @@ lab.experiment('create', function () {
177216 for ( let i = 0 ; i < files . length ; i ++ ) {
178217 const file = files [ i ] ;
179218 const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
180- if ( stats . isFile ( ) ) {
219+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
181220 Code . expect ( file ) . to . match ( / f o u r t h - m i g r a t i o n \. c o f f e e $ / ) ;
182221 }
183222 }
@@ -211,19 +250,17 @@ lab.experiment('create', function () {
211250
212251 lab . test ( 'will create a new migration' , ( ) => {
213252 const files = fs . readdirSync ( path . join ( __dirname , 'migrations/test' ) ) ;
214- Code . expect ( files . length ) . to . equal ( 2 ) ;
215- const file = files [ 0 ] ;
216- Code . expect ( file ) . to . match ( / f i r s t - m i g r a t i o n \. j s $ / ) ;
253+ const file = files . find ( file => / f i r s t - m i g r a t i o n \. j s $ / . test ( file ) ) ;
254+ Code . expect ( file ) . to . exist ( ) ;
217255 } ) ;
218256 lab . test ( 'will create a new migration/test/sqls directory' , ( ) => {
219257 const stats = fs . statSync ( path . join ( __dirname , 'migrations/test/sqls' ) ) ;
220258 Code . expect ( stats . isDirectory ( ) ) . to . be . true ( ) ;
221259 } ) ;
222260 lab . test ( 'will create a new migration sql up file' , ( ) => {
223261 const files = fs . readdirSync ( path . join ( __dirname , 'migrations/test/sqls' ) ) ;
224- Code . expect ( files . length ) . to . equal ( 2 ) ;
225- const file = files [ 1 ] ;
226- Code . expect ( file ) . to . match ( / f i r s t - m i g r a t i o n - u p \. s q l $ / ) ;
262+ const file = files . find ( file => / f i r s t - m i g r a t i o n - u p \. s q l $ / . test ( file ) ) ;
263+ Code . expect ( file ) . to . exist ( ) ;
227264 } ) ;
228265 } ) ;
229266 }
@@ -254,7 +291,7 @@ lab.experiment('create', function () {
254291 for ( let i = 0 ; i < files . length ; i ++ ) {
255292 const file = files [ i ] ;
256293 const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
257- if ( stats . isFile ( ) ) {
294+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
258295 Code . expect ( file ) . to . match ( / f i f t h - m i g r a t i o n \. c o f f e e $ / ) ;
259296 }
260297 }
@@ -306,7 +343,7 @@ lab.experiment('create', function () {
306343 for ( let i = 0 ; i < files . length ; i ++ ) {
307344 const file = files [ i ] ;
308345 const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
309- if ( stats . isFile ( ) ) {
346+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
310347 Code . expect ( file ) . to . match ( / s i x t h - m i g r a t i o n \. j s $ / ) ;
311348 }
312349 }
0 commit comments