Skip to content

Commit

Permalink
feat: allow users to store json to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Sep 30, 2020
1 parent f7ec953 commit fc7cb3d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/webpack-cli/README.md
Expand Up @@ -53,7 +53,7 @@ yarn add webpack-cli --dev
--no-hot Disables Hot Module Replacement
-d, --devtool string Controls if and how source maps are generated.
--prefetch string Prefetch this request
-j, --json Prints result as JSON
-j, --json string Prints result as JSON or store it in a file
--mode string Defines the mode to pass to webpack
-v, --version Get current version
--stats string It instructs webpack on how to treat the stats
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/groups/resolveStats.js
Expand Up @@ -14,7 +14,7 @@ const resolveStats = (args) => {
finalOptions.options.stats = stats;
}
if (json) {
finalOptions.outputOptions.json = true;
finalOptions.outputOptions.json = json;
}
return finalOptions;
};
Expand Down
12 changes: 11 additions & 1 deletion packages/webpack-cli/lib/utils/Compiler.js
@@ -1,6 +1,7 @@
const { packageExists } = require('@webpack-cli/package-utils');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
const logger = require('./logger');
const { writeFileSync } = require('fs');
const bailAndWatchWarning = require('./warnings/bailAndWatchWarning');
const { CompilerOutput } = require('./CompilerOutput');

Expand Down Expand Up @@ -58,7 +59,7 @@ class Compiler {
if (!outputOptions.watch && (stats.hasErrors() || stats.hasWarnings())) {
process.exitCode = 1;
}
if (outputOptions.json) {
if (outputOptions.json === true) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + '\n');
} else if (stats.hash !== lastHash) {
lastHash = stats.hash;
Expand All @@ -69,6 +70,15 @@ class Compiler {
statsErrors.push({ name: statErr.message, loc: errLoc });
});
}
const JSONStats = JSON.stringify(stats.toJson(outputOptions), null, 2);
if (typeof outputOptions.json === 'string') {
try {
writeFileSync(outputOptions.json, JSONStats);
logger.info(`stats are successfully stored as json to ${outputOptions.json}`);
} catch (err) {
logger.error(err);
}
}
return this.generateOutput(outputOptions, stats, statsErrors);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/utils/cli-flags.js
Expand Up @@ -178,9 +178,9 @@ const core = [
{
name: 'json',
usage: '--json',
type: Boolean,
type: [String, Boolean],
alias: 'j',
description: 'Prints result as JSON',
description: 'Prints result as JSON or store it in a file',
},
{
name: 'mode',
Expand Down
2 changes: 1 addition & 1 deletion scripts/cleanupTest.js
Expand Up @@ -3,7 +3,7 @@ const rimraf = require('rimraf');
const { join } = require('path');
const collectTestFolders = require('./utils');

const outputDirectories = ['bin', 'binary', 'dist', 'test', 'test-assets', 'test-plugin', 'test-loader'];
const outputDirectories = ['bin', 'binary', 'dist', 'test', 'test-assets', 'test-plugin', 'test-loader', 'stats.json'];

function folderStrategy(stats, file) {
return stats.isDirectory() && outputDirectories.includes(file);
Expand Down
22 changes: 22 additions & 0 deletions test/json/json.test.js
@@ -1,5 +1,7 @@
'use strict';
const { run } = require('../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');

describe('json flag', () => {
it('should return valid json', () => {
Expand All @@ -13,6 +15,26 @@ describe('json flag', () => {
expect(parseJson).not.toThrow();
});

it('should store json to a file', (done) => {
const { stdout } = run(__dirname, ['--json', 'stats.json']);

expect(stdout).toContain('stats are successfully stored as json to stats.json');
stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('hash');
expect(data).toContain('time');
expect(data).toContain('errors');
expect(data).toContain('version');
expect(() => JSON.parse(data)).not.toThrow();
done();
});
});

it('should return valid json with -j alias', () => {
const { stdout } = run(__dirname, ['-j']);

Expand Down

0 comments on commit fc7cb3d

Please sign in to comment.