Skip to content

Commit 063b609

Browse files
authored
Merge pull request #43 from fs-webdev/runFrEnvAutomatically
minor fixes, and auto setup of .env file for hf template
2 parents 39ded8e + ba7b1d0 commit 063b609

File tree

3 files changed

+44
-96
lines changed

3 files changed

+44
-96
lines changed

packages/react-scripts/scripts/init.js

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,9 @@ function tryGitInit(appPath) {
7676
}
7777
}
7878

79-
module.exports = async function(
80-
appPath,
81-
appName,
82-
verbose,
83-
originalDirectory,
84-
template
85-
) {
79+
module.exports = async function(appPath, appName, verbose, originalDirectory, template) {
8680
const answers = await frontierInit.promptForConfig(appPath);
87-
const ownPath = path.dirname(
88-
require.resolve(path.join(__dirname, '..', 'package.json'))
89-
);
81+
const ownPath = path.dirname(require.resolve(path.join(__dirname, '..', 'package.json')));
9082
const appPackage = require(path.join(appPath, 'package.json'));
9183
const useYarn = fs.existsSync(path.join(appPath, 'yarn.lock'));
9284

@@ -120,10 +112,7 @@ module.exports = async function(
120112

121113
const readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
122114
if (readmeExists) {
123-
fs.renameSync(
124-
path.join(appPath, 'README.md'),
125-
path.join(appPath, 'README.old.md')
126-
);
115+
fs.renameSync(path.join(appPath, 'README.md'), path.join(appPath, 'README.old.md'));
127116
}
128117

129118
// Copy the files for the user
@@ -133,20 +122,14 @@ module.exports = async function(
133122
if (fs.existsSync(templatePath)) {
134123
fs.copySync(templatePath, appPath);
135124
} else {
136-
console.error(
137-
`Could not locate supplied template: ${chalk.green(templatePath)}`
138-
);
125+
console.error(`Could not locate supplied template: ${chalk.green(templatePath)}`);
139126
return;
140127
}
141128

142129
// Rename gitignore after the fact to prevent npm from renaming it to .npmignore
143130
// See: https://github.com/npm/npm/issues/1862
144131
try {
145-
fs.moveSync(
146-
path.join(appPath, 'gitignore'),
147-
path.join(appPath, '.gitignore'),
148-
[]
149-
);
132+
fs.moveSync(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), []);
150133
} catch (err) {
151134
// Append if there's already a `.gitignore` file there
152135
if (err.code === 'EEXIST') {
@@ -171,10 +154,7 @@ module.exports = async function(
171154
args.push('react', 'react-dom');
172155

173156
// Install additional template dependencies, if present
174-
const templateDependenciesPath = path.join(
175-
appPath,
176-
'.template.dependencies.json'
177-
);
157+
const templateDependenciesPath = path.join(appPath, '.template.dependencies.json');
178158
if (fs.existsSync(templateDependenciesPath)) {
179159
const templateDependencies = require(templateDependenciesPath).dependencies;
180160
args = args.concat(
@@ -199,7 +179,7 @@ module.exports = async function(
199179
}
200180
}
201181

202-
frontierInit.installFrontierDependencies(appPath, answers, useYarn, ownPath);
182+
frontierInit.installFrontierDependencies(appPath, answers, ownPath);
203183

204184
if (useTypeScript) {
205185
verifyTypeScriptSetup();
@@ -223,44 +203,30 @@ module.exports = async function(
223203
// Change displayed command to yarn instead of yarnpkg
224204
const displayedCommand = useYarn ? 'yarn' : 'npm';
225205

226-
frontierInit.cleanupFrontierCode(appPath);
227-
228206
console.log();
229207
console.log(`Success! Created ${appName} at ${appPath}`);
230208
console.log('Inside that directory, you can run several commands:');
231209
console.log();
232210
console.log(chalk.cyan(` ${displayedCommand} start`));
233211
console.log(' Starts the development server.');
234212
console.log();
235-
console.log(
236-
chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build`)
237-
);
213+
console.log(chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build`));
238214
console.log(' Bundles the app into static files for production.');
239215
console.log();
240216
console.log(chalk.cyan(` ${displayedCommand} test`));
241217
console.log(' Starts the test runner.');
242218
console.log();
243-
console.log(
244-
chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}eject`)
245-
);
246-
console.log(
247-
' Removes this tool and copies build dependencies, configuration files'
248-
);
249-
console.log(
250-
' and scripts into the app directory. If you do this, you can’t go back!'
251-
);
219+
console.log(chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}eject`));
220+
console.log(' Removes this tool and copies build dependencies, configuration files');
221+
console.log(' and scripts into the app directory. If you do this, you can’t go back!');
252222
console.log();
253223
console.log('We suggest that you begin by typing:');
254224
console.log();
255225
console.log(chalk.cyan(' cd'), cdpath);
256226
console.log(` ${chalk.cyan(`${displayedCommand} start`)}`);
257227
if (readmeExists) {
258228
console.log();
259-
console.log(
260-
chalk.yellow(
261-
'You had a `README.md` file, we renamed it to `README.old.md`'
262-
)
263-
);
229+
console.log(chalk.yellow('You had a `README.md` file, we renamed it to `README.old.md`'));
264230
}
265231
console.log();
266232
console.log('Happy hacking!');
@@ -270,7 +236,6 @@ function isReactInstalled(appPackage) {
270236
const dependencies = appPackage.dependencies || {};
271237

272238
return (
273-
typeof dependencies.react !== 'undefined' &&
274-
typeof dependencies['react-dom'] !== 'undefined'
239+
typeof dependencies.react !== 'undefined' && typeof dependencies['react-dom'] !== 'undefined'
275240
);
276241
}

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

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = {
1111
installFrontierDependencies,
1212
promptForConfig,
1313
packageJsonWritten,
14-
cleanupFrontierCode,
1514
};
1615

1716
async function promptForConfig() {
@@ -44,19 +43,18 @@ async function promptForConfig() {
4443

4544
function packageJsonWritten() {}
4645

47-
function installFrontierDependencies(appPath, answers, useYarn, ownPath) {
46+
function installFrontierDependencies(appPath, answers, ownPath) {
4847
const { additionalFeatures } = answers;
4948

5049
if (additionalFeatures.includes('polymer')) {
51-
configurePolymer(appPath, useYarn);
50+
configurePolymer(appPath);
5251
}
5352
if (additionalFeatures.includes('electric-flow')) {
54-
configureEF(appPath, useYarn, ownPath);
53+
configureEF(appPath, ownPath);
5554
}
5655
if (additionalFeatures.includes('header-footer')) {
57-
configureHF(appPath, useYarn, ownPath);
56+
configureHF(appPath, ownPath);
5857
}
59-
injectPolymerCode(appPath);
6058

6159
const defaultModules = ['http-proxy-middleware@0.19.0', 'fs-webdev/exo'];
6260

@@ -67,8 +65,8 @@ function installFrontierDependencies(appPath, answers, useYarn, ownPath) {
6765
'webpack@4.19.1',
6866
];
6967

70-
installModulesSync(defaultModules, useYarn);
71-
installModulesSync(defaultDevModules, useYarn, true);
68+
installModulesSync(defaultModules);
69+
installModulesSync(defaultDevModules, true);
7270

7371
alterPackageJsonFile(appPath, appPackage => {
7472
const packageJson = { ...appPackage };
@@ -81,7 +79,7 @@ function installFrontierDependencies(appPath, answers, useYarn, ownPath) {
8179
};
8280
packageJson.scripts = { ...packageJson.scripts, ...additionalScripts };
8381
packageJson.eslintConfig = {
84-
extends: ['frontier/recommended'],
82+
extends: ['@fs/eslint-config-frontier-react'],
8583
};
8684
return packageJson;
8785
});
@@ -95,7 +93,7 @@ function alterPackageJsonFile(appPath, extendFunction) {
9593
);
9694
}
9795

98-
function configurePolymer(appPath, useYarn) {
96+
function configurePolymer(appPath) {
9997
alterPackageJsonFile(appPath, appPackage => {
10098
const packageJson = { ...appPackage };
10199
packageJson.vendorCopy = [
@@ -112,8 +110,9 @@ function configurePolymer(appPath, useYarn) {
112110
return packageJson;
113111
});
114112

113+
injectPolymerCode(appPath);
115114
const polymerModules = ['vendor-copy@2.0.0', '@webcomponents/webcomponentsjs@2.1.3'];
116-
installModulesSync(polymerModules, useYarn, true);
115+
installModulesSync(polymerModules, true);
117116
}
118117

119118
function injectPolymerCode(appPath) {
@@ -129,7 +128,7 @@ function injectPolymerCode(appPath) {
129128
fs.writeFileSync(indexPath, indexHtml);
130129
}
131130

132-
function configureEF(appPath, useYarn, ownPath) {
131+
function configureEF(appPath, ownPath) {
133132
// TODO - modify package.json to make sure name is correct for blueprint
134133
// TODO - use blueprint.yml as a template
135134

@@ -139,68 +138,54 @@ function configureEF(appPath, useYarn, ownPath) {
139138
alterPackageJsonFile(appPath, appPackage => {
140139
const packageJson = { ...appPackage };
141140
const additionalScripts = {
142-
"heroku-prebuild": "./heroku-prebuild.sh"
141+
'heroku-prebuild': './heroku-prebuild.sh',
143142
};
144143
packageJson.scripts = sortScripts({ ...packageJson.scripts, ...additionalScripts });
145144
return packageJson;
146145
});
147146
}
148147

149-
function configureHF(appPath, useYarn, ownPath) {
150-
148+
function configureHF(appPath, ownPath) {
151149
const templatePath = path.join(ownPath, 'template-hf');
152150
fs.copySync(templatePath, appPath, { overwrite: true });
153151

154152
alterPackageJsonFile(appPath, appPackage => {
155153
const packageJson = { ...appPackage };
156154
const additionalScripts = {
157-
"build:prod": "PUBLIC_URL=https://edge.fscdn.org/assets/ react-scripts build",
158-
"heroku-postbuild": "npm run build:prod"
155+
'build:prod': 'PUBLIC_URL=https://edge.fscdn.org/assets/ react-scripts build',
156+
'heroku-postbuild': 'npm run build:prod',
159157
};
160158
packageJson.scripts = sortScripts({ ...packageJson.scripts, ...additionalScripts });
161-
packageJson.main = "./server.js";
159+
packageJson.main = './server.js';
162160

163161
return packageJson;
164162
});
165163

164+
createLocalEnvFile();
166165
let modules = [
167166
'github:fs-webdev/hf#cra',
168167
'github:fs-webdev/snow#cra',
169168
'github:fs-webdev/startup',
170-
]
171-
installModulesSync(modules, useYarn);
169+
];
170+
installModulesSync(modules);
172171
}
173172

174-
function cleanupFrontierCode(appPath) {}
175-
176-
function installModulesSync(modules, useYarn, saveDev = false) {
177-
const { command, args } = buildInstallCommandAndArgs(useYarn, saveDev);
178-
osUtils.runExternalCommandSync(command, args.concat(modules));
173+
function installModulesSync(modules, saveDev = false) {
174+
const command = 'npm';
175+
const args = ['install', `--save${saveDev ? '-dev' : ''}`].concat(modules);
176+
osUtils.runExternalCommandSync(command, args);
179177
}
180178

181-
function buildInstallCommandAndArgs(useYarn, saveDev = false) {
182-
let command;
183-
let args;
184-
if (useYarn) {
185-
command = 'yarnpkg';
186-
args = ['add'];
187-
if (saveDev) {
188-
args.push('--dev');
189-
}
190-
} else {
191-
command = 'npm';
192-
args = ['install', '--save'];
193-
if (saveDev) {
194-
args[1] = '--save-dev';
195-
}
196-
}
197-
return { command, args };
179+
function createLocalEnvFile() {
180+
osUtils.runExternalCommandSync('npx', ['@fs/fr-cli', 'env', 'local']);
198181
}
199182

200-
function sortScripts(scripts){
183+
function sortScripts(scripts) {
201184
const sortedScripts = {};
202-
Object.keys(scripts).sort().forEach(function(key) {
203-
sortedScripts[key] = scripts[key];
204-
});
185+
Object.keys(scripts)
186+
.sort()
187+
.forEach(function(key) {
188+
sortedScripts[key] = scripts[key];
189+
});
205190
return sortedScripts;
206191
}

packages/react-scripts/template/.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)