graduate.js 2.39 KB
Newer Older
Christopher Chedeau's avatar
Christopher Chedeau committed
1
2
3
4
5
6
7
8
9
10
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

var fs = require('fs');
Dan Abramov's avatar
Dan Abramov committed
11
var path = require('path');
12
var rimrafSync = require('rimraf').sync;
Dan Abramov's avatar
Dan Abramov committed
13
var spawnSync = require('child_process').spawnSync;
Christopher Chedeau's avatar
Christopher Chedeau committed
14

Dan Abramov's avatar
Dan Abramov committed
15
console.log('Graduating...');
Dan Abramov's avatar
Dan Abramov committed
16
console.log();
Christopher Chedeau's avatar
Christopher Chedeau committed
17

Dan Abramov's avatar
Dan Abramov committed
18
19
var selfPath = path.join(__dirname, '..');
var hostPath = path.join(selfPath, '..', '..');
Christopher Chedeau's avatar
Christopher Chedeau committed
20
21
22

var files = [
  'scripts',
Dan Abramov's avatar
Dan Abramov committed
23
24
  'webpack.config.dev.js',
  'webpack.config.prod.js',
25
  '.eslintrc'
Christopher Chedeau's avatar
Christopher Chedeau committed
26
27
28
29
];

// Ensure that the host folder is clean and we won't override any files
files.forEach(function(file) {
Dan Abramov's avatar
Dan Abramov committed
30
  if (fs.existsSync(path.join(hostPath, file))) {
Christopher Chedeau's avatar
Christopher Chedeau committed
31
    console.error(
Dan Abramov's avatar
Dan Abramov committed
32
33
      '`' + file + '` already exists in your app folder. We cannot ' +
      'continue as you would lose all the changes in that file or directory. ' +
Christopher Chedeau's avatar
Christopher Chedeau committed
34
35
36
37
38
39
40
41
42
      'Please delete it (maybe make a copy for backup) and run this ' +
      'command again.'
    );
    process.exit(1);
  }
});

// Move the files over
files.forEach(function(file) {
Dan Abramov's avatar
Dan Abramov committed
43
44
  console.log('Moving ' + file + ' to ' + hostPath);
  fs.renameSync(path.join(selfPath, file), path.join(hostPath, file));
Christopher Chedeau's avatar
Christopher Chedeau committed
45
46
});

Dan Abramov's avatar
Dan Abramov committed
47
48
49
// These are unnecessary after graduation
fs.unlinkSync(path.join(hostPath, 'scripts', 'init.js'));
fs.unlinkSync(path.join(hostPath, 'scripts', 'graduate.js'));
Christopher Chedeau's avatar
Christopher Chedeau committed
50

Dan Abramov's avatar
Dan Abramov committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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'];

Dan Abramov's avatar
Dan Abramov committed
70
console.log('Writing package.json');
Dan Abramov's avatar
Dan Abramov committed
71
72
73
74
fs.writeFileSync(
  path.join(hostPath, 'package.json'),
  JSON.stringify(hostPackage, null, 2)
);
Dan Abramov's avatar
Dan Abramov committed
75
console.log();
Dan Abramov's avatar
Dan Abramov committed
76

Dan Abramov's avatar
Dan Abramov committed
77
console.log('Running npm install...');
78
rimrafSync(selfPath);
Dan Abramov's avatar
Dan Abramov committed
79
spawnSync('npm', ['install'], {stdio: 'inherit'});
Dan Abramov's avatar
Dan Abramov committed
80
console.log();
Dan Abramov's avatar
Dan Abramov committed
81

Christopher Chedeau's avatar
Christopher Chedeau committed
82
console.log('Done!');