@@ -5,65 +5,77 @@ const path = require('path')
55const chalk = require ( 'chalk' )
66const inquirer = require ( 'inquirer' )
77
8- const scriptsPath = path . join ( process . cwd ( ) , 'scripts' )
9- const ownPackageJsonPath = path . resolve ( __dirname , '..' , 'package.json ' )
10- const appPackageJsonPath = path . join ( process . cwd ( ) , 'package.json' )
11- const ownPackageJson = JSON . parse ( fs . readFileSync ( ownPackageJsonPath ) )
12- const appPackageJson = JSON . parse ( fs . readFileSync ( appPackageJsonPath ) )
8+ const appPath = path . join ( process . cwd ( ) )
9+ const appScriptsPath = path . join ( process . cwd ( ) , 'scripts ' )
10+ const flavorPackageJson = require ( path . join ( __dirname , '..' , 'package.json' ) )
11+ const appPackageJson = require ( path . join ( appPath , 'package.json' ) )
12+ const language = appPackageJson . cca . language
1313
14- // Ask the user for confirmation before ejecting.
15- // Abort in case of negative answer (default)
1614const ejectConfirmation = {
1715 type : 'confirm' ,
1816 name : 'doEject' ,
1917 default : false ,
2018 message : 'Are you sure you want to eject? This action is permanent.'
2119}
2220
21+ // Ask the user for confirmation before ejecting.
2322inquirer . prompt ( [ ejectConfirmation ] ) . then ( answers => {
24- if ( ! answers . doEject . value ) {
23+ // Abort in case of negative answer (default)
24+ if ( answers . doEject . value ) {
2525 console . log ( chalk . cyan ( 'Eject aborted!' ) )
26- return
26+ process . exit ( 0 )
2727 }
28- } )
29-
30- // Declaring new scripts
31- const scripts = {
32- start : 'node scripts/start.js' ,
33- test : 'node scripts/test.js' ,
34- build : 'node scripts/build.js'
35- }
36-
37- // Declare the new dependencies, excluding self
38- let devDependencies = { }
39- Object . keys ( appPackageJson . devDependencies )
40- . filter ( dep => { return dep !== ownPackageJson . name } )
41- . forEach ( dep => {
42- devDependencies [ dep ] = appPackageJson . devDependencies [ dep ]
43- } )
44- devDependencies = Object . assign ( { } , devDependencies , ownPackageJson . dependencies )
4528
46- // Write the new package.json
47- const newPackageJson = Object . assign ( { } , appPackageJson , { scripts : scripts , devDependencies : devDependencies } )
48- fs . writeFileSync (
49- appPackageJsonPath ,
50- JSON . stringify ( newPackageJson , null , 2 )
51- )
52-
53- // Copy scripts
54- function copyScript ( script ) {
55- fs . copySync ( path . join ( __dirname , script ) , path . join ( scriptsPath , script ) )
56- }
29+ // STEP 1 - Prepare package.json
30+ // Declaring new scripts
31+ const scripts = {
32+ start : 'node scripts/start.js' ,
33+ test : 'node scripts/test.js' ,
34+ build : 'node scripts/build.js'
35+ }
36+ // Remove flavor from devpendencies
37+ delete appPackageJson . devDependencies [ flavorPackageJson . name ]
38+ // Remove cca settings
39+ delete appPackageJson . cca
40+ // Flavor's dependencies -> application devDependency.
41+ // We merge flavor's dependencies with application's devDepependencies
42+ const devDependencies = Object . assign (
43+ { } ,
44+ appPackageJson . devDependencies ,
45+ flavorPackageJson . dependencies
46+ )
47+ // New package.json content
48+ const packageJsonContent = Object . assign (
49+ { } ,
50+ appPackageJson ,
51+ {
52+ scripts,
53+ devDependencies
54+ }
55+ )
56+ // Overide application's package.json with the new content
57+ fs . writeFileSync (
58+ path . join ( appPath , 'package.json' ) ,
59+ JSON . stringify ( packageJsonContent , null , 2 )
60+ )
5761
58- fs . ensureDirSync ( scriptsPath )
59- copyScript ( 'start.js' )
60- copyScript ( 'test.js' )
61- copyScript ( 'build.js' )
62+ // STEP 2 - Copy scripts
63+ function copyScript ( script ) {
64+ fs . copySync ( path . join ( __dirname , script ) , path . join ( appScriptsPath , script ) )
65+ }
66+ // Make sure appScriptsPath exists
67+ fs . ensureDirSync ( appScriptsPath )
68+ // Copy over start, test and build scripts
69+ copyScript ( 'start.js' )
70+ copyScript ( 'test.js' )
71+ copyScript ( 'build.js' )
6272
63- // Copy utils
64- fs . copySync ( path . join ( __dirname , 'utils' ) , path . join ( scriptsPath , 'utils' ) )
73+ // STEP 3 - Copy utils
74+ fs . copySync ( path . join ( __dirname , 'utils' ) , path . join ( appScriptsPath , 'utils' ) )
6575
66- // Copy configs
67- fs . copySync ( path . join ( __dirname , '../' , 'configs' ) , path . join ( 'configs' ) )
76+ // STEP 4 - Copy configs
77+ fs . copySync ( path . join ( __dirname , '../' , 'configs' , language ) , path . join ( appPath , 'configs' ) )
78+ fs . copySync ( path . join ( __dirname , '../' , 'configs' , 'webpackDevServer.config.js' ) , path . join ( appPath , 'configs' , 'webpackDevServer.config.js' ) )
6879
69- // Todo: provide some success info on success
80+ // TODO sucess message
81+ } )
0 commit comments