Skip to content

Commit 5a365f5

Browse files
committed
Ensure there is no race condition between manifest build and nodemon watch of src/manifest.js during local gulp task.
1 parent 5ef153b commit 5a365f5

File tree

3 files changed

+74
-43
lines changed

3 files changed

+74
-43
lines changed

.core/gulp.bootup.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,62 @@ const rootPath = path.resolve(__dirname, '..');
77
const config = require('./gulp.config');
88
const webpackConfig = require('./webpack.config')(config);
99
const chalk = require('chalk');
10+
const op = require('object-path');
11+
const moment = require('moment');
12+
13+
global.LOG_LEVELS = {
14+
DEBUG: 1000,
15+
INFO: 500,
16+
BOOT: 0,
17+
WARN: -500,
18+
ERROR: -1000,
19+
};
20+
21+
global.LOG_LEVEL = op.get(
22+
LOG_LEVELS,
23+
op.get(process.env, 'LOG_LEVEL', 'BOOT'),
24+
LOG_LEVELS.BOOT,
25+
);
26+
27+
const APP_NAME = op.get(process.env, 'APP_NAME', 'Reactium');
28+
const LOG_THRESHOLD = op.get(LOG_LEVELS, [LOG_LEVEL], LOG_LEVELS.BOOT);
29+
30+
const reactiumConsole = global.console;
31+
for (const [LEVEL, THRESHOLD] of Object.entries(LOG_LEVELS)) {
32+
global[LEVEL] = (...args) => {
33+
if (process.env.NO_LOGGING === 'true' || THRESHOLD > LOG_THRESHOLD) {
34+
return;
35+
}
36+
37+
const _W = THRESHOLD <= LOG_LEVELS.WARN;
38+
const _E = THRESHOLD <= LOG_LEVELS.ERROR;
39+
let color = _W ? chalk.yellow.bold : chalk.cyan;
40+
color = _E ? chalk.red.bold : color;
41+
42+
const time = `[${chalk.magenta(moment().format('HH:mm:ss'))}]`;
43+
let name = `${color(String(APP_NAME))}`;
44+
name = _E ? `%${name}%` : _W ? `!${name}!` : `[${name}]`;
45+
46+
let logMethod = op.get(reactiumConsole, LEVEL, reactiumConsole.log);
47+
logMethod =
48+
typeof logMethod === 'function' ? logMethod : reactiumConsole.log;
49+
const [first, ...remaining] = args;
50+
51+
if (typeof first === 'string') {
52+
logMethod(`${time} ${name} ${first}`, ...remaining);
53+
} else {
54+
logMethod(time, name, ...args);
55+
}
56+
};
57+
}
58+
global.console = {
59+
log: global.BOOT,
60+
warn: global.WARN,
61+
error: global.ERROR,
62+
info: global.BOOT,
63+
};
64+
65+
global.LOG = global.BOOT;
1066

1167
global.ReactiumGulp = ReactiumGulp;
1268

.core/gulp.tasks.js

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ const reactiumConfig = require('./reactium-config');
2424
const regenManifest = require('./manifest/manifest-tools');
2525
const umdWebpackGenerator = require('./umd.webpack.config');
2626
const rootPath = path.resolve(__dirname, '..');
27-
const { fork, spawn } = require('child_process');
27+
const { fork, spawn, execSync } = require('child_process');
2828
const workbox = require('workbox-build');
2929
const { File, FileReader } = require('file-api');
3030
const handlebars = require('handlebars');
31-
const timestamp = () => {
32-
const now = new Date();
33-
const ts = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
34-
return `[${chalk.blue(ts)}]`;
35-
};
31+
const { resolve } = require('path');
3632

3733
// For backward compatibility with gulp override tasks using run-sequence module
3834
// make compatible with gulp4
@@ -105,12 +101,10 @@ const reactium = (gulp, config, webpackConfig) => {
105101

106102
fs.createReadStream(e.path)
107103
.pipe(fs.createWriteStream(fpath))
108-
.on('error', error => console.error(timestamp(), error));
104+
.on('error', error => console.error(error));
109105
}
110106

111-
console.log(
112-
`${timestamp()} File ${e.event}: ${displaySrc} -> ${displayDest}`,
113-
);
107+
console.log(`File ${e.event}: ${displaySrc} -> ${displayDest}`);
114108
};
115109

