@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.
Files changed (155) hide show
  1. package/.turbo/turbo-clean.log +4 -0
  2. package/.turbo/turbo-compile-typescript.log +4 -0
  3. package/.turbo/turbo-lint.log +107 -0
  4. package/.turbo/turbo-test.log +631 -0
  5. package/.turbo/turbo-transpile-typescript.log +18 -0
  6. package/build/js/index.cjs +1491 -0
  7. package/build/js/index.cjs.map +1 -0
  8. package/build/js/index.js +1433 -0
  9. package/build/js/index.js.map +1 -0
  10. package/build/tsconfig.tsbuildinfo +1 -0
  11. package/{lib/__tests__/utils.d.ts → build/types/__tests__/alphabeticalScripts.spec.d.ts} +2 -2
  12. package/build/types/__tests__/alphabeticalScripts.spec.d.ts.map +1 -0
  13. package/build/types/__tests__/bannedDependencies.spec.d.ts +2 -0
  14. package/build/types/__tests__/bannedDependencies.spec.d.ts.map +1 -0
  15. package/{lib → build/types}/__tests__/consistentDependencies.spec.d.ts +0 -0
  16. package/build/types/__tests__/consistentDependencies.spec.d.ts.map +1 -0
  17. package/build/types/__tests__/consistentVersions.spec.d.ts +8 -0
  18. package/build/types/__tests__/consistentVersions.spec.d.ts.map +1 -0
  19. package/build/types/__tests__/fileContents.spec.d.ts +8 -0
  20. package/build/types/__tests__/fileContents.spec.d.ts.map +1 -0
  21. package/build/types/__tests__/mustSatisfyPeerDependencies.spec.d.ts +8 -0
  22. package/build/types/__tests__/mustSatisfyPeerDependencies.spec.d.ts.map +1 -0
  23. package/build/types/__tests__/nestedWorkspaces.spec.d.ts +2 -0
  24. package/build/types/__tests__/nestedWorkspaces.spec.d.ts.map +1 -0
  25. package/{lib → build/types}/__tests__/packageEntry.spec.d.ts +0 -0
  26. package/build/types/__tests__/packageEntry.spec.d.ts.map +1 -0
  27. package/{lib → build/types}/__tests__/packageOrder.spec.d.ts +0 -0
  28. package/build/types/__tests__/packageOrder.spec.d.ts.map +1 -0
  29. package/{lib → build/types}/__tests__/packageScript.spec.d.ts +0 -0
  30. package/build/types/__tests__/packageScript.spec.d.ts.map +1 -0
  31. package/build/types/__tests__/requireDependency.spec.d.ts +2 -0
  32. package/build/types/__tests__/requireDependency.spec.d.ts.map +1 -0
  33. package/build/types/__tests__/utils.d.ts +81 -0
  34. package/build/types/__tests__/utils.d.ts.map +1 -0
  35. package/build/types/alphabeticalDependencies.d.ts +23 -0
  36. package/build/types/alphabeticalDependencies.d.ts.map +1 -0
  37. package/build/types/alphabeticalScripts.d.ts +23 -0
  38. package/build/types/alphabeticalScripts.d.ts.map +1 -0
  39. package/build/types/bannedDependencies.d.ts +134 -0
  40. package/build/types/bannedDependencies.d.ts.map +1 -0
  41. package/build/types/consistentDependencies.d.ts +38 -0
  42. package/build/types/consistentDependencies.d.ts.map +1 -0
  43. package/build/types/consistentVersions.d.ts +47 -0
  44. package/build/types/consistentVersions.d.ts.map +1 -0
  45. package/build/types/fileContents.d.ts +111 -0
  46. package/build/types/fileContents.d.ts.map +1 -0
  47. package/build/types/index.d.ts +20 -0
  48. package/build/types/index.d.ts.map +1 -0
  49. package/build/types/mustSatisfyPeerDependencies.d.ts +721 -0
  50. package/build/types/mustSatisfyPeerDependencies.d.ts.map +1 -0
  51. package/build/types/nestedWorkspaces.d.ts +24 -0
  52. package/build/types/nestedWorkspaces.d.ts.map +1 -0
  53. package/build/types/packageEntry.d.ts +115 -0
  54. package/build/types/packageEntry.d.ts.map +1 -0
  55. package/build/types/packageOrder.d.ts +33 -0
  56. package/build/types/packageOrder.d.ts.map +1 -0
  57. package/build/types/packageScript.d.ts +89 -0
  58. package/build/types/packageScript.d.ts.map +1 -0
  59. package/build/types/requireDependency.d.ts +99 -0
  60. package/build/types/requireDependency.d.ts.map +1 -0
  61. package/build/types/standardTsconfig.d.ts +113 -0
  62. package/build/types/standardTsconfig.d.ts.map +1 -0
  63. package/build/types/util/checkAlpha.d.ts +10 -0
  64. package/build/types/util/checkAlpha.d.ts.map +1 -0
  65. package/build/types/util/createNewRuleConversion.d.ts +30 -0
  66. package/build/types/util/createNewRuleConversion.d.ts.map +1 -0
  67. package/build/types/util/makeDirectory.d.ts +8 -0
  68. package/build/types/util/makeDirectory.d.ts.map +1 -0
  69. package/build/types/util/packageDependencyGraphService.d.ts +37 -0
  70. package/build/types/util/packageDependencyGraphService.d.ts.map +1 -0
  71. package/{jest.config.js → jest.config.cjs} +0 -0
  72. package/package.json +39 -20
  73. package/src/__tests__/alphabeticalScripts.spec.ts +76 -0
  74. package/src/__tests__/bannedDependencies.spec.ts +191 -0
  75. package/src/__tests__/consistentDependencies.spec.ts +41 -27
  76. package/src/__tests__/consistentVersions.spec.ts +224 -0
  77. package/src/__tests__/fileContents.spec.ts +75 -0
  78. package/src/__tests__/mustSatisfyPeerDependencies.spec.ts +1189 -0
  79. package/src/__tests__/nestedWorkspaces.spec.ts +153 -0
  80. package/src/__tests__/packageEntry.spec.ts +99 -31
  81. package/src/__tests__/packageOrder.spec.ts +48 -41
  82. package/src/__tests__/packageScript.spec.ts +66 -56
  83. package/src/__tests__/requireDependency.spec.ts +152 -0
  84. package/src/__tests__/utils.ts +115 -11
  85. package/src/alphabeticalDependencies.ts +6 -48
  86. package/src/alphabeticalScripts.ts +21 -0
  87. package/src/bannedDependencies.ts +135 -44
  88. package/src/consistentDependencies.ts +38 -14
  89. package/src/consistentVersions.ts +142 -0
  90. package/src/fileContents.ts +35 -30
  91. package/src/index.ts +13 -8
  92. package/src/mustSatisfyPeerDependencies.ts +748 -0
  93. package/src/nestedWorkspaces.ts +61 -0
  94. package/src/packageEntry.ts +72 -27
  95. package/src/packageOrder.ts +13 -9
  96. package/src/packageScript.ts +13 -15
  97. package/src/requireDependency.ts +71 -0
  98. package/src/standardTsconfig.ts +49 -24
  99. package/src/util/checkAlpha.ts +59 -0
  100. package/src/util/createNewRuleConversion.ts +38 -0
  101. package/src/util/makeDirectory.ts +24 -0
  102. package/src/util/packageDependencyGraphService.ts +114 -0
  103. package/tsconfig.json +10 -2
  104. package/lib/__tests__/consistentDependencies.spec.d.ts.map +0 -1
  105. package/lib/__tests__/consistentDependencies.spec.js +0 -108
  106. package/lib/__tests__/consistentDependencies.spec.js.map +0 -1
  107. package/lib/__tests__/packageEntry.spec.d.ts.map +0 -1
  108. package/lib/__tests__/packageEntry.spec.js +0 -99
  109. package/lib/__tests__/packageEntry.spec.js.map +0 -1
  110. package/lib/__tests__/packageOrder.spec.d.ts.map +0 -1
  111. package/lib/__tests__/packageOrder.spec.js +0 -115
  112. package/lib/__tests__/packageOrder.spec.js.map +0 -1
  113. package/lib/__tests__/packageScript.spec.d.ts.map +0 -1
  114. package/lib/__tests__/packageScript.spec.js +0 -172
  115. package/lib/__tests__/packageScript.spec.js.map +0 -1
  116. package/lib/__tests__/utils.d.ts.map +0 -1
  117. package/lib/__tests__/utils.js +0 -23
  118. package/lib/__tests__/utils.js.map +0 -1
  119. package/lib/alphabeticalDependencies.d.ts +0 -10
  120. package/lib/alphabeticalDependencies.d.ts.map +0 -1
  121. package/lib/alphabeticalDependencies.js +0 -56
  122. package/lib/alphabeticalDependencies.js.map +0 -1
  123. package/lib/bannedDependencies.d.ts +0 -15
  124. package/lib/bannedDependencies.d.ts.map +0 -1
  125. package/lib/bannedDependencies.js +0 -57
  126. package/lib/bannedDependencies.js.map +0 -1
  127. package/lib/consistentDependencies.d.ts +0 -10
  128. package/lib/consistentDependencies.d.ts.map +0 -1
  129. package/lib/consistentDependencies.js +0 -57
  130. package/lib/consistentDependencies.js.map +0 -1
  131. package/lib/fileContents.d.ts +0 -25
  132. package/lib/fileContents.d.ts.map +0 -1
  133. package/lib/fileContents.js +0 -77
  134. package/lib/fileContents.js.map +0 -1
  135. package/lib/index.d.ts +0 -15
  136. package/lib/index.d.ts.map +0 -1
  137. package/lib/index.js +0 -25
  138. package/lib/index.js.map +0 -1
  139. package/lib/packageEntry.d.ts +0 -16
  140. package/lib/packageEntry.d.ts.map +0 -1
  141. package/lib/packageEntry.js +0 -40
  142. package/lib/packageEntry.js.map +0 -1
  143. package/lib/packageOrder.d.ts +0 -12
  144. package/lib/packageOrder.d.ts.map +0 -1
  145. package/lib/packageOrder.js +0 -103
  146. package/lib/packageOrder.js.map +0 -1
  147. package/lib/packageScript.d.ts +0 -25
  148. package/lib/packageScript.d.ts.map +0 -1
  149. package/lib/packageScript.js +0 -89
  150. package/lib/packageScript.js.map +0 -1
  151. package/lib/standardTsconfig.d.ts +0 -33
  152. package/lib/standardTsconfig.d.ts.map +0 -1
  153. package/lib/standardTsconfig.js +0 -98
  154. package/lib/standardTsconfig.js.map +0 -1
  155. package/tsconfig.tsbuildinfo +0 -2439
