@monorepolint/rules 0.5.0-alpha.10 → 0.5.0-alpha.103
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/.turbo/turbo-clean.log +4 -0
- package/.turbo/turbo-compile-typescript.log +4 -0
- package/.turbo/turbo-lint.log +107 -0
- package/.turbo/turbo-test.log +631 -0
- package/.turbo/turbo-transpile-typescript.log +18 -0
- package/build/js/index.cjs +1491 -0
- package/build/js/index.cjs.map +1 -0
- package/build/js/index.js +1433 -0
- package/build/js/index.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/{lib/__tests__/utils.d.ts → build/types/__tests__/alphabeticalScripts.spec.d.ts} +2 -2
- package/build/types/__tests__/alphabeticalScripts.spec.d.ts.map +1 -0
- package/build/types/__tests__/bannedDependencies.spec.d.ts +2 -0
- package/build/types/__tests__/bannedDependencies.spec.d.ts.map +1 -0
- package/{lib → build/types}/__tests__/consistentDependencies.spec.d.ts +0 -0
- package/build/types/__tests__/consistentDependencies.spec.d.ts.map +1 -0
- package/build/types/__tests__/consistentVersions.spec.d.ts +8 -0
- package/build/types/__tests__/consistentVersions.spec.d.ts.map +1 -0
- package/build/types/__tests__/fileContents.spec.d.ts +8 -0
- package/build/types/__tests__/fileContents.spec.d.ts.map +1 -0
- package/build/types/__tests__/mustSatisfyPeerDependencies.spec.d.ts +8 -0
- package/build/types/__tests__/mustSatisfyPeerDependencies.spec.d.ts.map +1 -0
- package/build/types/__tests__/nestedWorkspaces.spec.d.ts +2 -0
- package/build/types/__tests__/nestedWorkspaces.spec.d.ts.map +1 -0
- package/{lib → build/types}/__tests__/packageEntry.spec.d.ts +0 -0
- package/build/types/__tests__/packageEntry.spec.d.ts.map +1 -0
- package/{lib → build/types}/__tests__/packageOrder.spec.d.ts +0 -0
- package/build/types/__tests__/packageOrder.spec.d.ts.map +1 -0
- package/{lib → build/types}/__tests__/packageScript.spec.d.ts +0 -0
- package/build/types/__tests__/packageScript.spec.d.ts.map +1 -0
- package/build/types/__tests__/requireDependency.spec.d.ts +2 -0
- package/build/types/__tests__/requireDependency.spec.d.ts.map +1 -0
- package/build/types/__tests__/utils.d.ts +81 -0
- package/build/types/__tests__/utils.d.ts.map +1 -0
- package/build/types/alphabeticalDependencies.d.ts +23 -0
- package/build/types/alphabeticalDependencies.d.ts.map +1 -0
- package/build/types/alphabeticalScripts.d.ts +23 -0
- package/build/types/alphabeticalScripts.d.ts.map +1 -0
- package/build/types/bannedDependencies.d.ts +134 -0
- package/build/types/bannedDependencies.d.ts.map +1 -0
- package/build/types/consistentDependencies.d.ts +38 -0
- package/build/types/consistentDependencies.d.ts.map +1 -0
- package/build/types/consistentVersions.d.ts +47 -0
- package/build/types/consistentVersions.d.ts.map +1 -0
- package/build/types/fileContents.d.ts +111 -0
- package/build/types/fileContents.d.ts.map +1 -0
- package/build/types/index.d.ts +20 -0
- package/build/types/index.d.ts.map +1 -0
- package/build/types/mustSatisfyPeerDependencies.d.ts +721 -0
- package/build/types/mustSatisfyPeerDependencies.d.ts.map +1 -0
- package/build/types/nestedWorkspaces.d.ts +24 -0
- package/build/types/nestedWorkspaces.d.ts.map +1 -0
- package/build/types/packageEntry.d.ts +115 -0
- package/build/types/packageEntry.d.ts.map +1 -0
- package/build/types/packageOrder.d.ts +33 -0
- package/build/types/packageOrder.d.ts.map +1 -0
- package/build/types/packageScript.d.ts +89 -0
- package/build/types/packageScript.d.ts.map +1 -0
- package/build/types/requireDependency.d.ts +99 -0
- package/build/types/requireDependency.d.ts.map +1 -0
- package/build/types/standardTsconfig.d.ts +113 -0
- package/build/types/standardTsconfig.d.ts.map +1 -0
- package/build/types/util/checkAlpha.d.ts +10 -0
- package/build/types/util/checkAlpha.d.ts.map +1 -0
- package/build/types/util/createNewRuleConversion.d.ts +30 -0
- package/build/types/util/createNewRuleConversion.d.ts.map +1 -0
- package/build/types/util/makeDirectory.d.ts +8 -0
- package/build/types/util/makeDirectory.d.ts.map +1 -0
- package/build/types/util/packageDependencyGraphService.d.ts +37 -0
- package/build/types/util/packageDependencyGraphService.d.ts.map +1 -0
- package/{jest.config.js → jest.config.cjs} +0 -0
- package/package.json +39 -20
- package/src/__tests__/alphabeticalScripts.spec.ts +76 -0
- package/src/__tests__/bannedDependencies.spec.ts +191 -0
- package/src/__tests__/consistentDependencies.spec.ts +41 -27
- package/src/__tests__/consistentVersions.spec.ts +224 -0
- package/src/__tests__/fileContents.spec.ts +75 -0
- package/src/__tests__/mustSatisfyPeerDependencies.spec.ts +1189 -0
- package/src/__tests__/nestedWorkspaces.spec.ts +153 -0
- package/src/__tests__/packageEntry.spec.ts +99 -31
- package/src/__tests__/packageOrder.spec.ts +48 -41
- package/src/__tests__/packageScript.spec.ts +66 -56
- package/src/__tests__/requireDependency.spec.ts +152 -0
- package/src/__tests__/utils.ts +115 -11
- package/src/alphabeticalDependencies.ts +6 -48
- package/src/alphabeticalScripts.ts +21 -0
- package/src/bannedDependencies.ts +135 -44
- package/src/consistentDependencies.ts +38 -14
- package/src/consistentVersions.ts +142 -0
- package/src/fileContents.ts +35 -30
- package/src/index.ts +13 -8
- package/src/mustSatisfyPeerDependencies.ts +748 -0
- package/src/nestedWorkspaces.ts +61 -0
- package/src/packageEntry.ts +72 -27
- package/src/packageOrder.ts +13 -9
- package/src/packageScript.ts +13 -15
- package/src/requireDependency.ts +71 -0
- package/src/standardTsconfig.ts +49 -24
- package/src/util/checkAlpha.ts +59 -0
- package/src/util/createNewRuleConversion.ts +38 -0
- package/src/util/makeDirectory.ts +24 -0
- package/src/util/packageDependencyGraphService.ts +114 -0
- package/tsconfig.json +10 -2
- package/lib/__tests__/consistentDependencies.spec.d.ts.map +0 -1
- package/lib/__tests__/consistentDependencies.spec.js +0 -108
- package/lib/__tests__/consistentDependencies.spec.js.map +0 -1
- package/lib/__tests__/packageEntry.spec.d.ts.map +0 -1
- package/lib/__tests__/packageEntry.spec.js +0 -99
- package/lib/__tests__/packageEntry.spec.js.map +0 -1
- package/lib/__tests__/packageOrder.spec.d.ts.map +0 -1
- package/lib/__tests__/packageOrder.spec.js +0 -115
- package/lib/__tests__/packageOrder.spec.js.map +0 -1
- package/lib/__tests__/packageScript.spec.d.ts.map +0 -1
- package/lib/__tests__/packageScript.spec.js +0 -172
- package/lib/__tests__/packageScript.spec.js.map +0 -1
- package/lib/__tests__/utils.d.ts.map +0 -1
- package/lib/__tests__/utils.js +0 -23
- package/lib/__tests__/utils.js.map +0 -1
- package/lib/alphabeticalDependencies.d.ts +0 -10
- package/lib/alphabeticalDependencies.d.ts.map +0 -1
- package/lib/alphabeticalDependencies.js +0 -56
- package/lib/alphabeticalDependencies.js.map +0 -1
- package/lib/bannedDependencies.d.ts +0 -15
- package/lib/bannedDependencies.d.ts.map +0 -1
- package/lib/bannedDependencies.js +0 -57
- package/lib/bannedDependencies.js.map +0 -1
- package/lib/consistentDependencies.d.ts +0 -10
- package/lib/consistentDependencies.d.ts.map +0 -1
- package/lib/consistentDependencies.js +0 -57
- package/lib/consistentDependencies.js.map +0 -1
- package/lib/fileContents.d.ts +0 -25
- package/lib/fileContents.d.ts.map +0 -1
- package/lib/fileContents.js +0 -77
- package/lib/fileContents.js.map +0 -1
- package/lib/index.d.ts +0 -15
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -25
- package/lib/index.js.map +0 -1
- package/lib/packageEntry.d.ts +0 -16
- package/lib/packageEntry.d.ts.map +0 -1
- package/lib/packageEntry.js +0 -40
- package/lib/packageEntry.js.map +0 -1
- package/lib/packageOrder.d.ts +0 -12
- package/lib/packageOrder.d.ts.map +0 -1
- package/lib/packageOrder.js +0 -103
- package/lib/packageOrder.js.map +0 -1
- package/lib/packageScript.d.ts +0 -25
- package/lib/packageScript.d.ts.map +0 -1
- package/lib/packageScript.js +0 -89
- package/lib/packageScript.js.map +0 -1
- package/lib/standardTsconfig.d.ts +0 -33
- package/lib/standardTsconfig.d.ts.map +0 -1
- package/lib/standardTsconfig.js +0 -98
- package/lib/standardTsconfig.js.map +0 -1
- package/tsconfig.tsbuildinfo +0 -2439
|
@@ -6,13 +6,10 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
// tslint:disable:no-console
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import { Failure, PackageContext } from "@monorepolint/core";
|
|
15
|
-
import { packageScript } from "../packageScript";
|
|
9
|
+
import { describe, expect, it, beforeEach, jest } from "@jest/globals";
|
|
10
|
+
import { Context, Failure } from "@monorepolint/config";
|
|
11
|
+
import { packageScript } from "../packageScript.js";
|
|
12
|
+
import { AddErrorSpy, createTestingWorkspace, HOST_FACTORIES, TestingWorkspace } from "./utils.js";
|
|
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,23 @@ 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(
|
|
124
|
+
`Expected standardized script entry for '${MISSING_SCRIPT_NAME}'`
|
|
125
|
+
) as any as string,
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts[MISSING_SCRIPT_NAME]).toEqual(
|
|
130
|
+
MISSING_SCRIPT_VALUE
|
|
131
|
+
);
|
|
122
132
|
});
|
|
123
133
|
|
|
124
134
|
it("does nothing if the value exists", () => {
|
|
125
|
-
|
|
135
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
126
136
|
|
|
127
137
|
packageScript.check(context, {
|
|
128
138
|
scripts: {
|
|
@@ -132,13 +142,13 @@ describe("expectPackageScript", () => {
|
|
|
132
142
|
|
|
133
143
|
expect(spy).not.toHaveBeenCalled();
|
|
134
144
|
|
|
135
|
-
expect(JSON.parse(
|
|
145
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({
|
|
136
146
|
[SCRIPT_NAME]: SCRIPT_VALUE,
|
|
137
147
|
});
|
|
138
148
|
});
|
|
139
149
|
|
|
140
150
|
it("errors if long form is used and no value matches and there is no fixValue", () => {
|
|
141
|
-
|
|
151
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
142
152
|
|
|
143
153
|
packageScript.check(context, {
|
|
144
154
|
scripts: {
|
|
@@ -155,7 +165,7 @@ describe("expectPackageScript", () => {
|
|
|
155
165
|
});
|
|
156
166
|
|
|
157
167
|
it("uses the fixValue for fixing if provided", () => {
|
|
158
|
-
|
|
168
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
159
169
|
|
|
160
170
|
packageScript.check(context, {
|
|
161
171
|
scripts: {
|
|
@@ -171,14 +181,14 @@ describe("expectPackageScript", () => {
|
|
|
171
181
|
expect(errors.length).toBe(1);
|
|
172
182
|
expect(errors[0][0].fixer).toBeDefined();
|
|
173
183
|
|
|
174
|
-
expect(JSON.parse(
|
|
184
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({
|
|
175
185
|
[SCRIPT_NAME]: SCRIPT_VALUE,
|
|
176
186
|
foo: "a",
|
|
177
187
|
});
|
|
178
188
|
});
|
|
179
189
|
|
|
180
190
|
it("can fix to empty", () => {
|
|
181
|
-
|
|
191
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
182
192
|
|
|
183
193
|
packageScript.check(context, {
|
|
184
194
|
scripts: {
|
|
@@ -194,11 +204,11 @@ describe("expectPackageScript", () => {
|
|
|
194
204
|
expect(errors.length).toBe(1);
|
|
195
205
|
expect(errors[0][0].fixer).toBeDefined();
|
|
196
206
|
|
|
197
|
-
expect(JSON.parse(
|
|
207
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({});
|
|
198
208
|
});
|
|
199
209
|
|
|
200
210
|
it("can allow only empty", () => {
|
|
201
|
-
|
|
211
|
+
workspace.writeFile("package.json", PACKAGE_WITH_SCRIPTS);
|
|
202
212
|
|
|
203
213
|
packageScript.check(context, {
|
|
204
214
|
scripts: {
|
|
@@ -214,7 +224,7 @@ describe("expectPackageScript", () => {
|
|
|
214
224
|
expect(errors.length).toBe(1);
|
|
215
225
|
expect(errors[0][0].fixer).toBeDefined();
|
|
216
226
|
|
|
217
|
-
expect(JSON.parse(
|
|
227
|
+
expect(JSON.parse(workspace.readFile("package.json")!).scripts).toEqual({});
|
|
218
228
|
});
|
|
219
229
|
});
|
|
220
230
|
});
|
|
@@ -0,0 +1,152 @@
|
|
|
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 { WorkspaceContextImpl } from "@monorepolint/core";
|
|
8
|
+
import { SimpleHost } from "@monorepolint/utils";
|
|
9
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import * as tmp from "tmp";
|
|
12
|
+
import { requireDependency } from "../requireDependency.js";
|
|
13
|
+
import { makeDirectoryRecursively } from "../util/makeDirectory.js";
|
|
14
|
+
import { jsonToString } from "./utils.js";
|
|
15
|
+
import { describe, expect, it, afterEach, jest } from "@jest/globals";
|
|
16
|
+
|
|
17
|
+
const PACKAGE_ROOT = jsonToString({
|
|
18
|
+
workspaces: {
|
|
19
|
+
packages: ["packages/*"],
|
|
20
|
+
},
|
|
21
|
+
dependencies: {
|
|
22
|
+
foo: "5",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const PACKAGE_WITH_NO_ENTRIES = jsonToString({});
|
|
27
|
+
|
|
28
|
+
const PACKAGE_WITH_ENTRIES_MISSING = jsonToString({
|
|
29
|
+
dependencies: {},
|
|
30
|
+
devDependencies: {},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const PACKAGE_WITH_WRONG_ENTRIES = jsonToString({
|
|
34
|
+
dependencies: {
|
|
35
|
+
foo: "0.1.0",
|
|
36
|
+
},
|
|
37
|
+
devDependencies: {
|
|
38
|
+
bar: "1.0.0",
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const PACKAGE_WITH_RIGHT_ENTRIES = jsonToString({
|
|
43
|
+
dependencies: {
|
|
44
|
+
foo: "1.0.0",
|
|
45
|
+
},
|
|
46
|
+
devDependencies: {
|
|
47
|
+
bar: "^2.0.0",
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const OPTIONS = {
|
|
52
|
+
dependencies: {
|
|
53
|
+
foo: "1.0.0",
|
|
54
|
+
},
|
|
55
|
+
devDependencies: {
|
|
56
|
+
bar: "^2.0.0",
|
|
57
|
+
},
|
|
58
|
+
} as const;
|
|
59
|
+
|
|
60
|
+
const CORRECT_OUTPUT = jsonToString(OPTIONS);
|
|
61
|
+
|
|
62
|
+
describe("requireDependency", () => {
|
|
63
|
+
tmp.setGracefulCleanup();
|
|
64
|
+
|
|
65
|
+
let cleanupJobs: Array<() => void> = [];
|
|
66
|
+
|
|
67
|
+
afterEach(() => {
|
|
68
|
+
for (const cleanupJob of cleanupJobs) {
|
|
69
|
+
cleanupJob();
|
|
70
|
+
}
|
|
71
|
+
cleanupJobs = [];
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
function makeWorkspace({ fix }: { fix: boolean }) {
|
|
75
|
+
const dir: tmp.DirResult = tmp.dirSync({ unsafeCleanup: true });
|
|
76
|
+
cleanupJobs.push(() => dir.removeCallback());
|
|
77
|
+
|
|
78
|
+
const workspaceContext = new WorkspaceContextImpl(
|
|
79
|
+
dir.name,
|
|
80
|
+
{
|
|
81
|
+
rules: [],
|
|
82
|
+
fix,
|
|
83
|
+
verbose: false,
|
|
84
|
+
silent: true,
|
|
85
|
+
},
|
|
86
|
+
new SimpleHost()
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
function checkAndSpy(q: string) {
|
|
90
|
+
const context = workspaceContext.createChildContext(path.resolve(dir.name, q));
|
|
91
|
+
const addErrorSpy = jest.spyOn(context, "addError");
|
|
92
|
+
requireDependency.check(context, OPTIONS);
|
|
93
|
+
return { context, addErrorSpy };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function addFile(filePath: string, content: string) {
|
|
97
|
+
const dirPath = path.resolve(dir.name, path.dirname(filePath));
|
|
98
|
+
const resolvedFilePath = path.resolve(dir.name, filePath);
|
|
99
|
+
|
|
100
|
+
makeDirectoryRecursively(dirPath);
|
|
101
|
+
writeFileSync(resolvedFilePath, content);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readFile(filePath: string) {
|
|
105
|
+
return readFileSync(path.resolve(dir.name, filePath)).toString();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return { addFile, readFile, workspaceContext, checkAndSpy };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
it("checks correctly", () => {
|
|
112
|
+
const { addFile, workspaceContext, checkAndSpy } = makeWorkspace({ fix: false });
|
|
113
|
+
addFile("./package.json", PACKAGE_ROOT);
|
|
114
|
+
addFile("./packages/none/package.json", PACKAGE_WITH_NO_ENTRIES);
|
|
115
|
+
addFile("./packages/missing/package.json", PACKAGE_WITH_ENTRIES_MISSING);
|
|
116
|
+
addFile("./packages/wrong/package.json", PACKAGE_WITH_WRONG_ENTRIES);
|
|
117
|
+
addFile("./packages/right/package.json", PACKAGE_WITH_RIGHT_ENTRIES);
|
|
118
|
+
|
|
119
|
+
requireDependency.check(workspaceContext, OPTIONS);
|
|
120
|
+
|
|
121
|
+
const none = checkAndSpy("./packages/none");
|
|
122
|
+
expect(none.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
123
|
+
|
|
124
|
+
const missing = checkAndSpy("./packages/missing");
|
|
125
|
+
expect(missing.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
126
|
+
|
|
127
|
+
const wrong = checkAndSpy("./packages/wrong");
|
|
128
|
+
expect(wrong.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
129
|
+
|
|
130
|
+
const right = checkAndSpy("./packages/right");
|
|
131
|
+
expect(right.addErrorSpy).toHaveBeenCalledTimes(0);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("fixes correctly", () => {
|
|
135
|
+
const { addFile, readFile, checkAndSpy } = makeWorkspace({ fix: true });
|
|
136
|
+
addFile("./package.json", PACKAGE_ROOT);
|
|
137
|
+
addFile("./packages/missing/package.json", PACKAGE_WITH_ENTRIES_MISSING);
|
|
138
|
+
addFile("./packages/wrong/package.json", PACKAGE_WITH_WRONG_ENTRIES);
|
|
139
|
+
|
|
140
|
+
const missing = checkAndSpy("./packages/missing");
|
|
141
|
+
expect(missing.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
142
|
+
|
|
143
|
+
const wrong = checkAndSpy("./packages/wrong");
|
|
144
|
+
expect(wrong.addErrorSpy).toHaveBeenCalledTimes(2);
|
|
145
|
+
|
|
146
|
+
const missingContents = readFile("./packages/missing/package.json");
|
|
147
|
+
expect(missingContents).toEqual(CORRECT_OUTPUT);
|
|
148
|
+
|
|
149
|
+
const contents = readFile("./packages/wrong/package.json");
|
|
150
|
+
expect(contents).toEqual(CORRECT_OUTPUT);
|
|
151
|
+
});
|
|
152
|
+
});
|
package/src/__tests__/utils.ts
CHANGED
|
@@ -5,19 +5,123 @@
|
|
|
5
5
|
*
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
import { AddErrorOptions, WorkspaceContext } from "@monorepolint/config";
|
|
9
|
+
import { WorkspaceContextImpl } from "@monorepolint/core";
|
|
10
|
+
import { CachingHost, Host, SimpleHost } from "@monorepolint/utils";
|
|
11
|
+
import { expect, jest } from "@jest/globals";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
import * as tmp from "tmp";
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
export function jsonToString(obj: unknown) {
|
|
16
|
+
return JSON.stringify(obj, undefined, 2) + "\n";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface TestingWorkspaceOpts {
|
|
20
|
+
host: Host;
|
|
21
|
+
rootProjectName?: string;
|
|
22
|
+
fixFlag: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function createTestingWorkspace(inboundOpts: TestingWorkspaceOpts) {
|
|
26
|
+
tmp.setGracefulCleanup();
|
|
27
|
+
const tmpdir = tmp.dirSync();
|
|
28
|
+
const opts = {
|
|
29
|
+
...inboundOpts,
|
|
30
|
+
rootProjectName: inboundOpts.rootProjectName ?? "rootProject",
|
|
31
|
+
};
|
|
32
|
+
const rootPath = tmpdir.name;
|
|
33
|
+
opts.host.mkdir(rootPath, { recursive: true });
|
|
15
34
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
35
|
+
opts.host.writeJson(path.join(rootPath, "package.json"), {
|
|
36
|
+
name: opts.rootProjectName,
|
|
37
|
+
workspaces: {
|
|
38
|
+
packages: ["packages/*"],
|
|
19
39
|
},
|
|
20
|
-
})
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await opts.host.flush();
|
|
21
43
|
|
|
22
|
-
return
|
|
44
|
+
return new DefaultTestingWorkspace(
|
|
45
|
+
{ ...opts, rootPath },
|
|
46
|
+
new WorkspaceContextImpl(rootPath, { fix: opts.fixFlag, rules: [] }, opts.host)
|
|
47
|
+
);
|
|
23
48
|
}
|
|
49
|
+
|
|
50
|
+
interface RealTestingWorkspaceOpts extends Required<TestingWorkspaceOpts> {
|
|
51
|
+
rootPath: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface TestingWorkspace {
|
|
55
|
+
/**
|
|
56
|
+
* Adds a utf8 file to `packageName` with `relativePath` to the package directory.
|
|
57
|
+
*
|
|
58
|
+
* @param packageName The child package to add to or undefined for root package
|
|
59
|
+
* @param filePath The path of the file to be written. Will be prepended with package dir
|
|
60
|
+
* @param contents the contents to be written (as utf8)
|
|
61
|
+
*/
|
|
62
|
+
writeFile(filePath: string, contents: string): void;
|
|
63
|
+
|
|
64
|
+
writeJsonFile(filePath: string, json: object): void;
|
|
65
|
+
addProject(name: string, fields: object): void;
|
|
66
|
+
getFilePath(filePath: string): string;
|
|
67
|
+
readFile(filePath: string): string;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Helper method for matching failures via jest `expect().toMatchObject
|
|
71
|
+
*
|
|
72
|
+
*/
|
|
73
|
+
failureMatcher(opts: { file: string; message: string; hasFixer: boolean }): any;
|
|
74
|
+
|
|
75
|
+
readonly context: WorkspaceContext;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
class DefaultTestingWorkspace implements TestingWorkspace {
|
|
79
|
+
constructor(private opts: RealTestingWorkspaceOpts, public readonly context: WorkspaceContext) {}
|
|
80
|
+
|
|
81
|
+
addProject(name: string, fields: object) {
|
|
82
|
+
this.writeJsonFile(path.join("packages", name, "package.json"), {
|
|
83
|
+
name,
|
|
84
|
+
...fields,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
writeJsonFile(filePath: string, json: object) {
|
|
89
|
+
this.writeFile(filePath, JSON.stringify(json, undefined, 2));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Adds a utf8 file to `packageName` with `relativePath` to the package directory.
|
|
94
|
+
*
|
|
95
|
+
* @param packageName The child package to add to or undefined for root package
|
|
96
|
+
* @param filePath The path of the file to be written. Will be prepended with package dir
|
|
97
|
+
* @param contents the contents to be written (as utf8)
|
|
98
|
+
*/
|
|
99
|
+
writeFile(filePath: string, contents: string) {
|
|
100
|
+
const fullFilePath = this.getFilePath(filePath);
|
|
101
|
+
this.opts.host.mkdir(path.dirname(fullFilePath), { recursive: true });
|
|
102
|
+
this.opts.host.writeFile(fullFilePath, contents, { encoding: "utf-8" });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getFilePath(filePath: string) {
|
|
106
|
+
return path.join(this.opts.rootPath, filePath);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
readFile(filePath: string) {
|
|
110
|
+
return this.opts.host.readFile(this.getFilePath(filePath), { encoding: "utf-8" });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
failureMatcher(opts: { file: string; message: string; hasFixer: boolean }) {
|
|
114
|
+
return {
|
|
115
|
+
file: this.getFilePath(opts.file),
|
|
116
|
+
message: opts.message,
|
|
117
|
+
...(opts.hasFixer ? { fixer: expect.any(Function) } : {}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type AddErrorSpy = jest.SpiedFunction<(options: AddErrorOptions) => void>;
|
|
123
|
+
|
|
124
|
+
export const HOST_FACTORIES: Array<{ name: string; make: () => Host }> = [
|
|
125
|
+
{ name: "SimpleHost", make: () => new SimpleHost() },
|
|
126
|
+
{ name: "CachingHost", make: () => new CachingHost() },
|
|
127
|
+
];
|
|
@@ -5,61 +5,19 @@
|
|
|
5
5
|
*
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { Context, RuleModule } from "@monorepolint/
|
|
9
|
-
import { writeJson } from "@monorepolint/utils";
|
|
10
|
-
import diff from "jest-diff";
|
|
8
|
+
import { Context, RuleModule } from "@monorepolint/config";
|
|
11
9
|
import * as r from "runtypes";
|
|
12
|
-
|
|
10
|
+
import { checkAlpha } from "./util/checkAlpha.js";
|
|
11
|
+
import { createNewRuleConversion } from "./util/createNewRuleConversion.js";
|
|
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
|
-
}
|
|
21
|
+
};
|
|
63
22
|
|
|
64
|
-
|
|
65
|
-
}
|
|
23
|
+
export const AlphabeticalDependencies = createNewRuleConversion("AlphabetialDependencies", alphabeticalDependencies);
|
|
@@ -0,0 +1,21 @@
|
|
|
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/config";
|
|
9
|
+
import * as r from "runtypes";
|
|
10
|
+
import { checkAlpha } from "./util/checkAlpha.js";
|
|
11
|
+
import { createNewRuleConversion } from "./util/createNewRuleConversion.js";
|
|
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
|
+
};
|
|
20
|
+
|
|
21
|
+
export const AlphabeticalScripts = createNewRuleConversion("AlphabeticalScripts", alphabeticalScripts);
|