88 */
99
1010var fs = require ( 'fs' ) ;
11+ var path = require ( 'path' ) ;
1112
1213console . log ( 'Extracting scripts...' ) ;
14+ console . log ( ) ;
1315
14- var hostPath = __dirname ;
15- var selfPath = hostPath + '/node_modules/create-react-app-scripts' ;
16+ var selfPath = path . join ( __dirname , '..' ) ;
17+ var hostPath = path . join ( selfPath , '..' , '..' ) ;
1618
1719var files = [
1820 'scripts' ,
19- '. webpack.config.dev.js' ,
20- '. webpack.config.prod.js' ,
21+ 'webpack.config.dev.js' ,
22+ 'webpack.config.prod.js' ,
2123 '.babelrc' ,
2224 '.eslintrc' ,
2325] ;
2426
2527// Ensure that the host folder is clean and we won't override any files
2628files . forEach ( function ( file ) {
27- if ( fs . existsSync ( hostPath + '/' + file ) ) {
29+ if ( fs . existsSync ( path . join ( hostPath , file ) ) ) {
2830 console . error (
29- '`' + file + '` already exists on your app folder, we cannot ' +
30- 'continue as you would lose all the changes in that file.' ,
31+ '`' + file + '` already exists in your app folder. We cannot ' +
32+ 'continue as you would lose all the changes in that file or directory. ' +
3133 'Please delete it (maybe make a copy for backup) and run this ' +
3234 'command again.'
3335 ) ;
@@ -37,9 +39,38 @@ files.forEach(function(file) {
3739
3840// Move the files over
3941files . forEach ( function ( file ) {
40- fs . renameSync ( selfPath + '/' + file , hostPath + '/' + file ) ;
42+ console . log ( 'Moving ' + file + ' to ' + hostPath ) ;
43+ fs . renameSync ( path . join ( selfPath , file ) , path . join ( hostPath , file ) ) ;
4144} ) ;
4245
43- var hostPackage = require ( hostPath + '/package.json' ) ;
46+ // These are unnecessary after graduation
47+ fs . unlinkSync ( path . join ( hostPath , 'scripts' , 'init.js' ) ) ;
48+ fs . unlinkSync ( path . join ( hostPath , 'scripts' , 'graduate.js' ) ) ;
4449
50+ console . log ( ) ;
51+
52+ var selfPackage = require ( path . join ( selfPath , 'package.json' ) ) ;
53+ var hostPackage = require ( path . join ( hostPath , 'package.json' ) ) ;
54+
55+ console . log ( 'Removing dependency: create-react-app-scripts' ) ;
56+ delete hostPackage . devDependencies [ 'create-react-app-scripts' ] ;
57+
58+ Object . keys ( selfPackage . dependencies ) . forEach ( function ( key ) {
59+ console . log ( 'Adding dependency: ' + key ) ;
60+ hostPackage . devDependencies [ key ] = selfPackage . dependencies [ key ] ;
61+ } ) ;
62+
63+ console . log ( 'Updating scripts' ) ;
64+ Object . keys ( hostPackage . scripts ) . forEach ( function ( key ) {
65+ hostPackage . scripts [ key ] = 'node ./scripts/' + key + '.js'
66+ } ) ;
67+ delete hostPackage . scripts [ 'graduate' ] ;
68+
69+ console . log ( 'Writing package.json...' ) ;
70+ fs . writeFileSync (
71+ path . join ( hostPath , 'package.json' ) ,
72+ JSON . stringify ( hostPackage , null , 2 )
73+ ) ;
74+
75+ console . log ( ) ;
4576console . log ( 'Done!' ) ;
0 commit comments