116110
const serve = ({ open } = { open: config.open }) => done => {
@@ -120,8 +114,7 @@ const reactium = (gulp, config, webpackConfig) => {
120114
.then(() => {
121115
browserSync({
122116
notify: false,
123-
timestamps: true,
124-
logPrefix: '00:00:00',
117+
timestamps: false,
125118
port: config.port.browsersync,
126119
ui: { port: config.port.browsersync + 1 },
127120
proxy,
@@ -144,12 +137,12 @@ const reactium = (gulp, config, webpackConfig) => {
144137
watchProcess.on('message', message => {
145138
switch (message) {
146139
case 'build-started': {
147-
console.log(timestamp() + " Starting 'build'...");
140+
console.log("Starting 'build'...");
148141
done();
149142
return;
150143
}
151144
case 'restart-watches': {
152-
console.log(timestamp() + " Restarting 'watch'...");
145+
console.log("Restarting 'watch'...");
153146
watchProcess.kill();
154147
watch(_ => _, true);
155148
return;
@@ -166,12 +159,14 @@ const reactium = (gulp, config, webpackConfig) => {
166159
) => {
167160
const ps = spawn(cmd, args, { stdio: [stdin, stdout, stderr] });
168161
ps.on('close', code => {
169-
if (code !== 0) console.log(timestamp() + `Error executing ${cmd}`);
162+
if (code !== 0) console.log(`Error executing ${cmd}`);
170163
done();
171164
});
165+
166+
return ps;
172167
};
173168

174-
const local = ({ ssr = false } = {}) => done => {
169+
const local = ({ ssr = false } = {}) => async done => {
175170
const SSR_MODE = ssr ? 'on' : 'off';
176171
const crossEnvModulePath = path.resolve(
177172
path.dirname(require.resolve('cross-env')),
@@ -186,17 +181,7 @@ const reactium = (gulp, config, webpackConfig) => {
186181
crossEnvPackage.bin['cross-env'],
187182
);
188183

189-
// warnings here
190-
// TODO: convert to useHookComponent
191-
if (!fs.existsSync(rootPath, 'src/app/components/Fallback/index.js')) {
192-
console.log('');
193-
console.log(
194-
chalk.magenta(
195-
'Create a `src/app/components/Fallback` component with default export to support lazy loaded components and remove the webpack warning you see below.',
196-
),
197-
);
198-
console.log('');
199-
}
184+
await gulp.task('mainManifest')(() => Promise.resolve());
200185

201186
command(
202187
'node',
@@ -318,6 +303,7 @@ const reactium = (gulp, config, webpackConfig) => {
318303
),
319304
manifestProcessor: require('./manifest/processors/manifest'),
320305
});
306+
321307
done();
322308
};
323309

@@ -395,9 +381,7 @@ const reactium = (gulp, config, webpackConfig) => {
395381

396382
for (let umd of umdConfigs) {
397383
try {
398-
console.log(
399-
timestamp() + ` Generating UMD library ${umd.libraryName}`,
400-
);
384+
console.log(`Generating UMD library ${umd.libraryName}`);
401385
await new Promise((resolve, reject) => {
402386
webpack(umdWebpackGenerator(umd), (err, stats) => {
403387
if (err) {
@@ -433,7 +417,7 @@ const reactium = (gulp, config, webpackConfig) => {
433417
};
434418

435419
if (!fs.existsSync(config.umd.defaultWorker)) {
436-
console.info(timestamp() + ' Skipping service worker generation.');
420+
console.log('Skipping service worker generation.');
437421
return Promise.resolve();
438422
}
439423

@@ -448,15 +432,10 @@ const reactium = (gulp, config, webpackConfig) => {
448432
for (const warning of warnings) {
449433
console.warn(warning);
450434
}
451-
console.info(
452-
timestamp() + ' Service worker generation completed.',
453-
);
435+
console.log('Service worker generation completed.');
454436
})
455437
.catch(error => {
456-
console.warn(
457-
timestamp() + ' Service worker generation failed:',
458-
error,
459-
);
438+
console.warn('Service worker generation failed:', error);
460439
});
461440
};
462441

.core/manifest/manifest-tools.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,7 @@ module.exports = function({
214214

215215
// Write Manifest only if it does not exist or has changed
216216
if (!fs.existsSync(manifestFilePath) || manifestHasChanged()) {
217-
console.log(
218-
`[${moment().format('HH:mm:ss')}] Writing '${chalk.cyan(
219-
manifestFilePath,
220-
)}'...`,
221-
);
217+
console.log(`'${chalk.cyan(manifestFilePath)}'...`);
222218
const dir = path.dirname(manifestFilePath);
223219
fs.ensureDirSync(dir);
224220
fs.writeFileSync(manifestFilePath, fileContents);

0 commit comments

Comments
 (0)