Skip to content

Commit d592a6d

Browse files
committed
Get graduate working
1 parent 85a5cf0 commit d592a6d

File tree

6 files changed

+61
-19
lines changed

6 files changed

+61
-19
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "create-react-app-scripts",
33
"version": "0.0.1",
44
"scripts": {
5-
"start": "node scripts/start.js local",
6-
"build": "node scripts/build.js local",
5+
"start": "node scripts/start.js",
6+
"build": "node scripts/build.js",
77
"create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\""
88
},
99
"bin": {

scripts/build.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
process.env.NODE_ENV = 'production';
1111

12+
var path = require('path');
1213
var spawnSync = require('child_process').spawnSync;
1314
var webpack = require('webpack');
1415
var config = require('../webpack.config.prod');
1516

16-
var relative = process.argv[2] === 'local' ? '.' : '../..';
17+
var isInNodeModules = 'node_modules' ===
18+
path.basename(path.resolve(path.join(__dirname, '..', '..')));
19+
var relative = isInNodeModules ? '../..' : '.';
1720
spawnSync('rm', ['-rf', relative + '/build']);
1821

1922
webpack(config).run(function(err, stats) {

scripts/graduate.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
*/
99

1010
var fs = require('fs');
11+
var path = require('path');
1112

1213
console.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

1719
var 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
2628
files.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
3941
files.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();
4576
console.log('Done!');

scripts/init.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
*/
99

1010
var fs = require('fs');
11+
var path = require('path');
1112

1213
module.exports = function(hostPath, appName) {
13-
var selfPath = hostPath + '/node_modules/create-react-app-scripts';
14+
var selfPath = path.join(hostPath, 'node_modules', 'create-react-app-scripts');
1415

15-
var hostPackage = require(hostPath + '/package.json');
16-
var selfPackage = require(selfPath + '/package.json');
16+
var hostPackage = require(path.join(hostPath, 'package.json'));
17+
var selfPackage = require(path.join(selfPath, 'package.json'));
1718

1819
// Copy over devDependencies
1920
hostPackage.dependencies = hostPackage.dependencies || {};
@@ -28,12 +29,15 @@ module.exports = function(hostPath, appName) {
2829
command + '-react-app';
2930
});
3031

31-
fs.writeFileSync(hostPath + '/package.json', JSON.stringify(hostPackage, null, 2));
32+
fs.writeFileSync(
33+
path.join(hostPath, 'package.json'),
34+
JSON.stringify(hostPackage, null, 2)
35+
);
3236

3337
// TODO: run npm install in hostPath, (not needed for npm 3 if we accept some hackery)
3438

3539
// Move the src folder
36-
fs.renameSync(selfPath + '/src', hostPath + '/src');
40+
fs.renameSync(path.join(selfPath, 'src'), path.join(hostPath, 'src'));
3741

3842
console.log('Success! Created ' + appName + ' at ' + hostPath + '.');
3943
console.log();

webpack.config.dev.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ var autoprefixer = require('autoprefixer');
1212
var webpack = require('webpack');
1313
var HtmlWebpackPlugin = require('html-webpack-plugin');
1414

15-
var relative = process.argv[2] === 'local' ? '.' : '../..';
15+
var isInNodeModules = 'node_modules' ===
16+
path.basename(path.resolve(path.join(__dirname, '..')));
17+
var relative = isInNodeModules ? '../..' : '.';
1618

1719
module.exports = {
1820
devtool: 'eval',

webpack.config.prod.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ var autoprefixer = require('autoprefixer');
1212
var webpack = require('webpack');
1313
var HtmlWebpackPlugin = require('html-webpack-plugin');
1414

15-
var relative = process.argv[2] === 'local' ? '.' : '../..';
15+
var isInNodeModules = 'node_modules' ===
16+
path.basename(path.resolve(path.join(__dirname, '..')));
17+
var relative = isInNodeModules ? '../..' : '.';
1618

1719
module.exports = {
1820
devtool: 'source-map',

0 commit comments

Comments
 (0)