@reactunity/scripts 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/scripts/eject.js CHANGED
@@ -16,10 +16,11 @@ process.on('unhandledRejection', err => {
16
16
 
17
17
  const fs = require('fs-extra');
18
18
  const path = require('path');
19
+ const prompts = require('prompts');
19
20
  const execSync = require('child_process').execSync;
20
21
  const chalk = require('react-dev-utils/chalk');
21
22
  const paths = require('../config/paths');
22
- const inquirer = require('react-dev-utils/inquirer');
23
+ const createJestConfig = require('./utils/createJestConfig');
23
24
  const spawnSync = require('react-dev-utils/crossSpawn').sync;
24
25
  const os = require('os');
25
26
 
@@ -61,273 +62,275 @@ console.log(
61
62
  );
62
63
  console.log();
63
64
 
64
- inquirer
65
- .prompt({
66
- type: 'confirm',
67
- name: 'shouldEject',
68
- message: 'Are you sure you want to eject? This action is permanent.',
69
- default: false,
70
- })
71
- .then(answer => {
72
- if (!answer.shouldEject) {
73
- console.log(cyan('Close one! Eject aborted.'));
74
- return;
75
- }
65
+ prompts({
66
+ type: 'confirm',
67
+ name: 'shouldEject',
68
+ message: 'Are you sure you want to eject? This action is permanent.',
69
+ initial: false,
70
+ }).then(answer => {
71
+ if (!answer.shouldEject) {
72
+ console.log(cyan('Close one! Eject aborted.'));
73
+ return;
74
+ }
75
+
76
+ const gitStatus = getGitStatus();
77
+ if (gitStatus) {
78
+ console.error(
79
+ chalk.red(
80
+ 'This git repository has untracked files or uncommitted changes:'
81
+ ) +
82
+ '\n\n' +
83
+ gitStatus
84
+ .split('\n')
85
+ .map(line => line.match(/ .*/g)[0].trim())
86
+ .join('\n') +
87
+ '\n\n' +
88
+ chalk.red(
89
+ 'Remove untracked files, stash or commit any changes, and try again.'
90
+ )
91
+ );
92
+ process.exit(1);
93
+ }
76
94
 
77
- const gitStatus = getGitStatus();
78
- if (gitStatus) {
95
+ console.log('Ejecting...');
96
+
97
+ const ownPath = paths.ownPath;
98
+ const appPath = paths.appPath;
99
+
100
+ function verifyAbsent(file) {
101
+ if (fs.existsSync(path.join(appPath, file))) {
79
102
  console.error(
80
- chalk.red(
81
- 'This git repository has untracked files or uncommitted changes:'
82
- ) +
83
- '\n\n' +
84
- gitStatus
85
- .split('\n')
86
- .map(line => line.match(/ .*/g)[0].trim())
87
- .join('\n') +
88
- '\n\n' +
89
- chalk.red(
90
- 'Remove untracked files, stash or commit any changes, and try again.'
91
- )
103
+ `\`${file}\` already exists in your app folder. We cannot ` +
104
+ 'continue as you would lose all the changes in that file or directory. ' +
105
+ 'Please move or delete it (maybe make a copy for backup) and run this ' +
106
+ 'command again.'
92
107
  );
93
108
  process.exit(1);
94
109
  }
110
+ }
95
111
 
96
- console.log('Ejecting...');
112
+ const folders = ['config', 'config/jest', 'scripts', 'config/webpack/persistentCache'];
113
+
114
+ // Make shallow array of files paths
115
+ const files = folders.reduce((files, folder) => {
116
+ return files.concat(
117
+ fs
118
+ .readdirSync(path.join(ownPath, folder))
119
+ // set full path
120
+ .map(file => path.join(ownPath, folder, file))
121
+ // omit dirs from file list
122
+ .filter(file => fs.lstatSync(file).isFile())
123
+ );
124
+ }, []);
97
125
 
98
- const ownPath = paths.ownPath;
99
- const appPath = paths.appPath;
126
+ // Ensure that the app folder is clean and we won't override any files
127
+ folders.forEach(verifyAbsent);
128
+ files.forEach(verifyAbsent);
100
129
 
101
- function verifyAbsent(file) {
102
- if (fs.existsSync(path.join(appPath, file))) {
103
- console.error(
104
- `\`${file}\` already exists in your app folder. We cannot ` +
105
- 'continue as you would lose all the changes in that file or directory. ' +
106
- 'Please move or delete it (maybe make a copy for backup) and run this ' +
107
- 'command again.'
108
- );
109
- process.exit(1);
110
- }
111
- }
130
+ // Prepare Jest config early in case it throws
131
+ const jestConfig = createJestConfig(
132
+ filePath => path.posix.join('<rootDir>', filePath),
133
+ null,
134
+ true
135
+ );
112
136
 
113
- const folders = ['config', 'scripts'];
114
-
115
- // Make shallow array of files paths
116
- const files = folders.reduce((files, folder) => {
117
- return files.concat(
118
- fs
119
- .readdirSync(path.join(ownPath, folder))
120
- // set full path
121
- .map(file => path.join(ownPath, folder, file))
122
- // omit dirs from file list
123
- .filter(file => fs.lstatSync(file).isFile())
124
- );
125
- }, []);
137
+ console.log();
138
+ console.log(cyan(`Copying files into ${appPath}`));
126
139
 
127
- // Ensure that the app folder is clean and we won't override any files
128
- folders.forEach(verifyAbsent);
129
- files.forEach(verifyAbsent);
140
+ folders.forEach(folder => {
141
+ fs.mkdirSync(path.join(appPath, folder), { recursive: true });
142
+ });
130
143
 
131
- console.log();
132
- console.log(cyan(`Copying files into ${appPath}`));
144
+ files.forEach(file => {
145
+ let content = fs.readFileSync(file, 'utf8');
133
146
 
134
- folders.forEach(folder => {
135
- fs.mkdirSync(path.join(appPath, folder));
147
+ // Skip flagged files
148
+ if (content.match(/\/\/ @remove-file-on-eject/)) {
149
+ return;
150
+ }
151
+ content =
152
+ content
153
+ // Remove dead code from .js files on eject
154
+ .replace(
155
+ /\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/gm,
156
+ ''
157
+ )
158
+ // Remove dead code from .applescript files on eject
159
+ .replace(
160
+ /-- @remove-on-eject-begin([\s\S]*?)-- @remove-on-eject-end/gm,
161
+ ''
162
+ )
163
+ .trim() + '\n';
164
+ console.log(` Adding ${cyan(file.replace(ownPath, ''))} to the project`);
165
+ fs.writeFileSync(file.replace(ownPath, appPath), content);
166
+ });
167
+ console.log();
168
+
169
+ const ownPackage = require(path.join(ownPath, 'package.json'));
170
+ const appPackage = require(path.join(appPath, 'package.json'));
171
+
172
+ console.log(cyan('Updating the dependencies'));
173
+ const ownPackageName = ownPackage.name;
174
+ if (appPackage.devDependencies) {
175
+ // We used to put react-unity-scripts in devDependencies
176
+ if (appPackage.devDependencies[ownPackageName]) {
177
+ console.log(` Removing ${cyan(ownPackageName)} from devDependencies`);
178
+ delete appPackage.devDependencies[ownPackageName];
179
+ }
180
+ }
181
+ appPackage.dependencies = appPackage.dependencies || {};
182
+ if (appPackage.dependencies[ownPackageName]) {
183
+ console.log(` Removing ${cyan(ownPackageName)} from dependencies`);
184
+ delete appPackage.dependencies[ownPackageName];
185
+ }
186
+ Object.keys(ownPackage.dependencies).forEach(key => {
187
+ // For some reason optionalDependencies end up in dependencies after install
188
+ if (
189
+ ownPackage.optionalDependencies &&
190
+ ownPackage.optionalDependencies[key]
191
+ ) {
192
+ return;
193
+ }
194
+ console.log(` Adding ${cyan(key)} to dependencies`);
195
+ appPackage.dependencies[key] = ownPackage.dependencies[key];
196
+ });
197
+ // Sort the deps
198
+ const unsortedDependencies = appPackage.dependencies;
199
+ appPackage.dependencies = {};
200
+ Object.keys(unsortedDependencies)
201
+ .sort()
202
+ .forEach(key => {
203
+ appPackage.dependencies[key] = unsortedDependencies[key];
136
204
  });
137
-
138
- files.forEach(file => {
139
- let content = fs.readFileSync(file, 'utf8');
140
-
141
- // Skip flagged files
142
- if (content.match(/\/\/ @remove-file-on-eject/)) {
205
+ console.log();
206
+
207
+ console.log(cyan('Updating the scripts'));
208
+ delete appPackage.scripts['eject'];
209
+ Object.keys(appPackage.scripts).forEach(key => {
210
+ Object.keys(ownPackage.bin).forEach(binKey => {
211
+ const regex = new RegExp(binKey + ' (\\w+)', 'g');
212
+ if (!regex.test(appPackage.scripts[key])) {
143
213
  return;
144
214
  }
215
+ appPackage.scripts[key] = appPackage.scripts[key].replace(
216
+ regex,
217
+ 'node scripts/$1.js'
218
+ );
219
+ console.log(
220
+ ` Replacing ${cyan(`"${binKey} ${key}"`)} with ${cyan(
221
+ `"node scripts/${key}.js"`
222
+ )}`
223
+ );
224
+ });
225
+ });
226
+
227
+ console.log();
228
+ console.log(cyan('Configuring package.json'));
229
+ // Add Jest config
230
+ console.log(` Adding ${cyan('Jest')} configuration`);
231
+ appPackage.jest = jestConfig;
232
+
233
+ // Add Babel config
234
+ console.log(` Adding ${cyan('Babel')} preset`);
235
+ appPackage.babel = {
236
+ presets: ['react-app'],
237
+ };
238
+
239
+ // Add ESlint config
240
+ if (!appPackage.eslintConfig) {
241
+ console.log(` Adding ${cyan('ESLint')} configuration`);
242
+ appPackage.eslintConfig = {
243
+ extends: 'react-app',
244
+ };
245
+ }
246
+
247
+ fs.writeFileSync(
248
+ path.join(appPath, 'package.json'),
249
+ JSON.stringify(appPackage, null, 2) + os.EOL
250
+ );
251
+ console.log();
252
+
253
+ if (fs.existsSync(paths.appTypeDeclarations)) {
254
+ try {
255
+ // Read app declarations file
256
+ let content = fs.readFileSync(paths.appTypeDeclarations, 'utf8');
257
+ const ownContent =
258
+ fs.readFileSync(paths.ownTypeDeclarations, 'utf8').trim() + os.EOL;
259
+
260
+ // Remove react-unity-scripts reference since they're getting a copy of the types in their project
145
261
  content =
146
262
  content
147
- // Remove dead code from .js files on eject
148
- .replace(
149
- /\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/gm,
150
- ''
151
- )
152
- // Remove dead code from .applescript files on eject
263
+ // Remove react-unity-scripts types
153
264
  .replace(
154
- /-- @remove-on-eject-begin([\s\S]*?)-- @remove-on-eject-end/gm,
265
+ /^\s*\/\/\/\s*<reference\s+types.+?"react-unity-scripts".*\/>.*(?:\n|$)/gm,
155
266
  ''
156
267
  )
157
- .trim() + '\n';
158
- console.log(` Adding ${cyan(file.replace(ownPath, ''))} to the project`);
159
- fs.writeFileSync(file.replace(ownPath, appPath), content);
160
- });
161
- console.log();
268
+ .trim() + os.EOL;
162
269
 
163
- const ownPackage = require(path.join(ownPath, 'package.json'));
164
- const appPackage = require(path.join(appPath, 'package.json'));
165
-
166
- console.log(cyan('Updating the dependencies'));
167
- const ownPackageName = ownPackage.name;
168
- if (appPackage.devDependencies) {
169
- // We used to put react-unity-scripts in devDependencies
170
- if (appPackage.devDependencies[ownPackageName]) {
171
- console.log(` Removing ${cyan(ownPackageName)} from devDependencies`);
172
- delete appPackage.devDependencies[ownPackageName];
173
- }
174
- }
175
- appPackage.dependencies = appPackage.dependencies || {};
176
- if (appPackage.dependencies[ownPackageName]) {
177
- console.log(` Removing ${cyan(ownPackageName)} from dependencies`);
178
- delete appPackage.dependencies[ownPackageName];
270
+ fs.writeFileSync(
271
+ paths.appTypeDeclarations,
272
+ (ownContent + os.EOL + content).trim() + os.EOL
273
+ );
274
+ } catch (e) {
275
+ // It's not essential that this succeeds, the TypeScript user should
276
+ // be able to re-create these types with ease.
179
277
  }
180
- Object.keys(ownPackage.dependencies).forEach(key => {
181
- // For some reason optionalDependencies end up in dependencies after install
182
- if (
183
- ownPackage.optionalDependencies &&
184
- ownPackage.optionalDependencies[key]
185
- ) {
186
- return;
187
- }
188
- console.log(` Adding ${cyan(key)} to dependencies`);
189
- appPackage.dependencies[key] = ownPackage.dependencies[key];
190
- });
191
- // Sort the deps
192
- const unsortedDependencies = appPackage.dependencies;
193
- appPackage.dependencies = {};
194
- Object.keys(unsortedDependencies)
195
- .sort()
196
- .forEach(key => {
197
- appPackage.dependencies[key] = unsortedDependencies[key];
198
- });
199
- console.log();
278
+ }
200
279
 
201
- console.log(cyan('Updating the scripts'));
202
- delete appPackage.scripts['eject'];
203
- Object.keys(appPackage.scripts).forEach(key => {
280
+ // "Don't destroy what isn't ours"
281
+ if (ownPath.indexOf(appPath) === 0) {
282
+ try {
283
+ // remove react-unity-scripts and react-unity-scripts binaries from app node_modules
204
284
  Object.keys(ownPackage.bin).forEach(binKey => {
205
- const regex = new RegExp(binKey + ' (\\w+)', 'g');
206
- if (!regex.test(appPackage.scripts[key])) {
207
- return;
208
- }
209
- appPackage.scripts[key] = appPackage.scripts[key].replace(
210
- regex,
211
- 'node scripts/$1.js'
212
- );
213
- console.log(
214
- ` Replacing ${cyan(`"${binKey} ${key}"`)} with ${cyan(
215
- `"node scripts/${key}.js"`
216
- )}`
217
- );
285
+ fs.removeSync(path.join(appPath, 'node_modules', '.bin', binKey));
218
286
  });
219
- });
220
-
221
- console.log();
222
- console.log(cyan('Configuring package.json'));
223
-
224
- // Add Babel config
225
- console.log(` Adding ${cyan('Babel')} preset`);
226
- appPackage.babel = {
227
- presets: ['react-app'],
228
- };
229
-
230
- // Add ESlint config
231
- if (!appPackage.eslintConfig) {
232
- console.log(` Adding ${cyan('ESLint')} configuration`);
233
- appPackage.eslintConfig = {
234
- extends: 'react-app',
235
- };
287
+ fs.removeSync(ownPath);
288
+ } catch (e) {
289
+ // It's not essential that this succeeds
236
290
  }
291
+ }
237
292
 
238
- fs.writeFileSync(
239
- path.join(appPath, 'package.json'),
240
- JSON.stringify(appPackage, null, 2) + os.EOL
293
+ if (fs.existsSync(paths.yarnLockFile)) {
294
+ const windowsCmdFilePath = path.join(
295
+ appPath,
296
+ 'node_modules',
297
+ '.bin',
298
+ 'react-unity-scripts.cmd'
241
299
  );
242
- console.log();
243
-
244
- if (fs.existsSync(paths.appTypeDeclarations)) {
245
- try {
246
- // Read app declarations file
247
- let content = fs.readFileSync(paths.appTypeDeclarations, 'utf8');
248
- const ownContent =
249
- fs.readFileSync(paths.ownTypeDeclarations, 'utf8').trim() + os.EOL;
250
-
251
- // Remove react-unity-scripts reference since they're getting a copy of the types in their project
252
- content =
253
- content
254
- // Remove react-unity-scripts types
255
- .replace(
256
- /^\s*\/\/\/\s*<reference\s+types.+?"react-unity-scripts".*\/>.*(?:\n|$)/gm,
257
- ''
258
- )
259
- .trim() + os.EOL;
260
-
261
- fs.writeFileSync(
262
- paths.appTypeDeclarations,
263
- (ownContent + os.EOL + content).trim() + os.EOL
264
- );
265
- } catch (e) {
266
- // It's not essential that this succeeds, the TypeScript user should
267
- // be able to re-create these types with ease.
268
- }
269
- }
270
-
271
- // "Don't destroy what isn't ours"
272
- if (ownPath.indexOf(appPath) === 0) {
300
+ let windowsCmdFileContent;
301
+ if (process.platform === 'win32') {
302
+ // https://github.com/facebook/create-react-app/pull/3806#issuecomment-357781035
303
+ // Yarn is diligent about cleaning up after itself, but this causes the react-unity-scripts.cmd file
304
+ // to be deleted while it is running. This trips Windows up after the eject completes.
305
+ // We'll read the batch file and later "write it back" to match npm behavior.
273
306
  try {
274
- // remove react-unity-scripts and react-unity-scripts binaries from app node_modules
275
- Object.keys(ownPackage.bin).forEach(binKey => {
276
- fs.removeSync(path.join(appPath, 'node_modules', '.bin', binKey));
277
- });
278
- fs.removeSync(ownPath);
279
- } catch (e) {
280
- // It's not essential that this succeeds
307
+ windowsCmdFileContent = fs.readFileSync(windowsCmdFilePath);
308
+ } catch (err) {
309
+ // If this fails we're not worse off than if we didn't try to fix it.
281
310
  }
282
311
  }
283
312
 
284
- if (fs.existsSync(paths.yarnLockFile)) {
285
- const windowsCmdFilePath = path.join(
286
- appPath,
287
- 'node_modules',
288
- '.bin',
289
- 'react-unity-scripts.cmd'
290
- );
291
- let windowsCmdFileContent;
292
- if (process.platform === 'win32') {
293
- // https://github.com/facebook/create-react-app/pull/3806#issuecomment-357781035
294
- // Yarn is diligent about cleaning up after itself, but this causes the react-unity-scripts.cmd file
295
- // to be deleted while it is running. This trips Windows up after the eject completes.
296
- // We'll read the batch file and later "write it back" to match npm behavior.
297
- try {
298
- windowsCmdFileContent = fs.readFileSync(windowsCmdFilePath);
299
- } catch (err) {
300
- // If this fails we're not worse off than if we didn't try to fix it.
301
- }
302
- }
303
-
304
- console.log(cyan('Running yarn...'));
305
- spawnSync('yarnpkg', ['--cwd', process.cwd()], { stdio: 'inherit' });
313
+ console.log(cyan('Running yarn...'));
314
+ spawnSync('yarnpkg', ['--cwd', process.cwd()], { stdio: 'inherit' });
306
315
 
307
- if (windowsCmdFileContent && !fs.existsSync(windowsCmdFilePath)) {
308
- try {
309
- fs.writeFileSync(windowsCmdFilePath, windowsCmdFileContent);
310
- } catch (err) {
311
- // If this fails we're not worse off than if we didn't try to fix it.
312
- }
316
+ if (windowsCmdFileContent && !fs.existsSync(windowsCmdFilePath)) {
317
+ try {
318
+ fs.writeFileSync(windowsCmdFilePath, windowsCmdFileContent);
319
+ } catch (err) {
320
+ // If this fails we're not worse off than if we didn't try to fix it.
313
321
  }
314
- } else {
315
- console.log(cyan('Running npm install...'));
316
- spawnSync('npm', ['install', '--loglevel', 'error'], {
317
- stdio: 'inherit',
318
- });
319
- }
320
- console.log(green('Ejected successfully!'));
321
- console.log();
322
-
323
- if (tryGitAdd(appPath)) {
324
- console.log(cyan('Staged ejected files for commit.'));
325
- console.log();
326
322
  }
323
+ } else {
324
+ console.log(cyan('Running npm install...'));
325
+ spawnSync('npm', ['install', '--loglevel', 'error'], {
326
+ stdio: 'inherit',
327
+ });
328
+ }
329
+ console.log(green('Ejected successfully!'));
330
+ console.log();
327
331
 
328
- console.log(
329
- green('Please consider sharing why you ejected in this survey:')
330
- );
331
- console.log(green(' http://goo.gl/forms/Bi6CZjk1EqsdelXk1'));
332
+ if (tryGitAdd(appPath)) {
333
+ console.log(cyan('Staged ejected files for commit.'));
332
334
  console.log();
333
- });
335
+ }
336
+ });
package/scripts/init.js CHANGED
@@ -126,24 +126,17 @@ module.exports = function (
126
126
 
127
127
  const templatePackage = templateJson.package || {};
128
128
 
129
- // TODO: Deprecate support for root-level `dependencies` and `scripts` in v5.
130
- // These should now be set under the `package` key.
129
+ // This was deprecated in CRA v5.
131
130
  if (templateJson.dependencies || templateJson.scripts) {
132
131
  console.log();
133
132
  console.log(
134
- chalk.yellow(
135
- 'Root-level `dependencies` and `scripts` keys in `template.json` are deprecated.\n' +
136
- 'This template should be updated to use the new `package` key.'
133
+ chalk.red(
134
+ 'Root-level `dependencies` and `scripts` keys in `template.json` were deprecated for Create React App 5.\n' +
135
+ 'This template needs to be updated to use the new `package` key.'
137
136
  )
138
137
  );
139
138
  console.log('For more information, visit https://cra.link/templates');
140
139
  }
141
- if (templateJson.dependencies) {
142
- templatePackage.dependencies = templateJson.dependencies;
143
- }
144
- if (templateJson.scripts) {
145
- templatePackage.scripts = templateJson.scripts;
146
- }
147
140
 
148
141
  // Keys to ignore in templatePackage
149
142
  const templatePackageBlacklist = [
@@ -298,7 +291,12 @@ module.exports = function (
298
291
  } else {
299
292
  command = 'npm';
300
293
  remove = 'uninstall';
301
- args = ['install', '--save', verbose && '--verbose'].filter(e => e);
294
+ args = [
295
+ 'install',
296
+ '--no-audit', // https://github.com/facebook/create-react-app/issues/11174
297
+ '--save',
298
+ verbose && '--verbose',
299
+ ].filter(e => e);
302
300
  }
303
301
 
304
302
  // Install additional template dependencies, if present.
package/scripts/lint.js CHANGED
@@ -9,7 +9,8 @@
9
9
  'use strict';
10
10
 
11
11
  // Do this as the first thing so that any code reading it knows the right env.
12
- process.env.BABEL_ENV = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
12
+ process.env.BABEL_ENV =
13
+ process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
13
14
  process.env.NODE_ENV = process.env.NODE_ENV || 'development';
14
15
 
15
16
  // Makes the script crash on unhandled rejections instead of silently
@@ -26,14 +27,14 @@ const paths = require('../config/paths');
26
27
  const eslintConfig = require('../config/eslintConfig');
27
28
  const formatter = require('react-dev-utils/eslintFormatter');
28
29
 
29
- const { ESLint } = require("eslint");
30
+ const { ESLint } = require('eslint');
30
31
 
31
32
  (async function main() {
32
33
  const eslint = new ESLint(eslintConfig);
33
34
  const results = await eslint.lintFiles([paths.appSrc]);
34
35
  const resultText = formatter(results);
35
36
  console.log(resultText);
36
- })().catch((error) => {
37
+ })().catch(error => {
37
38
  process.exitCode = 1;
38
39
  console.error(error);
39
40
  });