package/package.json CHANGED
@@ -1,38 +1,57 @@
1
1
  {
2
2
  "name": "@monorepolint/rules",
3
- "version": "0.5.0-alpha.10+b584a18",
3
+ "version": "0.5.0-alpha.103+241c1b6",
4
4
  "author": "Eric L Anderson (https://github.com/ericanderson)",
5
5
  "contributors": [
6
6
  "Mac Lockard (https://github.com/maclockard)"
7
7
  ],
8
8
  "url": "https://github.com/monorepolint/monorepolint",
9
9
  "license": "MIT",
10
- "main": "lib/index.js",
11
- "typings": "lib/index.d.ts",
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./build/types/index.d.ts",
14
+ "import": "./build/js/index.js",
15
+ "require": "./build/js/index.cjs"
16
+ }
17
+ },
12
18
  "scripts": {
13
- "clean": "rm -rf build lib node_modules *.tgz",
14
- "compile:typescript": "../../node_modules/.bin/tsc",
15
- "jest:watch": "../../node_modules/.bin/jest --config ../../jest.config.js --watch",
16
- "jest": "../../node_modules/.bin/jest --config ../../jest.config.js",
17
- "lint:typescript": "../../node_modules/.bin/tslint --config ../../tslint.json --project .",
18
- "test:watch": "../../node_modules/.bin/jest --colors --passWithNoTests --watch",
19
- "test": "../../node_modules/.bin/jest --colors --passWithNoTests"
19
+ "clean": "rm -rf build dist lib node_modules *.tgz tsconfig.tsbuildinfo",
20
+ "compile-typescript": "tsc --build",
21
+ "lint": "eslint .",
22
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --colors --passWithNoTests",
23
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --colors --passWithNoTests --watch",
24
+ "transpile-typescript": "tsup --config ../../tsup.config.cjs"
20
25
  },
