|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +var fs = require('fs'); |
| 11 | +var path = require('path'); |
| 12 | +var rl = require('readline'); |
| 13 | +var rimrafSync = require('rimraf').sync; |
| 14 | +var spawnSync = require('child_process').spawnSync; |
| 15 | + |
| 16 | +var prompt = function(question, cb) { |
| 17 | + var rlInterface = rl.createInterface({ |
| 18 | + input: process.stdin, |
| 19 | + output: process.stdout, |
| 20 | + }); |
| 21 | + rlInterface.question(question + '\n', function(answer) { |
| 22 | + rlInterface.close(); |
| 23 | + cb(answer); |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +prompt('Are you sure you want to eject? This action is permanent. [Y/n]', function(answer) { |
| 28 | + if (answer && answer !== 'Y' && answer !== 'yes') { |
| 29 | + console.log('Close one! Eject aported.'); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | + |
| 33 | + console.log('Ejecting...'); |
| 34 | + console.log(); |
| 35 | + var selfPath = path.join(__dirname, '..'); |
| 36 | + var hostPath = path.join(selfPath, '..', '..'); |
| 37 | + |
| 38 | + var files = [ |
| 39 | + 'scripts', |
| 40 | + 'webpack.config.dev.js', |
| 41 | + 'webpack.config.prod.js', |
| 42 | + '.eslintrc' |
| 43 | + ]; |
| 44 | + |
| 45 | + // Ensure that the host folder is clean and we won't override any files |
| 46 | + files.forEach(function(file) { |
| 47 | + if (fs.existsSync(path.join(hostPath, file))) { |
| 48 | + console.error( |
| 49 | + '`' + file + '` already exists in your app folder. We cannot ' + |
| 50 | + 'continue as you would lose all the changes in that file or directory. ' + |
| 51 | + 'Please delete it (maybe make a copy for backup) and run this ' + |
| 52 | + 'command again.' |
| 53 | + ); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // Move the files over |
| 59 | + files.forEach(function(file) { |
| 60 | + console.log('Moving ' + file + ' to ' + hostPath); |
| 61 | + fs.renameSync(path.join(selfPath, file), path.join(hostPath, file)); |
| 62 | + }); |
| 63 | + |
| 64 | + // These are unnecessary after graduation |
| 65 | + fs.unlinkSync(path.join(hostPath, 'scripts', 'init.js')); |
| 66 | + fs.unlinkSync(path.join(hostPath, 'scripts', 'eject.js')); |
| 67 | + |
| 68 | + console.log(); |
| 69 | + |
| 70 | + var selfPackage = require(path.join(selfPath, 'package.json')); |
| 71 | + var hostPackage = require(path.join(hostPath, 'package.json')); |
| 72 | + |
| 73 | + console.log('Removing dependency: create-react-app-scripts'); |
| 74 | + delete hostPackage.devDependencies['create-react-app-scripts']; |
| 75 | + |
| 76 | + Object.keys(selfPackage.dependencies).forEach(function (key) { |
| 77 | + console.log('Adding dependency: ' + key); |
| 78 | + hostPackage.devDependencies[key] = selfPackage.dependencies[key]; |
| 79 | + }); |
| 80 | + |
| 81 | + console.log('Updating scripts'); |
| 82 | + Object.keys(hostPackage.scripts).forEach(function (key) { |
| 83 | + hostPackage.scripts[key] = 'node ./scripts/' + key + '.js' |
| 84 | + }); |
| 85 | + delete hostPackage.scripts['eject']; |
| 86 | + |
| 87 | + console.log('Writing package.json'); |
| 88 | + fs.writeFileSync( |
| 89 | + path.join(hostPath, 'package.json'), |
| 90 | + JSON.stringify(hostPackage, null, 2) |
| 91 | + ); |
| 92 | + console.log(); |
| 93 | + |
| 94 | + console.log('Running npm install...'); |
| 95 | + rimrafSync(selfPath); |
| 96 | + spawnSync('npm', ['install'], {stdio: 'inherit'}); |
| 97 | + console.log(); |
| 98 | + |
| 99 | + console.log('Done!'); |
| 100 | + |
| 101 | +}); |
0 commit comments