Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: add --output-file #4085

Merged
merged 2 commits into from Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/cli.js
Expand Up @@ -14,6 +14,7 @@ const path = require("path");
const printConfig = require("./printConfig");
const resolveFrom = require("resolve-from");
const standalone = require("./standalone");
const writeOutputFile = require("./writeOutputFile");

const EXIT_CODE_ERROR = 2;

Expand Down Expand Up @@ -77,6 +78,9 @@ const EXIT_CODE_ERROR = 2;
"no-color": {
type: string
},
"output-file" : {
type: string
},
"report-needless-disables": {
alias: string
},
Expand Down Expand Up @@ -120,6 +124,7 @@ const EXIT_CODE_ERROR = 2;
ignorePath: string,
ignorePattern: string,
maxWarnings: number,
outputFile: string,
quiet: any,
reportNeedlessDisables: any,
stdinFilename: any,
Expand Down Expand Up @@ -150,6 +155,7 @@ const EXIT_CODE_ERROR = 2;
fix?: any,
ignoreDisables?: any,
ignorePath?: any,
outputFile?: string,
reportNeedlessDisables?: any,
maxWarnings?: any,
syntax?: any,
Expand Down Expand Up @@ -276,6 +282,10 @@ const meowOptions /*: meowOptionsType*/ = {
Number of warnings above which the process will exit with code ${EXIT_CODE_ERROR}.
Useful when setting "defaultSeverity" to "warning" and expecting the
process to fail on warnings (e.g. CI build).

--output-file, -o

Path of file to write report.

--version, -v

Expand Down Expand Up @@ -344,6 +354,10 @@ const meowOptions /*: meowOptionsType*/ = {
"no-color": {
type: "boolean"
},
"output-file": {
alias: "o",
type: "string"
},
"report-needless-disables": {
alias: "rd",
type: "boolean"
Expand Down Expand Up @@ -464,6 +478,10 @@ module.exports = (argv /*: string[]*/) /*: Promise<void>|void*/ => {
optionsBase.fix = cli.flags.fix;
}

if (cli.flags.outputFile) {
optionsBase.outputFile = cli.flags.outputFile;
}

const reportNeedlessDisables = cli.flags.reportNeedlessDisables;

if (reportNeedlessDisables) {
Expand Down Expand Up @@ -544,6 +562,12 @@ module.exports = (argv /*: string[]*/) /*: Promise<void>|void*/ => {

process.stdout.write(linted.output);

if (options.outputFile) {
writeOutputFile(linted.output, options.outputFile).catch(
handleError
);
}

if (linted.errored) {
process.exitCode = EXIT_CODE_ERROR;
} else if (maxWarnings !== undefined && linted.maxWarningsExceeded) {
Expand Down
10 changes: 10 additions & 0 deletions lib/writeOutputFile.js
@@ -0,0 +1,10 @@
/* @flow */
"use strict";

const path = require("path");
const pify = require("pify");
const stripAnsi = require("strip-ansi");
const writeFileAtomic /*: Function*/ = require("./vendor/writeFileAtomic");

module.exports = (content /*: string*/, filePath /*: string*/) =>
pify(writeFileAtomic)(path.normalize(filePath), stripAnsi(content));
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -82,6 +82,7 @@
"slash": "^3.0.0",
"specificity": "^0.4.1",
"string-width": "^4.1.0",
"strip-ansi": "^5.2.0",
"style-search": "^0.1.0",
"sugarss": "^2.0.0",
"svg-tags": "^1.0.0",
Expand Down Expand Up @@ -111,8 +112,7 @@
"remark-preset-lint-consistent": "^2.0.2",
"remark-preset-lint-recommended": "^3.0.2",
"remark-validate-links": "^8.0.2",
"request": "^2.88.0",
"strip-ansi": "^5.2.0"
"request": "^2.88.0"
},
"scripts": {
"benchmark-rule": "node scripts/benchmark-rule.js",
Expand Down
9 changes: 9 additions & 0 deletions system-tests/outputFile/__snapshots__/outputFile.test.js.snap
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`outputFile writes the result file 1`] = `
"
stylesheet.css
1:3 ✖ Unexpected empty block block-no-empty

"
`;
5 changes: 5 additions & 0 deletions system-tests/outputFile/config.json
@@ -0,0 +1,5 @@
{
"rules": {
"block-no-empty": true
}
}
55 changes: 55 additions & 0 deletions system-tests/outputFile/outputFile.test.js
@@ -0,0 +1,55 @@
"use strict";

const _ = require("lodash");
const del = require("del");
const fs = require("fs");
const os = require("os");
const path = require("path");
const pify = require("pify");
const spawn = require("child_process").spawn;
const systemTestUtils = require("../systemTestUtils");

describe("outputFile", () => {
let tmpDir;
let directionPath;
let cliPath;
let localPath;

beforeEach(() => {
localPath = path.resolve(__dirname);
tmpDir = os.tmpdir();
cliPath = path.join(localPath, "../../bin/stylelint.js");
directionPath = path.join(tmpDir, `direction-${_.uniqueId()}.txt`);
});

afterEach(() => {
del(directionPath, { force: true });
});

it("writes the result file ", done => {
const childProcess = spawn(
"node",
[
cliPath,
`${localPath}/*.css`,
"--config=config.json",
`--output-file=${directionPath}`
],
{
cwd: localPath
}
);

let stdout = "";

childProcess.stdout.on("data", data => (stdout += data));

childProcess.on("close", function() {
return pify(fs.readFile)(directionPath, "utf-8").then(content => {
expect(content).toEqual(systemTestUtils.stripColors(stdout));
expect(content).toMatchSnapshot();
done();
});
});
});
});
1 change: 1 addition & 0 deletions system-tests/outputFile/stylesheet.css
@@ -0,0 +1 @@
a { }