@monorepolint/rules 0.5.0-alpha.9 → 0.5.0-alpha.95
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/__tests__/alphabeticalScripts.spec.d.ts +8 -0
- package/lib/__tests__/alphabeticalScripts.spec.d.ts.map +1 -0
- package/lib/__tests__/alphabeticalScripts.spec.js +61 -0
- package/lib/__tests__/alphabeticalScripts.spec.js.map +1 -0
- package/lib/__tests__/bannedDependencies.spec.d.ts +2 -0
- package/lib/__tests__/bannedDependencies.spec.d.ts.map +1 -0
- package/lib/__tests__/bannedDependencies.spec.js +161 -0
- package/lib/__tests__/bannedDependencies.spec.js.map +1 -0
- package/lib/__tests__/consistentDependencies.spec.js +30 -23
- package/lib/__tests__/consistentDependencies.spec.js.map +1 -1
- package/lib/__tests__/consistentVersions.spec.d.ts +8 -0
- package/lib/__tests__/consistentVersions.spec.d.ts.map +1 -0
- package/lib/__tests__/consistentVersions.spec.js +183 -0
- package/lib/__tests__/consistentVersions.spec.js.map +1 -0
- package/lib/__tests__/fileContents.spec.d.ts +8 -0
- package/lib/__tests__/fileContents.spec.d.ts.map +1 -0
- package/lib/__tests__/fileContents.spec.js +59 -0
- package/lib/__tests__/fileContents.spec.js.map +1 -0
- package/lib/__tests__/mustSatisfyPeerDependencies.spec.d.ts +8 -0
- package/lib/__tests__/mustSatisfyPeerDependencies.spec.d.ts.map +1 -0
- package/lib/__tests__/mustSatisfyPeerDependencies.spec.js +1063 -0
- package/lib/__tests__/mustSatisfyPeerDependencies.spec.js.map +1 -0
- package/lib/__tests__/nestedWorkspaces.spec.d.ts +2 -0
- package/lib/__tests__/nestedWorkspaces.spec.d.ts.map +1 -0
- package/lib/__tests__/nestedWorkspaces.spec.js +124 -0
- package/lib/__tests__/nestedWorkspaces.spec.js.map +1 -0
- package/lib/__tests__/packageEntry.spec.js +72 -27
- package/lib/__tests__/packageEntry.spec.js.map +1 -1
- package/lib/__tests__/packageOrder.spec.js +34 -33
- package/lib/__tests__/packageOrder.spec.js.map +1 -1
- package/lib/__tests__/packageScript.spec.js +50 -51
- package/lib/__tests__/packageScript.spec.js.map +1 -1
- package/lib/__tests__/requireDependency.spec.d.ts +2 -0
- package/lib/__tests__/requireDependency.spec.d.ts.map +1 -0
- package/lib/__tests__/requireDependency.spec.js +123 -0
- package/lib/__tests__/requireDependency.spec.js.map +1 -0
- package/lib/__tests__/utils.d.ts +69 -1
- package/lib/__tests__/utils.d.ts.map +1 -1
- package/lib/__tests__/utils.js +72 -12
- package/lib/__tests__/utils.js.map +1 -1
- package/lib/alphabeticalDependencies.d.ts +3 -1
- package/lib/alphabeticalDependencies.d.ts.map +1 -1
- package/lib/alphabeticalDependencies.js +5 -39
- package/lib/alphabeticalDependencies.js.map +1 -1
- package/lib/alphabeticalScripts.d.ts +12 -0
- package/lib/alphabeticalScripts.d.ts.map +1 -0
- package/lib/alphabeticalScripts.js +20 -0
- package/lib/alphabeticalScripts.js.map +1 -0
- package/lib/bannedDependencies.d.ts +23 -5
- package/lib/bannedDependencies.d.ts.map +1 -1
- package/lib/bannedDependencies.js +110 -36
- package/lib/bannedDependencies.js.map +1 -1
- package/lib/consistentDependencies.d.ts +8 -1
- package/lib/consistentDependencies.d.ts.map +1 -1
- package/lib/consistentDependencies.js +30 -12
- package/lib/consistentDependencies.js.map +1 -1
- package/lib/consistentVersions.d.ts +14 -0
- package/lib/consistentVersions.d.ts.map +1 -0
- package/lib/consistentVersions.js +94 -0
- package/lib/consistentVersions.js.map +1 -0
- package/lib/fileContents.d.ts +8 -8
- package/lib/fileContents.d.ts.map +1 -1
- package/lib/fileContents.js +32 -27
- package/lib/fileContents.js.map +1 -1
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +19 -8
- package/lib/index.js.map +1 -1
- package/lib/mustSatisfyPeerDependencies.d.ts +240 -0
- package/lib/mustSatisfyPeerDependencies.d.ts.map +1 -0
- package/lib/mustSatisfyPeerDependencies.js +636 -0
- package/lib/mustSatisfyPeerDependencies.js.map +1 -0
- package/lib/nestedWorkspaces.d.ts +13 -0
- package/lib/nestedWorkspaces.d.ts.map +1 -0
- package/lib/nestedWorkspaces.js +50 -0
- package/lib/nestedWorkspaces.js.map +1 -0
- package/lib/packageEntry.d.ts +15 -6
- package/lib/packageEntry.d.ts.map +1 -1
- package/lib/packageEntry.js +53 -18
- package/lib/packageEntry.js.map +1 -1
- package/lib/packageOrder.d.ts +3 -3
- package/lib/packageOrder.d.ts.map +1 -1
- package/lib/packageOrder.js +8 -7
- package/lib/packageOrder.js.map +1 -1
- package/lib/packageScript.d.ts +10 -10
- package/lib/packageScript.js +6 -5
- package/lib/packageScript.js.map +1 -1
- package/lib/requireDependency.d.ts +15 -0
- package/lib/requireDependency.d.ts.map +1 -0
- package/lib/requireDependency.js +65 -0
- package/lib/requireDependency.js.map +1 -0
- package/lib/standardTsconfig.d.ts +20 -8
- package/lib/standardTsconfig.d.ts.map +1 -1
- package/lib/standardTsconfig.js +36 -18
- package/lib/standardTsconfig.js.map +1 -1
- package/lib/util/checkAlpha.d.ts +10 -0
- package/lib/util/checkAlpha.d.ts.map +1 -0
- package/lib/util/checkAlpha.js +51 -0
- package/lib/util/checkAlpha.js.map +1 -0
- package/lib/util/makeDirectory.d.ts +8 -0
- package/lib/util/makeDirectory.d.ts.map +1 -0
- package/lib/util/makeDirectory.js +28 -0
- package/lib/util/makeDirectory.js.map +1 -0
- package/lib/util/packageDependencyGraphService.d.ts +37 -0
- package/lib/util/packageDependencyGraphService.d.ts.map +1 -0
- package/lib/util/packageDependencyGraphService.js +70 -0
- package/lib/util/packageDependencyGraphService.js.map +1 -0
- package/package.json +18 -15
- package/src/__tests__/alphabeticalScripts.spec.ts +75 -0
- package/src/__tests__/bannedDependencies.spec.ts +191 -0
- package/src/__tests__/consistentDependencies.spec.ts +39 -26
- package/src/__tests__/consistentVersions.spec.ts +223 -0
- package/src/__tests__/fileContents.spec.ts +74 -0
- package/src/__tests__/mustSatisfyPeerDependencies.spec.ts +1188 -0
- package/src/__tests__/nestedWorkspaces.spec.ts +152 -0
- package/src/__tests__/packageEntry.spec.ts +98 -31
- package/src/__tests__/packageOrder.spec.ts +46 -40
- package/src/__tests__/packageScript.spec.ts +62 -54
- package/src/__tests__/requireDependency.spec.ts +151 -0
- package/src/__tests__/utils.ts +113 -11
- package/src/alphabeticalDependencies.ts +3 -47
- package/src/alphabeticalScripts.ts +19 -0
- package/src/bannedDependencies.ts +133 -43
- package/src/consistentDependencies.ts +34 -12
- package/src/consistentVersions.ts +140 -0
- package/src/fileContents.ts +31 -27
- package/src/index.ts +5 -0
- package/src/mustSatisfyPeerDependencies.ts +739 -0
- package/src/nestedWorkspaces.ts +59 -0
- package/src/packageEntry.ts +67 -24
- package/src/packageOrder.ts +6 -6
- package/src/packageScript.ts +3 -3
- package/src/requireDependency.ts +69 -0
- package/src/standardTsconfig.ts +40 -18
- package/src/util/checkAlpha.ts +59 -0
- package/src/util/makeDirectory.ts +24 -0
- package/src/util/packageDependencyGraphService.ts +114 -0
- package/tsconfig.tsbuildinfo +1 -2439
|
@@ -6,13 +6,10 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
// tslint:disable:no-console
|
|
9
|
-
import { createMockFiles } from "./utils";
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
const mockFiles: Map<string, string> = createMockFiles();
|
|
13
|
-
|
|
14
|
-
import { Failure, PackageContext } from "@monorepolint/core";
|
|
10
|
+
import { Context, Failure } from "@monorepolint/core";
|
|
15
11
|
import { packageScript } from "../packageScript";
|
|
12
|
+
import { AddErrorSpy, createTestingWorkspace, HOST_FACTORIES, TestingWorkspace } from "./utils";
|
|
16
13
|
|
|
17
14
|
const json = (a: unknown) => JSON.stringify(a, undefined, 2) + "\n";
|
|
18
15
|
|
|
@@ -33,29 +30,24 @@ const PACKAGE_WITH_SCRIPTS = json({
|
|
|
33
30
|
},
|
|
34
31
|
});
|
|
35
32
|
|
|
36
|
-
describe("expectPackageScript", () => {
|
|
37
|
-
afterEach(() => {
|
|
38
|
-
mockFiles.clear();
|
|
39
|
-
});
|
|
40
|
-
|
|
33
|
+
describe.each(HOST_FACTORIES)("expectPackageScript ($name)", (hostFactory) => {
|
|
41
34
|
describe("fix: false", () => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
fix: false,
|
|
45
|
-
verbose: false,
|
|
46
|
-
silent: true,
|
|
47
|
-
});
|
|
35
|
+
let workspace: TestingWorkspace;
|
|
36
|
+
let spy: AddErrorSpy;
|
|
48
37
|
|
|
49
|
-
|
|
38
|
+
beforeEach(async () => {
|
|
39
|
+
workspace = await createTestingWorkspace({
|
|
40
|
+
fixFlag: false,
|
|
41
|
+
host: hostFactory.make(),
|
|
42
|
+
});
|
|
50
43
|
|
|
51
|
-
|
|
52
|
-
spy.mockClear();
|
|
44
|
+
spy = jest.spyOn(workspace.context, "addError");
|
|
53
45
|
});
|
|
54
46
|
|
|
55
47
|
it("handles an empty script section", () => {
|
|
56
|
-
|
|
48
|
+
workspace.writeFile("package.json", PACKAGE_WITHOUT_SCRIPTS);
|
|
57
49
|
|
|
58
|
-
packageScript.check(context, {
|
|
50
|
+
packageScript.check(workspace.context, {
|
|
59
51
|
scripts: {
|
|
60
52
|
foo: "bar",
|
|
61
53
|
},
|
|
@@ -64,27 +56,33 @@ describe("expectPackageScript", () => {
|
|
|
64
56
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
65
57
|
|
|
66
58
|
const failure: Failure = spy.mock.calls[0][0];
|
|
67
|
-
expect(failure
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
expect(failure).toMatchObject(
|
|
60
|
+
workspace.failureMatcher({
|
|
61
|
+
file: "package.json",
|
|
62
|
+
hasFixer: true,
|
|
63
|
+
message: "No scripts block in package.json",
|
|
64
|
+
})
|
|
65
|
+
);
|
|
70
66
|
});
|
|
71
67
|
});
|
|
72
68
|
|
|
73
69
|
describe("fix: true", () => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
let workspace: TestingWorkspace;
|
|
71
|
+
let spy: AddErrorSpy;
|
|
72
|
+
let context: Context;
|
|
73
|
+
|
|
74
|
+
beforeEach(async () => {
|
|
75
|
+
workspace = await createTestingWorkspace({
|
|
76
|
+
fixFlag: true,
|
|
77
|
+
host: hostFactory.make(),
|
|
78
|
+
});
|
|
81
79
|
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
spy = jest.spyOn(workspace.context, "addError");
|
|
81
|
+
context = workspace.context; // minimizing delta
|
|
84
82
|
});
|
|
85
83
|
|
|
86
84
|
it("fixes an empty script section", () => {
|
|
87
|
-
|
|
85
|
+
workspace.writeFile("package.json", PACKAGE_WITHOUT_SCRIPTS);
|
|
88
86
|
|
|
89
87
|
packageScript.check(context, {
|
|
90
88
|
scripts: {
|
|
@@ -95,15 +93,19 @@ describe("expectPackageScript", () => {
|
|
|
95
93
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
96
94
|
|
|
97
95
|
const failure: Failure = spy.mock.calls[0][0];
|
|
98
|
-
expect(failure
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
96
|
+
expect(failure).toMatchObject(
|
|
97
|
+
workspace.failureMatcher({
|
|
98
|
+
file: "package.json",
|
|
99
|
+
hasFixer: true,
|
|
100
|
+
message: "No scripts block in package.json",
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({});
|
|
103
105
|
});
|
|
104
106
|
|
|
105
107
|
it("adds a script", () => {
|
|
106
|
-
|
|
108
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
107
109
|
|
|
108
110
|
packageScript.check(context, {
|
|
109
111
|
scripts: {
|
|
@@ -114,15 +116,21 @@ describe("expectPackageScript", () => {
|
|
|
114
116
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
115
117
|
|
|
116
118
|
const failure: Failure = spy.mock.calls[0][0];
|
|
117
|
-
expect(failure
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
expect(failure).toMatchObject(
|
|
120
|
+
workspace.failureMatcher({
|
|
121
|
+
file: "package.json",
|
|
122
|
+
hasFixer: true,
|
|
123
|
+
message: expect.stringContaining(`Expected standardized script entry for '${MISSING_SCRIPT_NAME}'`),
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts[MISSING_SCRIPT_NAME]).toEqual(
|
|
128
|
+
MISSING_SCRIPT_VALUE
|
|
129
|
+
);
|
|
122
130
|
});
|
|
123
131
|
|
|
124
132
|
it("does nothing if the value exists", () => {
|
|
125
|
-
|
|
133
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
126
134
|
|
|
127
135
|
packageScript.check(context, {
|
|
128
136
|
scripts: {
|
|
@@ -132,13 +140,13 @@ describe("expectPackageScript", () => {
|
|
|
132
140
|
|
|
133
141
|
expect(spy).not.toHaveBeenCalled();
|
|
134
142
|
|
|
135
|
-
expect(JSON.parse(
|
|
143
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({
|
|
136
144
|
[SCRIPT_NAME]: SCRIPT_VALUE,
|
|
137
145
|
});
|
|
138
146
|
});
|
|
139
147
|
|
|
140
148
|
it("errors if long form is used and no value matches and there is no fixValue", () => {
|
|
141
|
-
|
|
149
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
142
150
|
|
|
143
151
|
packageScript.check(context, {
|
|
144
152
|
scripts: {
|
|
@@ -155,7 +163,7 @@ describe("expectPackageScript", () => {
|
|
|
155
163
|
});
|
|
156
164
|
|
|
157
165
|
it("uses the fixValue for fixing if provided", () => {
|
|
158
|
-
|
|
166
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
159
167
|
|
|
160
168
|
packageScript.check(context, {
|
|
161
169
|
scripts: {
|
|
@@ -171,14 +179,14 @@ describe("expectPackageScript", () => {
|
|
|
171
179
|
expect(errors.length).toBe(1);
|
|
172
180
|
expect(errors[0][0].fixer).toBeDefined();
|
|
173
181
|
|
|
174
|
-
expect(JSON.parse(
|
|
182
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({
|
|
175
183
|
[SCRIPT_NAME]: SCRIPT_VALUE,
|
|
176
184
|
foo: "a",
|
|
177
185
|
});
|
|
178
186
|
});
|
|
179
187
|
|
|
180
188
|
it("can fix to empty", () => {
|
|
181
|
-
|
|
189
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
182
190
|
|
|
183
191
|
packageScript.check(context, {
|
|
184
192
|
scripts: {
|
|
@@ -194,11 +202,11 @@ describe("expectPackageScript", () => {
|
|
|
194
202
|
expect(errors.length).toBe(1);
|
|
195
203
|
expect(errors[0][0].fixer).toBeDefined();
|
|
196
204
|
|
|
197
|
-
expect(JSON.parse(
|
|
205
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({});
|
|
198
206
|
});
|
|
199
207
|
|
|
200
208
|
it("can allow only empty", () => {
|
|
201
|
-
|
|
209
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
202
210
|
|
|
203
211
|
packageScript.check(context, {
|
|
204
212
|
scripts: {
|
|
@@ -214,7 +222,7 @@ describe("expectPackageScript", () => {
|
|
|
214
222
|
expect(errors.length).toBe(1);
|
|
215
223
|
expect(errors[0][0].fixer).toBeDefined();
|
|
216
224
|
|
|
217
|
-
expect(JSON.parse(
|
|
225
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({});
|
|
218
226
|
});
|
|
219
227
|
});
|
|
220
228
|
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2019 Palantir Technologies, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the MIT license. See LICENSE file in the project root for details.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
import { WorkspaceContext } from "@monorepolint/core";
|
|
8
|
+
import { SimpleHost } from "@monorepolint/utils";
|
|
9
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
10
|
+
import * as path from "path";
|
|
11
|
+
import * as tmp from "tmp";
|
|
12
|
+
import { requireDependency } from "../requireDependency";
|
|
13
|
+
import { makeDirectoryRecursively } from "../util/makeDirectory";
|
|
14
|
+
import { jsonToString } from "./utils";
|
|
15
|
+
|
|
16
|
+
const PACKAGE_ROOT = jsonToString({
|
|
17
|
+
workspaces: {
|
|
18
|
+
packages: ["packages/*"],
|
|
19
|
+
},
|
|
20
|
+
dependencies: {
|
|
21
|
+
foo: "5",
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const PACKAGE_WITH_NO_ENTRIES = jsonToString({});
|
|
26
|
+
|
|
27
|
+
const PACKAGE_WITH_ENTRIES_MISSING = jsonToString({
|
|
28
|
+
dependencies: {},
|
|
29
|
+
devDependencies: {},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const PACKAGE_WITH_WRONG_ENTRIES = jsonToString({
|
|
33
|
+
dependencies: {
|
|
34
|
+
foo: "0.1.0",
|
|
35
|
+
},
|
|
36
|
+
devDependencies: {
|
|
37
|
+
bar: "1.0.0",
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const PACKAGE_WITH_RIGHT_ENTRIES = jsonToString({
|
|
42
|
+
dependencies: {
|
|
43
|
+
foo: "1.0.0",
|
|
44
|
+
},
|
|
45
|
+
devDependencies: {
|
|
46
|
+
bar: "^2.0.0",
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const OPTIONS = {
|
|
51
|
+
dependencies: {
|
|
52
|
+
foo: "1.0.0",
|
|
53
|
+
},
|
|
54
|
+
devDependencies: {
|
|
55
|
+
bar: "^2.0.0",
|
|
56
|
+
},
|
|
57
|
+
} as const;
|
|
58
|
+
|
|
59
|
+
const CORRECT_OUTPUT = jsonToString(OPTIONS);
|
|
60
|
+
|
|
61
|
+
describe("requireDependency", () => {
|
|
62
|
+
tmp.setGracefulCleanup();
|
|
63
|
+
|
|
64
|
+
let cleanupJobs: Array<() => void> = [];
|
|
65
|
+
|
|
66
|
+
afterEach(() => {
|
|
67
|
+
for (const cleanupJob of cleanupJobs) {
|
|
68
|
+
cleanupJob();
|
|
69
|
+
}
|
|
70
|
+
cleanupJobs = [];
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
function makeWorkspace({ fix }: { fix: boolean }) {
|
|
74
|
+
const dir: tmp.DirResult = tmp.dirSync({ unsafeCleanup: true });
|
|
75
|
+
cleanupJobs.push(() => dir.removeCallback());
|
|
76
|
+
|
|
77
|
+
const workspaceContext = new WorkspaceContext(
|
|
78
|
+
dir.name,
|
|
79
|
+
{
|
|
80
|
+
rules: [],
|
|
81
|
+
fix,
|
|
82
|
+
verbose: false,
|
|
83
|
+
silent: true,
|
|
84
|
+
},
|
|
85
|
+
new SimpleHost()
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
function checkAndSpy(q: string) {
|
|
89
|
+
const context = workspaceContext.createChildContext(path.resolve(dir.name, q));
|
|
90
|
+
const addErrorSpy = jest.spyOn(context, "addError");
|
|
91
|
+
requireDependency.check(context, OPTIONS);
|
|
92
|
+
return { context, addErrorSpy };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function addFile(filePath: string, content: string) {
|
|
96
|
+
const dirPath = path.resolve(dir.name, path.dirname(filePath));
|
|
97
|
+
const resolvedFilePath = path.resolve(dir.name, filePath);
|
|
98
|
+
|
|
99
|
+
makeDirectoryRecursively(dirPath);
|
|
100
|
+
writeFileSync(resolvedFilePath, content);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function readFile(filePath: string) {
|
|
104
|
+
return readFileSync(path.resolve(dir.name, filePath)).toString();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { addFile, readFile, workspaceContext, checkAndSpy };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
it("checks correctly", () => {
|
|
111
|
+
const { addFile, workspaceContext, checkAndSpy } = makeWorkspace({ fix: false });
|
|
112
|
+
addFile("./package.json", PACKAGE_ROOT);
|
|
113
|
+
addFile("./packages/none/package.json", PACKAGE_WITH_NO_ENTRIES);
|
|
114
|
+
addFile("./packages/missing/package.json", PACKAGE_WITH_ENTRIES_MISSING);
|
|
115
|
+
addFile("./packages/wrong/package.json", PACKAGE_WITH_WRONG_ENTRIES);
|
|
116
|
+
addFile("./packages/right/package.json", PACKAGE_WITH_RIGHT_ENTRIES);
|
|
117
|
+
|
|
118
|
+
requireDependency.check(workspaceContext, OPTIONS);
|
|
119
|
+
|
|
120
|
+
const none = checkAndSpy("./packages/none");
|
|
121
|
+
expect(none.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
122
|
+
|
|
123
|
+
const missing = checkAndSpy("./packages/missing");
|
|
124
|
+
expect(missing.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
125
|
+
|
|
126
|
+
const wrong = checkAndSpy("./packages/wrong");
|
|
127
|
+
expect(wrong.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
128
|
+
|
|
129
|
+
const right = checkAndSpy("./packages/right");
|
|
130
|
+
expect(right.addErrorSpy).toHaveBeenCalledTimes(0);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("fixes correctly", () => {
|
|
134
|
+
const { addFile, readFile, checkAndSpy } = makeWorkspace({ fix: true });
|
|
135
|
+
addFile("./package.json", PACKAGE_ROOT);
|
|
136
|
+
addFile("./packages/missing/package.json", PACKAGE_WITH_ENTRIES_MISSING);
|
|
137
|
+
addFile("./packages/wrong/package.json", PACKAGE_WITH_WRONG_ENTRIES);
|
|
138
|
+
|
|
139
|
+
const missing = checkAndSpy("./packages/missing");
|
|
140
|
+
expect(missing.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
141
|
+
|
|
142
|
+
const wrong = checkAndSpy("./packages/wrong");
|
|
143
|
+
expect(wrong.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
144
|
+
|
|
145
|
+
const missingContents = readFile("./packages/missing/package.json");
|
|
146
|
+
expect(missingContents).toEqual(CORRECT_OUTPUT);
|
|
147
|
+
|
|
148
|
+
const contents = readFile("./packages/wrong/package.json");
|
|
149
|
+
expect(contents).toEqual(CORRECT_OUTPUT);
|
|
150
|
+
});
|
|
151
|
+
});
|
package/src/__tests__/utils.ts
CHANGED
|
@@ -5,19 +5,121 @@
|
|
|
5
5
|
*
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
import { AddErrorOptions, WorkspaceContext } from "@monorepolint/core";
|
|
9
|
+
import { CachingHost, Host, SimpleHost } from "@monorepolint/utils";
|
|
10
|
+
import * as path from "path";
|
|
11
|
+
import * as tmp from "tmp";
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
export function jsonToString(obj: {}) {
|
|
14
|
+
return JSON.stringify(obj, undefined, 2) + "\n";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface TestingWorkspaceOpts {
|
|
18
|
+
host: Host;
|
|
19
|
+
rootProjectName?: string;
|
|
20
|
+
fixFlag: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function createTestingWorkspace(inboundOpts: TestingWorkspaceOpts) {
|
|
24
|
+
tmp.setGracefulCleanup();
|
|
25
|
+
const tmpdir = tmp.dirSync();
|
|
26
|
+
const opts = {
|
|
27
|
+
...inboundOpts,
|
|
28
|
+
rootProjectName: inboundOpts.rootProjectName ?? "rootProject",
|
|
29
|
+
};
|
|
30
|
+
const rootPath = tmpdir.name;
|
|
31
|
+
opts.host.mkdir(rootPath, { recursive: true });
|
|
15
32
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
33
|
+
opts.host.writeJson(path.join(rootPath, "package.json"), {
|
|
34
|
+
name: opts.rootProjectName,
|
|
35
|
+
workspaces: {
|
|
36
|
+
packages: ["packages/*"],
|
|
19
37
|
},
|
|
20
|
-
})
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await opts.host.flush();
|
|
21
41
|
|
|
22
|
-
return
|
|
42
|
+
return new DefaultTestingWorkspace(
|
|
43
|
+
{ ...opts, rootPath },
|
|
44
|
+
new WorkspaceContext(rootPath, { fix: opts.fixFlag, rules: [] }, opts.host)
|
|
45
|
+
);
|
|
23
46
|
}
|
|
47
|
+
|
|
48
|
+
interface RealTestingWorkspaceOpts extends Required<TestingWorkspaceOpts> {
|
|
49
|
+
rootPath: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface TestingWorkspace {
|
|
53
|
+
/**
|
|
54
|
+
* Adds a utf8 file to `packageName` with `relativePath` to the package directory.
|
|
55
|
+
*
|
|
56
|
+
* @param packageName The child package to add to or undefined for root package
|
|
57
|
+
* @param filePath The path of the file to be written. Will be prepended with package dir
|
|
58
|
+
* @param contents the contents to be written (as utf8)
|
|
59
|
+
*/
|
|
60
|
+
writeFile(filePath: string, contents: string): void;
|
|
61
|
+
|
|
62
|
+
writeJsonFile(filePath: string, json: object): void;
|
|
63
|
+
addProject(name: string, fields: object): void;
|
|
64
|
+
getFilePath(filePath: string): string;
|
|
65
|
+
readFile(filePath: string): string;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Helper method for matching failures via jest `expect().toMatchObject
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
failureMatcher(opts: { file: string; message: string; hasFixer: boolean }): any;
|
|
72
|
+
|
|
73
|
+
readonly context: WorkspaceContext;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class DefaultTestingWorkspace implements TestingWorkspace {
|
|
77
|
+
constructor(private opts: RealTestingWorkspaceOpts, public readonly context: WorkspaceContext) {}
|
|
78
|
+
|
|
79
|
+
addProject(name: string, fields: object) {
|
|
80
|
+
this.writeJsonFile(path.join("packages", name, "package.json"), {
|
|
81
|
+
name,
|
|
82
|
+
...fields,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
writeJsonFile(filePath: string, json: object) {
|
|
87
|
+
this.writeFile(filePath, JSON.stringify(json, undefined, 2));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Adds a utf8 file to `packageName` with `relativePath` to the package directory.
|
|
92
|
+
*
|
|
93
|
+
* @param packageName The child package to add to or undefined for root package
|
|
94
|
+
* @param filePath The path of the file to be written. Will be prepended with package dir
|
|
95
|
+
* @param contents the contents to be written (as utf8)
|
|
96
|
+
*/
|
|
97
|
+
writeFile(filePath: string, contents: string) {
|
|
98
|
+
const fullFilePath = this.getFilePath(filePath);
|
|
99
|
+
this.opts.host.mkdir(path.dirname(fullFilePath), { recursive: true });
|
|
100
|
+
this.opts.host.writeFile(fullFilePath, contents, { encoding: "utf-8" });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
getFilePath(filePath: string) {
|
|
104
|
+
return path.join(this.opts.rootPath, filePath);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
readFile(filePath: string) {
|
|
108
|
+
return this.opts.host.readFile(this.getFilePath(filePath), { encoding: "utf-8" });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
failureMatcher(opts: { file: string; message: string; hasFixer: boolean }) {
|
|
112
|
+
return {
|
|
113
|
+
file: this.getFilePath(opts.file),
|
|
114
|
+
message: opts.message,
|
|
115
|
+
...(opts.hasFixer ? { fixer: expect.any(Function) } : {}),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type AddErrorSpy = jest.SpyInstance<void, [AddErrorOptions]>;
|
|
121
|
+
|
|
122
|
+
export const HOST_FACTORIES: Array<{ name: string; make: () => Host }> = [
|
|
123
|
+
{ name: "SimpleHost", make: () => new SimpleHost() },
|
|
124
|
+
{ name: "CachingHost", make: () => new CachingHost() },
|
|
125
|
+
];
|
|
@@ -6,60 +6,16 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { Context, RuleModule } from "@monorepolint/core";
|
|
9
|
-
import { writeJson } from "@monorepolint/utils";
|
|
10
|
-
import diff from "jest-diff";
|
|
11
9
|
import * as r from "runtypes";
|
|
10
|
+
import { checkAlpha } from "./util/checkAlpha";
|
|
12
11
|
|
|
13
12
|
const Options = r.Undefined;
|
|
14
13
|
|
|
15
|
-
export const alphabeticalDependencies = {
|
|
14
|
+
export const alphabeticalDependencies: RuleModule<typeof Options> = {
|
|
16
15
|
check: function expectAlphabeticalDependencies(context: Context) {
|
|
17
16
|
checkAlpha(context, "dependencies");
|
|
18
17
|
checkAlpha(context, "devDependencies");
|
|
19
18
|
checkAlpha(context, "peerDependencies");
|
|
20
19
|
},
|
|
21
20
|
optionsRuntype: Options,
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function checkAlpha(context: Context, block: "dependencies" | "devDependencies" | "peerDependencies") {
|
|
25
|
-
const packageJson = context.getPackageJson();
|
|
26
|
-
const packagePath = context.getPackageJsonPath();
|
|
27
|
-
|
|
28
|
-
const dependencies = packageJson[block];
|
|
29
|
-
|
|
30
|
-
if (dependencies === undefined) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const actualOrder = Object.keys(dependencies);
|
|
35
|
-
const expectedOrder = actualOrder.slice().sort(); // sort mutates, so we need to copy the previous result
|
|
36
|
-
|
|
37
|
-
if (!arrayOrderCompare(actualOrder, expectedOrder)) {
|
|
38
|
-
context.addError({
|
|
39
|
-
file: packagePath,
|
|
40
|
-
message: `Incorrect order of ${block} in package.json`,
|
|
41
|
-
longMessage: diff(expectedOrder, actualOrder, { expand: true }),
|
|
42
|
-
fixer: () => {
|
|
43
|
-
const expectedDependencies: Record<string, string> = {};
|
|
44
|
-
|
|
45
|
-
expectedOrder.forEach(dep => {
|
|
46
|
-
expectedDependencies[dep] = dependencies[dep];
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const newPackageJson = { ...packageJson };
|
|
50
|
-
newPackageJson[block] = expectedDependencies;
|
|
51
|
-
writeJson(packagePath, newPackageJson);
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function arrayOrderCompare(a: ReadonlyArray<string>, b: ReadonlyArray<string>) {
|
|
58
|
-
for (let index = 0; index < a.length; index++) {
|
|
59
|
-
if (a[index] !== b[index]) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2019 Palantir Technologies, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the MIT license. See LICENSE file in the project root for details.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Context, RuleModule } from "@monorepolint/core";
|
|
9
|
+
import * as r from "runtypes";
|
|
10
|
+
import { checkAlpha } from "./util/checkAlpha";
|
|
11
|
+
|
|
12
|
+
const Options = r.Undefined;
|
|
13
|
+
|
|
14
|
+
export const alphabeticalScripts: RuleModule<typeof Options> = {
|
|
15
|
+
check: function expectAlphabeticalScripts(context: Context) {
|
|
16
|
+
checkAlpha(context, "scripts");
|
|
17
|
+
},
|
|
18
|
+
optionsRuntype: Options,
|
|
19
|
+
};
|