21
26
  "dependencies": {
22
- "@monorepolint/core": "^0.5.0-alpha.10+b584a18",
23
- "@monorepolint/utils": "^0.5.0-alpha.10+b584a18",
24
- "jest-diff": "^24.9.0",
25
- "minimatch": "^3.0.4",
26
- "runtypes": "^4.0.0"
27
+ "@monorepolint/config": "^0.5.0-alpha.103+241c1b6",
28
+ "@monorepolint/core": "^0.5.0-alpha.103+241c1b6",
29
+ "@monorepolint/utils": "^0.5.0-alpha.103+241c1b6",
30
+ "globby": "^13.1.2",
31
+ "jest-diff": "^29.3.1",
32
+ "resolve-package-path": "^4.0.3",
33
+ "runtypes": "^6.6.0",
34
+ "semver": "^7.3.8",
35
+ "tslib": "^2.3.1"
27
36
  },
28
37
  "devDependencies": {
29
- "@types/jest-diff": "^20.0.0",
30
- "@types/minimatch": "^3.0.3",
31
- "@types/tmp": "^0.1.0",
32
- "tmp": "^0.1.0"
38
+ "@jest/globals": "^29.3.1",
39
+ "@types/jest": "^29.2.4",
40
+ "@types/semver": "^7.3.13",
41
+ "@types/tmp": "^0.2.3",
42
+ "@typescript-eslint/eslint-plugin": "^5.45.1",
43
+ "@typescript-eslint/parser": "^5.45.1",
44
+ "eslint": "^8.29.0",
45
+ "jest": "^29.3.1",
46
+ "prettier": "^2.8.0",
47
+ "tmp": "^0.2.1",
48
+ "ts-jest": "^29.0.3",
49
+ "tslib": "^2.4.1",
50
+ "tsup": "^6.5.0",
51
+ "typescript": "^4.9.3"
33
52
  },
34
53
  "publishConfig": {
35
54
  "access": "public"
36
55
  },
37
- "gitHead": "b584a18e553330f84b64afd3a658342cab6c7234"
56
+ "gitHead": "241c1b6bcf55ee188dd2ee3f652de4c91e0817aa"
38
57
  }
