Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Options:
--no-write Don't update record file [boolean] [default: false]
--stage-record-file Git add record file. Helpful when running esplint on a
pre-commit hook. [boolean] [default: false]
--working-dir directory from which config can be read from and record file written to [string] [default: ""]
```

### `esplint`
Expand Down
7 changes: 6 additions & 1 deletion __tests__/cli/check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ it("should pass default options to engine", () => {
cli([]);

expect(run).toHaveBeenCalledWith(
{ write: true, overwrite: false, stageRecordFile: false },
{ write: true, overwrite: false, stageRecordFile: false, workingDir: "" },
expect.anything()
);
});
Expand All @@ -64,6 +64,11 @@ it("should pass --overwrite option to engine", () => {
expect(run.mock.calls[0][0].overwrite).toEqual(true);
});

it("should pass --working-dir option to engine", () => {
cli(["--working-dir", "./example"]);
expect(run.mock.calls[0][0].workingDir).toEqual("./example");
});

it("should print exception and exit with error code", () => {
const error = new Error("this is an error");
run.mockImplementation(() => {
Expand Down
40 changes: 40 additions & 0 deletions __tests__/config/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const path = require("path");
const { getConfig } = require("../../lib/config");

describe("getConfig", function() {
it("should load config from another directory", function() {
const workingDir = path.resolve(__dirname, "./fixtures");
const expectedResult = {
surfaceArea: ["."],
eslint: {},
rules: ["no-console"],
write: true,
overwrite: false,
workingDir,
__originalConfig: {
surfaceArea: ["."],
rules: ["no-console"]
}
};
const result = getConfig({ workingDir });
expect(result).toEqual(expectedResult);
});
it("should load config from the current directory", function() {
const oldDir = process.cwd();
process.chdir(path.resolve(__dirname, "./fixtures"));
const expectedResult = {
surfaceArea: ["."],
eslint: {},
rules: ["no-console"],
write: true,
overwrite: false,
__originalConfig: {
surfaceArea: ["."],
rules: ["no-console"]
}
};
const result = getConfig();
expect(result).toEqual(expectedResult);
process.chdir(oldDir);
});
});
4 changes: 4 additions & 0 deletions __tests__/config/fixtures/.esplintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
surfaceArea: ["."],
rules: ["no-console"]
};
30 changes: 0 additions & 30 deletions __tests__/record.test.js

This file was deleted.

5 changes: 5 additions & 0 deletions __tests__/record/fixtures/.esplint.rec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recordVersion": 1,
"configHash": "773a8b36f5d74ada1b0144c983a6d725c71c9413",
"files": {}
}
4 changes: 4 additions & 0 deletions __tests__/record/fixtures/.esplintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
surfaceArea: ["."],
rules: ["no-console"]
};
1 change: 1 addition & 0 deletions __tests__/record/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log();
1 change: 1 addition & 0 deletions __tests__/record/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
56 changes: 56 additions & 0 deletions __tests__/record/record.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const path = require("path");
const { createRecord, readRecord } = require("../../lib/record");
jest.mock("../../package.json", () => ({ version: "1.0.0" }));

describe("createRecord", () => {
it("attaches current version", () => {
const { recordVersion } = createRecord({
config: { rules: [], __originalConfig: { rules: [] } },
files: {}
});
expect(recordVersion).toEqual(1);
});

it("sorts files", () => {
const { files } = createRecord({
config: { rules: [], __originalConfig: { rules: [] } },
files: {
"z/a/c": {
rule: 1
},
"b/b/c": {
rule: 1
},
"a/b/c": {
rule: 1
}
}
});
expect(Object.keys(files)).toEqual(["a/b/c", "b/b/c", "z/a/c"]);
});
});

describe("readRecord", () => {
it("should read record from another directory", () => {
const expectedResult = {
recordVersion: 1,
configHash: "773a8b36f5d74ada1b0144c983a6d725c71c9413",
files: {}
};
const workingDir = path.resolve(__dirname, "./fixtures");
const result = readRecord({ workingDir });
expect(result).toEqual(expectedResult);
});
it("should read record from root directory", () => {
const oldDir = process.cwd();
const expectedResult = {
recordVersion: 1,
configHash: "773a8b36f5d74ada1b0144c983a6d725c71c9413",
files: {}
};
process.chdir(path.resolve(__dirname, "./fixtures"));
const result = readRecord();
expect(result).toEqual(expectedResult);
process.chdir(oldDir);
});
});
10 changes: 8 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ function cli(processArgv = process.argv.slice(2)) {
describe:
"Git add record file. Helpful when running esplint on a pre-commit hook.",
type: "boolean"
})
.option("working-dir", {
default: "",
describe:
"The path to the working directory in which esplint will be run instead of the current directory",
type: "string"
}),
argv => {
check(argv);
Expand Down Expand Up @@ -169,8 +175,8 @@ function suppress(argv) {
}
}

function getOptions({ overwrite, noWrite, stageRecordFile }) {
return { overwrite, write: !noWrite, stageRecordFile };
function getOptions({ overwrite, noWrite, stageRecordFile, workingDir }) {
return { overwrite, write: !noWrite, stageRecordFile, workingDir };
}

function getFiles({ files }) {
Expand Down
10 changes: 9 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const cosmiconfig = require("cosmiconfig");
const path = require("path");
const log = require("./log");
const EsplintError = require("./EsplintError");

const explorer = cosmiconfig("esplint");

function load({ workingDir } = {}) {
if (workingDir) {
return explorer.searchSync(path.resolve(workingDir));
} else {
return explorer.searchSync();
}
}
function getConfig(options = {}) {
const result = explorer.searchSync();
const result = load(options);
const defaultConfig = {
surfaceArea: ["."],
eslint: {},
Expand Down
Loading