From 2987cc69b97503d9f413ba2d20f82a33b7cc6a32 Mon Sep 17 00:00:00 2001
From: Dan Abramov <dan.abramov@gmail.com>
Date: Sun, 17 Jul 2016 13:46:12 +0100
Subject: [PATCH] Get graduate working

---
 package.json           |  4 ++--
 scripts/build.js       |  5 ++++-
 scripts/graduate.js    | 49 ++++++++++++++++++++++++++++++++++--------
 scripts/init.js        | 14 +++++++-----
 webpack.config.dev.js  |  4 +++-
 webpack.config.prod.js |  4 +++-
 6 files changed, 61 insertions(+), 19 deletions(-)

diff --git a/package.json b/package.json
index 996112167..fe3a5692d 100644
--- a/package.json
+++ b/package.json
@@ -2,8 +2,8 @@
   "name": "create-react-app-scripts",
   "version": "0.0.1",
   "scripts": {
-    "start": "node scripts/start.js local",
-    "build": "node scripts/build.js local",
+    "start": "node scripts/start.js",
+    "build": "node scripts/build.js",
     "create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\""
   },
   "bin": {
diff --git a/scripts/build.js b/scripts/build.js
index 93b08992d..09ef8bdaf 100644
--- a/scripts/build.js
+++ b/scripts/build.js
@@ -9,11 +9,14 @@
 
 process.env.NODE_ENV = 'production';
 
+var path = require('path');
 var spawnSync = require('child_process').spawnSync;
 var webpack = require('webpack');
 var config = require('../webpack.config.prod');
 
-var relative = process.argv[2] === 'local' ? '.' : '../..';
+var isInNodeModules = 'node_modules' ===
+  path.basename(path.resolve(path.join(__dirname, '..', '..')));
+var relative = isInNodeModules ? '../..' : '.';
 spawnSync('rm', ['-rf', relative + '/build']);
 
 webpack(config).run(function(err, stats) {
diff --git a/scripts/graduate.js b/scripts/graduate.js
index 5d646a24a..58d9719c4 100644
--- a/scripts/graduate.js
+++ b/scripts/graduate.js
@@ -8,26 +8,28 @@
  */
 
 var fs = require('fs');
+var path = require('path');
 
 console.log('Extracting scripts...');
+console.log();
 
-var hostPath = __dirname;
-var selfPath = hostPath + '/node_modules/create-react-app-scripts';
+var selfPath = path.join(__dirname, '..');
+var hostPath = path.join(selfPath, '..', '..');
 
 var files = [
   'scripts',
-  '.webpack.config.dev.js',
-  '.webpack.config.prod.js',
+  'webpack.config.dev.js',
+  'webpack.config.prod.js',
   '.babelrc',
   '.eslintrc',
 ];
 
 // Ensure that the host folder is clean and we won't override any files
 files.forEach(function(file) {
-  if (fs.existsSync(hostPath + '/' + file)) {
+  if (fs.existsSync(path.join(hostPath, file))) {
     console.error(
-      '`' + file + '` already exists on your app folder, we cannot ' +
-      'continue as you would lose all the changes in that file.',
+      '`' + file + '` already exists in your app folder. We cannot ' +
+      'continue as you would lose all the changes in that file or directory. ' +
       'Please delete it (maybe make a copy for backup) and run this ' +
       'command again.'
     );
@@ -37,9 +39,38 @@ files.forEach(function(file) {
 
 // Move the files over
 files.forEach(function(file) {
-  fs.renameSync(selfPath + '/' + file, hostPath + '/' + file);
+  console.log('Moving ' + file + ' to ' + hostPath);
+  fs.renameSync(path.join(selfPath, file), path.join(hostPath, file));
 });
 
-var hostPackage = require(hostPath + '/package.json');
+// These are unnecessary after graduation
+fs.unlinkSync(path.join(hostPath, 'scripts', 'init.js'));
+fs.unlinkSync(path.join(hostPath, 'scripts', 'graduate.js'));
 
+console.log();
+
+var selfPackage = require(path.join(selfPath, 'package.json'));
+var hostPackage = require(path.join(hostPath, 'package.json'));
+
+console.log('Removing dependency: create-react-app-scripts');
+delete hostPackage.devDependencies['create-react-app-scripts'];
+
+Object.keys(selfPackage.dependencies).forEach(function (key) {
+  console.log('Adding dependency: ' + key);
+  hostPackage.devDependencies[key] = selfPackage.dependencies[key];
+});
+
+console.log('Updating scripts');
+Object.keys(hostPackage.scripts).forEach(function (key) {
+  hostPackage.scripts[key] = 'node ./scripts/' + key + '.js'
+});
+delete hostPackage.scripts['graduate'];
+
+console.log('Writing package.json...');
+fs.writeFileSync(
+  path.join(hostPath, 'package.json'),
+  JSON.stringify(hostPackage, null, 2)
+);
+
+console.log();
 console.log('Done!');
diff --git a/scripts/init.js b/scripts/init.js
index a8d5d04e9..6c3dc72bc 100644
--- a/scripts/init.js
+++ b/scripts/init.js
@@ -8,12 +8,13 @@
  */
 
 var fs = require('fs');
+var path = require('path');
 
 module.exports = function(hostPath, appName) {
-  var selfPath = hostPath + '/node_modules/create-react-app-scripts';
+  var selfPath = path.join(hostPath, 'node_modules', 'create-react-app-scripts');
 
-  var hostPackage = require(hostPath + '/package.json');
-  var selfPackage = require(selfPath + '/package.json');
+  var hostPackage = require(path.join(hostPath, 'package.json'));
+  var selfPackage = require(path.join(selfPath, 'package.json'));
 
   // Copy over devDependencies
   hostPackage.dependencies = hostPackage.dependencies || {};
@@ -28,12 +29,15 @@ module.exports = function(hostPath, appName) {
       command + '-react-app';
   });
 
-  fs.writeFileSync(hostPath + '/package.json', JSON.stringify(hostPackage, null, 2));
+  fs.writeFileSync(
+    path.join(hostPath, 'package.json'),
+    JSON.stringify(hostPackage, null, 2)
+  );
 
   // TODO: run npm install in hostPath, (not needed for npm 3 if we accept some hackery)
 
   // Move the src folder
-  fs.renameSync(selfPath + '/src', hostPath + '/src');
+  fs.renameSync(path.join(selfPath, 'src'), path.join(hostPath, 'src'));
 
   console.log('Success! Created ' + appName + ' at ' + hostPath + '.');
   console.log();
diff --git a/webpack.config.dev.js b/webpack.config.dev.js
index e9b8a7f54..5b8847233 100644
--- a/webpack.config.dev.js
+++ b/webpack.config.dev.js
@@ -12,7 +12,9 @@ var autoprefixer = require('autoprefixer');
 var webpack = require('webpack');
 var HtmlWebpackPlugin = require('html-webpack-plugin');
 
-var relative = process.argv[2] === 'local' ? '.' : '../..';
+var isInNodeModules = 'node_modules' ===
+  path.basename(path.resolve(path.join(__dirname, '..')));
+var relative = isInNodeModules ? '../..' : '.';
 
 module.exports = {
   devtool: 'eval',
diff --git a/webpack.config.prod.js b/webpack.config.prod.js
index 20b5d7a28..76a445057 100644
--- a/webpack.config.prod.js
+++ b/webpack.config.prod.js
@@ -12,7 +12,9 @@ var autoprefixer = require('autoprefixer');
 var webpack = require('webpack');
 var HtmlWebpackPlugin = require('html-webpack-plugin');
 
-var relative = process.argv[2] === 'local' ? '.' : '../..';
+var isInNodeModules = 'node_modules' ===
+  path.basename(path.resolve(path.join(__dirname, '..')));
+var relative = isInNodeModules ? '../..' : '.';
 
 module.exports = {
   devtool: 'source-map',
-- 
GitLab