@@ -0,0 +1,76 @@
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
+ // tslint:disable:no-console
9
+ import { describe, expect, it, beforeEach, jest } from "@jest/globals";
10
+ import { AddErrorSpy, createTestingWorkspace, HOST_FACTORIES, jsonToString, TestingWorkspace } from "./utils.js";
11
+ import { Context, Failure } from "@monorepolint/config";
12
+ import { alphabeticalScripts } from "../alphabeticalScripts.js";
13
+ import { createIncorrectOrderErrorMessage } from "../util/checkAlpha.js";
14
+
15
+ const PACKAGE_SCRIPTS_SORTED = jsonToString({
16
+ name: "foo-lib",
17
+ scripts: {
18
+ a: "a-",
19
+ b: "b-",
20
+ c: "c-",
21
+ },
22
+ });
23
+
24
+ const PACKAGE_SCRIPTS_UNSORTED = jsonToString({
25
+ name: "foo-lib",
26
+ scripts: {
27
+ c: "c-",
28
+ a: "a-",
29
+ b: "b-",
30
+ },
31
+ });
32
+
33
+ describe.each(HOST_FACTORIES)("alphabeticalScripts ($name)", (hostFactory) => {
34
+ describe("fix: true", () => {
35
+ let workspace: TestingWorkspace;
36
+ let spy: AddErrorSpy;
37
+ let context: Context;
38
+
39
+ beforeEach(async () => {
40
+ workspace = await createTestingWorkspace({
41
+ fixFlag: true,
42
+ host: hostFactory.make(),
43
+ });
44
+ context = workspace.context; // minimizing delta
45
+
46
+ spy = jest.spyOn(workspace.context, "addError");
47
+ });
48
+
49
+ it("fixes unsorted scripts", () => {
50
+ workspace.writeFile("package.json", PACKAGE_SCRIPTS_UNSORTED);
51
+
52
+ alphabeticalScripts.check(context, undefined);
53
+
54
+ expect(spy).toHaveBeenCalledTimes(1);
55
+
56
+ const failure: Failure = spy.mock.calls[0][0];
57
+ expect(failure).toMatchObject(
58
+ workspace.failureMatcher({
59
+ file: "package.json",
60
+ hasFixer: true,
61
+ message: createIncorrectOrderErrorMessage("scripts", "foo-lib"),
62
+ })
63
+ );
64
+
65
+ expect(workspace.readFile("package.json")).toEqual(PACKAGE_SCRIPTS_SORTED);
66
+ });
67
+
68
+ it("does nothing if already sorted", () => {
69
+ workspace.writeFile("package.json", PACKAGE_SCRIPTS_SORTED);
70
+
71
+ alphabeticalScripts.check(context, undefined);
72
+
73
+ expect(spy).toHaveBeenCalledTimes(0);
74
+ });
75
+ });
76
+ });
@@ -0,0 +1,191 @@
1
+ /*!
2
+ * Copyright 2020 Palantir Technologies, Inc.
3
+ *
4
+ * Licensed under the MIT license. See LICENSE file in the project root for details.
5
+ *
6
+ */
7
+ import { describe, expect, it, beforeEach, jest, afterEach } from "@jest/globals";
8
+ import { WorkspaceContextImpl } from "@monorepolint/core";
9
+ import { SimpleHost } from "@monorepolint/utils";
10
+ import { writeFileSync } from "fs";
11
+ import * as path from "path";
12
+ import * as tmp from "tmp";
13
+ import { bannedDependencies, Options } from "../bannedDependencies.js";
14
+ import { makeDirectoryRecursively } from "../util/makeDirectory.js";
15
+ import { jsonToString } from "./utils.js";
16
+ const EMPTY_PACKAGE = jsonToString({});
17
+
18
+ describe("bannedDependencies", () => {
19
+ tmp.setGracefulCleanup();
20
+
21
+ let cleanupJobs: Array<() => void> = [];
22
+ let cwd: string | undefined;
23
+
24
+ beforeEach(() => {
25
+ const dir = tmp.dirSync({ unsafeCleanup: true });
26
+ cleanupJobs.push(() => dir.removeCallback());
27
+ cwd = dir.name;
28
+
29
+ const spy = jest.spyOn(process, "cwd");
30
+ spy.mockReturnValue(cwd);
31
+ });
32
+
33
+ afterEach(() => {
34
+ for (const cleanupJob of cleanupJobs) {
35
+ cleanupJob();
36
+ }
37
+ cleanupJobs = [];
38
+ });
39
+
40
+ function makeWorkspace() {
41
+ const workspaceContext = new WorkspaceContextImpl(
42
+ cwd!,
43
+ {
44
+ rules: [],
45
+ fix: false,
46
+ verbose: false,
47
+ silent: true,
48
+ },
49
+ new SimpleHost()
50
+ );
51
+
52
+ function checkAndSpy(options: Options) {
53
+ const addErrorSpy = jest.spyOn(workspaceContext, "addError");
54
+ bannedDependencies.check(workspaceContext, {
55
+ ...options,
56
+ });
57
+ return { addErrorSpy };
58
+ }
59
+
60
+ function addFile(filePath: string, content: string) {
61
+ const dirPath = path.resolve(cwd!, path.dirname(filePath));
62
+ const resolvedFilePath = path.resolve(cwd!, filePath);
63
+
64
+ makeDirectoryRecursively(dirPath);
65
+ writeFileSync(resolvedFilePath, content);
66
+ return resolvedFilePath;
67
+ }
68
+
69
+ return { addFile, workspaceContext, checkAndSpy };
70
+ }
71
+
72
+ it("Flags banned dependencies correctly", async () => {
73
+ const { addFile, checkAndSpy } = makeWorkspace();
74
+ const rootPackageJson = jsonToString({
75
+ dependencies: {
76
+ aaa: "0.0.1",
77
+ ccc: "0.0.1",
78
+ },
79
+ });
80
+ addFile("./package.json", rootPackageJson);
81
+
82
+ const { addErrorSpy: addErrorSpy1 } = checkAndSpy({ bannedDependencies: ["ccc"] });
83
+ expect(addErrorSpy1).toHaveBeenCalledTimes(1);
84
+ addErrorSpy1.mockReset();
85
+
86
+ const { addErrorSpy: addErrorSpy2 } = checkAndSpy({ bannedDependencies: ["ddd"] });
87
+ expect(addErrorSpy2).toHaveBeenCalledTimes(0);
88
+ addErrorSpy2.mockReset();
89
+
90
+ const { addErrorSpy: addErrorSpy3 } = checkAndSpy({ bannedDependencies: ["ccc", "ddd"] });
91
+ expect(addErrorSpy3).toHaveBeenCalledTimes(1);
92
+ addErrorSpy3.mockReset();
93
+ });
94
+
95
+ it("Flags banned dependencies correctly w legacy globs", async () => {
96
+ const { addFile, checkAndSpy } = makeWorkspace();
97
+ const rootPackageJson = jsonToString({
98
+ dependencies: {
99
+ aaa: "0.0.1",
100
+ ccc: "0.0.1",
101
+ },
102
+ });
103
+ addFile("./package.json", rootPackageJson);
104
+
105
+ const { addErrorSpy: addErrorSpy1 } = checkAndSpy({ bannedDependencies: ["c*c"] });
106
+ expect(addErrorSpy1).toHaveBeenCalledTimes(1);
107
+ addErrorSpy1.mockReset();
108
+
109
+ const { addErrorSpy: addErrorSpy2 } = checkAndSpy({ bannedDependencies: ["d*d"] });
110
+ expect(addErrorSpy2).toHaveBeenCalledTimes(0);
111
+ addErrorSpy2.mockReset();
112
+
113
+ const { addErrorSpy: addErrorSpy3 } = checkAndSpy({ bannedDependencies: ["c*c", "d*d"] });
114
+ expect(addErrorSpy3).toHaveBeenCalledTimes(1);
115
+ addErrorSpy3.mockReset();
116
+ });
117
+
118
+ it("Flags banned dependencies correctly w new globs", async () => {
119
+ const { addFile, checkAndSpy } = makeWorkspace();
120
+ const rootPackageJson = jsonToString({
121
+ dependencies: {
122
+ aaa: "0.0.1",
123
+ ccc: "0.0.1",
124
+ },
125
+ });
126
+ addFile("./package.json", rootPackageJson);
127
+
128
+ const { addErrorSpy: addErrorSpy1 } = checkAndSpy({ bannedDependencies: { glob: ["c*c"] } });
129
+ expect(addErrorSpy1).toHaveBeenCalledTimes(1);
130
+ addErrorSpy1.mockReset();
131
+
132
+ const { addErrorSpy: addErrorSpy2 } = checkAndSpy({ bannedDependencies: { glob: ["d*d"] } });
133
+ expect(addErrorSpy2).toHaveBeenCalledTimes(0);
134
+ addErrorSpy2.mockReset();
135
+
136
+ const { addErrorSpy: addErrorSpy3 } = checkAndSpy({ bannedDependencies: { glob: ["c*c", "d*d"] } });
137
+ expect(addErrorSpy3).toHaveBeenCalledTimes(1);
138
+ addErrorSpy3.mockReset();
139
+ });
140
+
141
+ it("Flags banned dependencies correctly w exact", async () => {
142
+ const { addFile, checkAndSpy } = makeWorkspace();
143
+ const rootPackageJson = jsonToString({
144
+ dependencies: {
145
+ aaa: "0.0.1",
146
+ ccc: "0.0.1",
147
+ },
148
+ });
149
+ addFile("./package.json", rootPackageJson);
150
+
151
+ const { addErrorSpy: addErrorSpy1 } = checkAndSpy({ bannedDependencies: { exact: ["ccc"] } });
152
+ expect(addErrorSpy1).toHaveBeenCalledTimes(1);
153
+ addErrorSpy1.mockReset();
154
+
155
+ const { addErrorSpy: addErrorSpy2 } = checkAndSpy({ bannedDependencies: { exact: ["ddd"] } });
156
+ expect(addErrorSpy2).toHaveBeenCalledTimes(0);
157
+ addErrorSpy2.mockReset();
158
+
159
+ const { addErrorSpy: addErrorSpy3 } = checkAndSpy({ bannedDependencies: { exact: ["ccc", "ddd"] } });
160
+ expect(addErrorSpy3).toHaveBeenCalledTimes(1);
161
+ addErrorSpy3.mockReset();
162
+ });
163
+
164
+ it("Flags banned transitives correctly", async () => {
165
+ const { addFile, checkAndSpy } = makeWorkspace();
166
+ const rootPackageJson = jsonToString({
167
+ dependencies: {
168
+ aaa: "0.0.1",
169
+ },
170
+ });
171
+ addFile("./package.json", rootPackageJson);
172
+
173
+ const aaaPackageJson = jsonToString({
174
+ dependencies: {
175
+ bbb: "0.0.1",
176
+ ccc: "0.0.1",
177
+ },
178
+ });
179
+ addFile("./node_modules/aaa/package.json", aaaPackageJson);
180
+ const bbbPackageJson = jsonToString({
181
+ dependencies: {
182
+ ddd: "0.0.1",
183
+ },
184
+ });
185
+ addFile("./node_modules/aaa/node_modules/bbb/package.json", bbbPackageJson);
186
+ addFile("./node_modules/aaa/node_modules/bbb/node_modules/ddd/package.json", EMPTY_PACKAGE);
187
+ addFile("./node_modules/aaa/node_modules/ccc/package.json", EMPTY_PACKAGE);
188
+
189
+ expect(checkAndSpy({ bannedTransitiveDependencies: ["ccc", "ddd"] }).addErrorSpy).toHaveBeenCalledTimes(2);
190
+ });
191
+ });
@@ -4,15 +4,15 @@
4
4
  * Licensed under the MIT license. See LICENSE file in the project root for details.
