|
| 1 | +var fs = require('fs'); |
| 2 | + |
| 3 | +if (!( |
| 4 | + process.argv[2] === '--i-know-what-im-doing' && |
| 5 | + process.argv[3] === '--there-is-no-going-back' |
| 6 | +)) { |
| 7 | + console.log( |
| 8 | + 'This command will copy all the scripts and configurations', |
| 9 | + 'from the create-react-app command to your directory.', |
| 10 | + 'You will be able to tweak and extend them but be aware that', |
| 11 | + 'this is a one way operation, there is no going back' |
| 12 | + ); |
| 13 | + console.log( |
| 14 | + 'If you want to run this, please type the following command' |
| 15 | + ); |
| 16 | + console.log( |
| 17 | + ' npm run export-scripts --i-know-what-im-doing --there-is-no-going-back' |
| 18 | + ); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +console.log('Extracting scripts...'); |
| 23 | + |
| 24 | +var hostPath = __dirname; |
| 25 | +var selfPath = hostPath + '/node_modules/create-react-app-scripts'; |
| 26 | + |
| 27 | +var files = [ |
| 28 | + 'scripts', |
| 29 | + '.webpack.config.dev.js', |
| 30 | + '.webpack.config.prod.js', |
| 31 | + '.babelrc', |
| 32 | + '.eslintrc', |
| 33 | +]; |
| 34 | + |
| 35 | +// Ensure that the host folder is clean and we won't override any files |
| 36 | +files.forEach(function(file) { |
| 37 | + if (fs.existsSync(hostPath + '/' + file)) { |
| 38 | + console.error( |
| 39 | + '`' + file + '` already exists on your app folder, we cannot ' + |
| 40 | + 'continue as you would lose all the changes.', |
| 41 | + 'Please delete it (maybe make a copy for backup) and run this ' + |
| 42 | + 'command again.' |
| 43 | + ); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +// Move the files over |
| 49 | +files.forEach(function(file) { |
| 50 | + fs.renameSync(selfPath + '/' + file, hostPath + '/' + file); |
| 51 | +}); |
| 52 | + |
| 53 | +var hostPackage = require(hostPath + '/package.json'); |
| 54 | +var selfPackage = require(selfPath + '/package.json'); |
| 55 | + |
| 56 | +// Copy over dependencies |
| 57 | +hostPackage.dependencies = hostPackage.dependencies || {}; |
| 58 | +for (var key in selfPackage.dependencies) { |
| 59 | + hostPackage.devDependencies[key] = selfPackage.dependencies[key]; |
| 60 | +} |
| 61 | + |
| 62 | +delete hostPackage.dependencies['create-react-app-scripts']; |
| 63 | + |
| 64 | +// Update the script rules |
| 65 | +['start', 'build'].forEach(function(command) { |
| 66 | + hostPackage.scripts[command] = 'node scripts/' + command + '.js local'; |
| 67 | +}); |
| 68 | +delete hostPackage['export-scripts']; |
| 69 | + |
| 70 | +fs.writeFileSync(hostPath + '/package.json', JSON.stringify(hostPackage, null, 2)); |
| 71 | + |
| 72 | +// TODO: run npm install in hostPath |
| 73 | + |
| 74 | +// Move the src folder |
| 75 | + |
| 76 | +console.log('Done!'); |
0 commit comments