Skip to content

Commit 9e99138

Browse files
committed
Fix package.json not getting updated correctly
1 parent df40e90 commit 9e99138

File tree

4 files changed

+57
-46
lines changed

4 files changed

+57
-46
lines changed

packages/react-scripts/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"name": "@devloco/react-scripts-wptheme",
3-
"version": "3.3.0-wp.3",
3+
"version": "3.3.0-wp.11",
44
"description": "Configuration and scripts for Create React WP Theme.",
5-
"repository": "devloco/create-react-wptheme",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/devloco/create-react-wptheme-scripts.git",
8+
"directory": "packages/react-scripts"
9+
},
610
"author": "devloco",
711
"license": "MIT",
812
"keywords": [
@@ -30,10 +34,7 @@
3034
"bin",
3135
"config",
3236
"lib",
33-
"scripts",
34-
"template",
35-
"template-typescript",
36-
"utils"
37+
"scripts"
3738
],
3839
"bin": {
3940
"react-scripts": "./bin/react-scripts.js",

packages/react-scripts/scripts/init-wptheme.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,20 @@ process.on('unhandledRejection', err => {
1515
});
1616

1717
const fs = require('fs-extra');
18-
const os = require('os');
1918
const path = require('path');
2019
const chalk = require('chalk');
2120
const shelljs = require('@devloco/create-react-wptheme-utils/shell-js');
22-
const spawn = require('react-dev-utils/crossSpawn');
2321

24-
module.exports = function(
22+
const fnUpdateAppPackage = function(
23+
appPackage,
2524
appPath,
2625
appName,
2726
verbose,
2827
originalDirectory,
2928
templateName,
3029
readmeExists,
31-
useTypeScript,
3230
useYarn
3331
) {
34-
const appPackage = require(path.join(appPath, 'package.json'));
35-
const reactSrcName = 'react-src';
36-
3732
let aOriginalDirectory = originalDirectory.replace(/\\/g, '/').split('/');
3833
let originalThemeName = aOriginalDirectory[aOriginalDirectory.length - 1];
3934

@@ -62,11 +57,22 @@ module.exports = function(
6257
// Set the app name correctly
6358
appPackage.name = originalThemeName;
6459

65-
// rewrite package.json
66-
fs.writeFileSync(
67-
path.join(appPath, 'package.json'),
68-
JSON.stringify(appPackage, null, 2) + os.EOL
69-
);
60+
return appPackage;
61+
};
62+
63+
const fnFinish = function(
64+
appPackage,
65+
appPath,
66+
appName,
67+
verbose,
68+
originalDirectory,
69+
templateName,
70+
readmeExists,
71+
useYarn
72+
) {
73+
const reactSrcName = 'react-src';
74+
let aOriginalDirectory = originalDirectory.replace(/\\/g, '/').split('/');
75+
let originalThemeName = aOriginalDirectory[aOriginalDirectory.length - 1];
7076

7177
// Update the theme's style.css file with the Theme Name
7278
let styleCssFile = path.join(appPath, '/public/style.css');
@@ -77,22 +83,6 @@ module.exports = function(
7783
);
7884
fs.writeFileSync(styleCssFile, result, 'utf8', 'w');
7985

80-
// To use TypeScript, we need the types from Facebook's react-scripts.
81-
if (useTypeScript === true) {
82-
let command = useYarn ? 'yarn add' : 'npm install';
83-
let args = [verbose && '--verbose'].filter(e => e);
84-
args.push('react-scripts');
85-
86-
console.log(`Installing react-scripts using: ${command}...`);
87-
console.log();
88-
89-
const proc = spawn.sync(command, args, { stdio: 'inherit' });
90-
if (proc.status !== 0) {
91-
console.error(`\`${command} ${args.join(' ')}\` failed`);
92-
return;
93-
}
94-
}
95-
9686
// if we're not on Windows, then try to chmod the folder so that the PHP portion of
9787
// the setup can complete (the webserver needs write access to package.json)
9888
if (process.platform !== 'win32') {
@@ -139,6 +129,8 @@ module.exports = function(
139129

140130
// Change displayed command to yarn instead of yarnPkg
141131
const displayedCommand = useYarn ? 'yarn' : 'npm run';
132+
const buildCommandName = 'build';
133+
const startCommandName = 'start';
142134

143135
console.log();
144136
console.log(`Success! Created ${originalThemeName} at ${appPath}`);
@@ -171,3 +163,8 @@ module.exports = function(
171163
console.log();
172164
console.log('Happy hacking!');
173165
};
166+
167+
module.exports = {
168+
fnFinish,
169+
fnUpdateAppPackage,
170+
};

packages/react-scripts/scripts/init.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const os = require('os');
2424
const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
2525
const initWpTheme = require('./init-wptheme');
2626

27+
let _readmeExists = false;
28+
2729
function isInGitRepository() {
2830
try {
2931
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
@@ -83,7 +85,7 @@ module.exports = function(
8385
originalDirectory,
8486
templateName
8587
) {
86-
const appPackage = require(path.join(appPath, 'package.json'));
88+
let appPackage = require(path.join(appPath, 'package.json'));
8789
const useYarn = fs.existsSync(path.join(appPath, 'yarn.lock'));
8890

8991
if (!templateName) {
@@ -153,13 +155,25 @@ module.exports = function(
153155
// Setup the browsers list
154156
appPackage.browserslist = defaultBrowsers;
155157

158+
_readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
159+
160+
appPackage = initWpTheme.fnUpdateAppPackage(
161+
appPackage,
162+
appPath,
163+
appName,
164+
verbose,
165+
originalDirectory,
166+
templateName,
167+
_readmeExists,
168+
useYarn
169+
);
170+
156171
fs.writeFileSync(
157172
path.join(appPath, 'package.json'),
158173
JSON.stringify(appPackage, null, 2) + os.EOL
159174
);
160175

161-
const readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
162-
if (readmeExists) {
176+
if (_readmeExists) {
163177
fs.renameSync(
164178
path.join(appPath, 'README.md'),
165179
path.join(appPath, 'README.old.md')
@@ -252,11 +266,9 @@ module.exports = function(
252266
}
253267
}
254268

255-
let useTypeScript = false;
256269
if (args.find(arg => arg.includes('typescript'))) {
257270
console.log();
258271
verifyTypeScriptSetup();
259-
useTypeScript = true;
260272
}
261273

262274
// Remove template
@@ -277,16 +289,17 @@ module.exports = function(
277289
}
278290

279291
// wptheme intercept -- stop the standard create-react-app init here... wptheme will take over and display the final message.
280-
return initWpTheme(
292+
initWpTheme.fnFinish(
293+
appPackage,
281294
appPath,
282295
appName,
283296
verbose,
284297
originalDirectory,
285298
templateName,
286-
readmeExists,
287-
useTypeScript,
299+
_readmeExists,
288300
useYarn
289301
);
302+
return;
290303

291304
// Display the most elegant way to cd.
292305
// This needs to handle an undefined originalDirectory for
@@ -330,7 +343,7 @@ module.exports = function(
330343
console.log();
331344
console.log(chalk.cyan(' cd'), cdpath);
332345
console.log(` ${chalk.cyan(`${displayedCommand} start`)}`);
333-
if (readmeExists) {
346+
if (_readmeExists) {
334347
console.log();
335348
console.log(
336349
chalk.yellow(

packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const chalk = require('react-dev-utils/chalk');
1212
const fs = require('fs');
1313
const resolve = require('resolve');
1414
const path = require('path');
15-
const paths = require('../../config/paths');
15+
const paths = require('../../config/paths-wptheme');
1616
const os = require('os');
1717
const immer = require('react-dev-utils/immer').produce;
1818
const globby = require('react-dev-utils/globby').sync;
@@ -252,11 +252,11 @@ function verifyTypeScriptSetup() {
252252
writeJson(paths.appTsConfig, appTsConfig);
253253
}
254254

255-
// Reference `react-scripts` types
255+
// Reference `@devloco/react-scripts-wptheme` types
256256
if (!fs.existsSync(paths.appTypeDeclarations)) {
257257
fs.writeFileSync(
258258
paths.appTypeDeclarations,
259-
`/// <reference types="react-scripts" />${os.EOL}`
259+
`/// <reference types="@devloco/react-scripts-wptheme" />${os.EOL}`
260260
);
261261
}
262262
}

0 commit comments

Comments
 (0)