5
5
  *
6
6
  */
7
- import { WorkspaceContext } from "@monorepolint/core";
8
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
7
+ import { WorkspaceContextImpl } from "@monorepolint/core";
8
+ import { SimpleHost } from "@monorepolint/utils";
9
+ import { readFileSync, writeFileSync } from "fs";
9
10
  import * as path from "path";
10
11
  import * as tmp from "tmp";
11
- import { consistentDependencies } from "../consistentDependencies";
12
-
13
- function jsonToString(obj: {}) {
14
- return JSON.stringify(obj, undefined, 2) + "\n";
15
- }
12
+ import { consistentDependencies, Options } from "../consistentDependencies.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
16
 
17
17
  const PACKAGE_ROOT = jsonToString({
18
18
  workspaces: {
@@ -29,6 +29,12 @@ const PACKAGE_CHILD_WITH_STAR = jsonToString({
29
29
  },
30
30
  });
31
31
 
32
+ const PACKAGE_CHILD_WITH_LATEST = jsonToString({
33
+ dependencies: {
34
+ foo: "latest",
35
+ },
36
+ });
37
+
32
38
  const PACKAGE_CHILD_WITH_RIGHT_VERSION = jsonToString({
33
39
  dependencies: {
34
40
  foo: "5",
@@ -54,19 +60,24 @@ describe("consistentDependencies", () => {
54
60
  });
55
61
 
56
62
  function makeWorkspace({ fix }: { fix: boolean }) {
57
- const dir: tmp.DirResult = tmp.dirSync();
63
+ const dir: tmp.DirResult = tmp.dirSync({ unsafeCleanup: true });
58
64
  cleanupJobs.push(() => dir.removeCallback());
59
65
 
60
- const workspaceContext = new WorkspaceContext(dir.name, {
61
- rules: [],
62
- fix,
63
- verbose: false,
64
- });
65
-
66
- function checkAndSpy(q: string) {
66
+ const workspaceContext = new WorkspaceContextImpl(
67
+ dir.name,
68
+ {
69
+ rules: [],
70
+ fix,
71
+ verbose: false,
72
+ silent: true,
73
+ },
74
+ new SimpleHost()
75
+ );
76
+
77
+ function checkAndSpy(q: string, opts?: Options) {
67
78
  const context = workspaceContext.createChildContext(path.resolve(dir.name, q));
68
79
  const addErrorSpy = jest.spyOn(context, "addError");
69
- consistentDependencies.check(context, undefined);
80
+ consistentDependencies.check(context, opts);
70
81
  return { context, addErrorSpy };
71
82
  }
72
83
 
@@ -74,17 +85,7 @@ describe("consistentDependencies", () => {
74
85
  const dirPath = path.resolve(dir.name, path.dirname(filePath));
75
86
  const resolvedFilePath = path.resolve(dir.name, filePath);
76
87
 
77
- // node < 10 doesn't support mkdirSync w/ recursive: true
78
- // so we manually do it instead
79
- const dirSegments = dirPath.split(path.sep);
80
- for (let i = 2; i <= dirSegments.length; i++) {
81
- // we skip the empty segment
82
- const curDirPath = dirSegments.slice(0, i).join(path.sep);
83
- if (!existsSync(curDirPath)) {
84
- mkdirSync(curDirPath);
85
- }
86
- }
87
-
88
+ makeDirectoryRecursively(dirPath);
88
89
  writeFileSync(resolvedFilePath, content);
89
90
  }
90
91
 
@@ -99,6 +100,7 @@ describe("consistentDependencies", () => {
99
100
  const { addFile, workspaceContext, checkAndSpy } = makeWorkspace({ fix: false });
100
101
  addFile("./package.json", PACKAGE_ROOT);
101
102
  addFile("./packages/star/package.json", PACKAGE_CHILD_WITH_STAR);
103
+ addFile("./packages/latest/package.json", PACKAGE_CHILD_WITH_LATEST);
102
104
  addFile("./packages/right/package.json", PACKAGE_CHILD_WITH_RIGHT_VERSION);
103
105
  addFile("./packages/wrong/package.json", PACKAGE_CHILD_WITH_WRONG_VERSION);
104
106
 
@@ -107,6 +109,9 @@ describe("consistentDependencies", () => {
107
109
  const star = checkAndSpy("./packages/star");
108
110
  expect(star.addErrorSpy).toHaveBeenCalledTimes(0);
109
111
 
112
+ const latest = checkAndSpy("./packages/latest");
113
+ expect(latest.addErrorSpy).toHaveBeenCalledTimes(0);
114
+
110
115
  const right = checkAndSpy("./packages/right");
111
116
  expect(right.addErrorSpy).toHaveBeenCalledTimes(0);
112
117
 
@@ -125,4 +130,13 @@ describe("consistentDependencies", () => {
125
130
  const contents = readFile("./packages/wrong/package.json");
126
131
  expect(contents).toEqual(PACKAGE_CHILD_WITH_RIGHT_VERSION);
127
132
  });
133
+
134
+ it("ignores ignored dependencies", () => {
135
+ const { addFile, checkAndSpy } = makeWorkspace({ fix: false });
136
+ addFile("./package.json", PACKAGE_ROOT);
137
+ addFile("./packages/wrong/package.json", PACKAGE_CHILD_WITH_WRONG_VERSION);
138
+
139
+ const ignored = checkAndSpy("./packages/wrong", { ignoredDependencies: ["foo"] });
140
+ expect(ignored.addErrorSpy).toHaveBeenCalledTimes(0);
141
+ });
128
142
  });
@@ -0,0 +1,224 @@
1
+ /*!
2
+ * Copyright 2020 Palantir Technologies, Inc.
3
+ *
4
+ * Licensed under the MIT license. See LICENSE file in the project root for details.
5
+ *
6
+ */
7
+
8
+ import { WorkspaceContextImpl } from "@monorepolint/core";
9
+ import { Host, PackageJson, SimpleHost } from "@monorepolint/utils";
10
+ import * as path from "path";
11
+ import * as tmp from "tmp";
12
+ import { consistentVersions, Options } from "../consistentVersions.js";
13
+ import { makeDirectoryRecursively } from "../util/makeDirectory.js";
14
+ import { describe, expect, it, beforeEach, afterEach, jest } from "@jest/globals";
15
+
16
+ describe("consistentVersions", () => {
17
+ tmp.setGracefulCleanup();
18
+
19
+ let cleanupJobs: Array<() => void> = [];
20
+ let cwd: string | undefined;
21
+
22
+ beforeEach(() => {
23
+ const dir = tmp.dirSync({ unsafeCleanup: true });
24
+ cleanupJobs.push(() => dir.removeCallback());
25
+ cwd = dir.name;
26
+
27
+ const spy = jest.spyOn(process, "cwd");
28
+ spy.mockReturnValue(cwd);
29
+ });
30
+
31
+ afterEach(() => {
32
+ for (const cleanupJob of cleanupJobs) {
33
+ cleanupJob();
34
+ }
35
+ cleanupJobs = [];
36
+ });
37
+
38
+ function makeWorkspace(fix = false) {
39
+ const host: Host = new SimpleHost();
40
+ const workspaceContext = new WorkspaceContextImpl(
41
+ cwd!,
42
+ {
43
+ rules: [],
44
+ fix,
45
+ verbose: false,
46
+ silent: true,
47
+ },
48
+ host
49
+ );
50
+ const addErrorSpy = jest.spyOn(workspaceContext, "addError");
51
+
52
+ function check(options: Options = { matchDependencyVersions: {} }) {
53
+ consistentVersions.check(workspaceContext, options);
54
+ }
55
+
56
+ return { addErrorSpy, check, host };
57
+ }
58
+
59
+ function addPackageJson(host: Host, filePath: string, packageJson: PackageJson) {
60
+ const dirPath = path.resolve(cwd!, path.dirname(filePath));
61
+ const resolvedFilePath = path.resolve(cwd!, filePath);
62
+
63
+ makeDirectoryRecursively(dirPath);
64
+ host.writeJson(resolvedFilePath, packageJson);
65
+ return (): PackageJson => {
66
+ return host.readJson(resolvedFilePath);
67
+ };
68
+ }
69
+
70
+ describe("standard tests", () => {
71
+ let testPackageJson: PackageJson;
72
+
73
+ beforeEach(() => {
74
+ testPackageJson = {
75
+ name: "test",
76
+ dependencies: {
77
+ greatLib: "^15",
78
+ both: "1",
79
+ },
80
+ peerDependencies: {
81
+ whatever: "15",
82
+ },
83
+ devDependencies: {
84
+ else: "27.2.1",
85
+ both: "1",
86
+ },
87
+ };
88
+ });
89
+
90
+ it("Does nothing when arguments are empty", async () => {
91
+ const { addErrorSpy, check, host } = makeWorkspace();
92
+ addPackageJson(host, "./package.json", testPackageJson);
93
+
94
+ check();
95
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
96
+ check({ matchDependencyVersions: {} });
97
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
98
+ });
99
+
100
+ it("Fixes packages that have an incorrect dependency version", async () => {
101
+ const { addErrorSpy, check, host } = makeWorkspace(true);
102
+ const readTestPackageJson = addPackageJson(host, "./package.json", testPackageJson);
103
+
104
+ const requiredGreatLibVersion = "1.2.3";
105
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
106
+ check({
107
+ matchDependencyVersions: { both: testPackageJson.dependencies!.both, greatLib: requiredGreatLibVersion },
108
+ });
109
+ expect(addErrorSpy).toHaveBeenCalledTimes(1);
110
+ expect(readTestPackageJson().dependencies!.greatLib).toEqual(requiredGreatLibVersion);
111
+ });
112
+
113
+ it("Ignores packages that have a correct dependency version", async () => {
114
+ const { addErrorSpy, check, host } = makeWorkspace();
115
+ addPackageJson(host, "./package.json", testPackageJson);
116
+
117
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
118
+ check({
119
+ matchDependencyVersions: {
120
+ both: testPackageJson.dependencies!.both,
121
+ greatLib: testPackageJson.dependencies!.greatLib,
122
+ },
123
+ });
124
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
125
+ });
126
+
127
+ it("Fixes packages that have an incorrect devDependency version", async () => {
128
+ const { addErrorSpy, check, host } = makeWorkspace(true);
129
+ const readTestPackageJson = addPackageJson(host, "./package.json", testPackageJson);
130
+
131
+ const requiredElseLibVersion = "1.2.3";
132
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
133
+ check({ matchDependencyVersions: { both: testPackageJson.dependencies!.both, else: requiredElseLibVersion } });
134
+ expect(addErrorSpy).toHaveBeenCalledTimes(1);
135
+ expect(readTestPackageJson().devDependencies!.else).toEqual(requiredElseLibVersion);
136
+ });
137
+
138
+ it("Ignores packages that have a correct devDependency version", async () => {
139
+ const { addErrorSpy, check, host } = makeWorkspace();
140
+ addPackageJson(host, "./package.json", testPackageJson);
141
+
142
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
143
+ check({
144
+ matchDependencyVersions: {
145
+ both: testPackageJson.dependencies!.both,
146
+ greatLib: testPackageJson.dependencies!.greatLib,
147
+ },
148
+ });
149
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
150
+ });
151
+
152
+ it("Fixes packages that have an incorrect dependency and devDependency versions", async () => {
153
+ const { addErrorSpy, check, host } = makeWorkspace(true);
154
+ const readTestPackageJson = addPackageJson(host, "./package.json", testPackageJson);
155
+
156
+ const requiredBothVersion = "1.2.3";
157
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
158
+ check({ matchDependencyVersions: { both: requiredBothVersion } });
159
+ expect(addErrorSpy).toHaveBeenCalledTimes(2);
160
+ expect(readTestPackageJson().dependencies!.both).toEqual(requiredBothVersion);
161
+ expect(readTestPackageJson().devDependencies!.both).toEqual(requiredBothVersion);
162
+ });
163
+ });
164
+
165
+ describe("Multiple accepted versions tests", () => {
166
+ let testPackageJson: PackageJson;
167
+
168
+ beforeEach(() => {
169
+ testPackageJson = {
170
+ name: "test",
171
+ dependencies: {
172
+ greatLib: "^15",
173
+ both: "1",
174
+ },
175
+ peerDependencies: {
176
+ whatever: "15",
177
+ },
178
+ devDependencies: {
179
+ else: "27.2.1",
180
+ both: "1",
181
+ },
182
+ };
183
+ });
184
+
185
+ it("Accepts a match when multiple versions are configured", async () => {
186
+ const { addErrorSpy, check, host } = makeWorkspace();
187
+ addPackageJson(host, "./package.json", testPackageJson);
188
+
189
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
190
+ check({ matchDependencyVersions: { greatLib: [testPackageJson.dependencies!.greatLib] } });
191
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
192
+ check({ matchDependencyVersions: { greatLib: ["1", "2", testPackageJson.dependencies!.greatLib] } });
193
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
194
+ check({ matchDependencyVersions: { greatLib: ["1", "2", testPackageJson.dependencies!.greatLib, "99", "100"] } });
195
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
196
+ check({ matchDependencyVersions: { greatLib: [testPackageJson.dependencies!.greatLib, "99", "100"] } });
197
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
198
+ });
199
+
200
+ it("Errors when version does not match", async () => {
201
+ const { addErrorSpy, check, host } = makeWorkspace();
202
+ addPackageJson(host, "./package.json", testPackageJson);
203
+
204
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
205
+ check({ matchDependencyVersions: { greatLib: ["1", "2"] } });
206
+ expect(addErrorSpy).toHaveBeenCalledTimes(1);
207
+ addErrorSpy.mockReset();
208
+
209
+ expect(addErrorSpy).toHaveBeenCalledTimes(0);
210
+ check({ matchDependencyVersions: { both: ["99", "100"] } });
211
+ expect(addErrorSpy).toHaveBeenCalledTimes(2);
212
+ expect(addErrorSpy.mock.calls[0][0].message).toEqual(
213
+ `Expected dependency on both to match one of '["99","100"]', got '${
214
+ testPackageJson.dependencies!.both
215
+ }' instead.`
216
+ );
217
+ expect(addErrorSpy.mock.calls[1][0].message).toEqual(
218
+ `Expected devDependency on both to match one of '["99","100"]', got '${
219
+ testPackageJson.devDependencies!.both
220
+ }' instead.`
221
+ );
222
+ });
223
+ });
224
+ });