workspace-tools 0.16.2 → 0.18.2

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 (48) hide show
  1. package/CHANGELOG.json +76 -1
  2. package/CHANGELOG.md +34 -2
  3. package/README.md +4 -3
  4. package/beachball.config.js +9 -0
  5. package/lib/__tests__/dependencies.test.js +6 -6
  6. package/lib/__tests__/getChangedPackages.test.js +31 -31
  7. package/lib/__tests__/getDefaultRemote.test.js +6 -6
  8. package/lib/__tests__/getInitDefaultBranch.test.js +6 -6
  9. package/lib/__tests__/getScopedPackages.test.js +9 -9
  10. package/lib/__tests__/getWorkspaceRoot.test.js +11 -11
  11. package/lib/__tests__/getWorkspaces.test.js +15 -15
  12. package/lib/__tests__/lockfile.test.js +28 -11
  13. package/lib/__tests__/queryLockFile.test.d.ts +1 -0
  14. package/lib/__tests__/queryLockFile.test.js +43 -0
  15. package/lib/dependencies.js +1 -0
  16. package/lib/getPackageInfos.js +10 -4
  17. package/lib/getPackagePaths.js +19 -18
  18. package/lib/git.js +30 -23
  19. package/lib/graph.js +2 -1
  20. package/lib/helpers/setupFixture.js +10 -10
  21. package/lib/index.js +28 -19
  22. package/lib/infoFromPackageJson.js +1 -0
  23. package/lib/lockfile/index.d.ts +1 -1
  24. package/lib/lockfile/index.js +54 -15
  25. package/lib/lockfile/nameAtVersion.js +1 -0
  26. package/lib/lockfile/parseNpmLock.d.ts +2 -0
  27. package/lib/lockfile/parseNpmLock.js +22 -0
  28. package/lib/lockfile/parsePnpmLock.js +2 -1
  29. package/lib/lockfile/queryLockFile.js +2 -1
  30. package/lib/lockfile/types.d.ts +31 -0
  31. package/lib/paths.js +1 -0
  32. package/lib/scope.js +3 -2
  33. package/lib/workspaces/findWorkspacePath.js +1 -0
  34. package/lib/workspaces/getChangedPackages.js +8 -7
  35. package/lib/workspaces/getWorkspacePackageInfo.js +1 -0
  36. package/lib/workspaces/getWorkspaceRoot.js +16 -17
  37. package/lib/workspaces/getWorkspaces.js +16 -17
  38. package/lib/workspaces/implementations/index.d.ts +9 -1
  39. package/lib/workspaces/implementations/index.js +48 -20
  40. package/lib/workspaces/implementations/lerna.js +3 -2
  41. package/lib/workspaces/implementations/npm.js +4 -3
  42. package/lib/workspaces/implementations/packageJsonWorkspaces.js +7 -4
  43. package/lib/workspaces/implementations/pnpm.js +5 -4
  44. package/lib/workspaces/implementations/rush.js +2 -1
  45. package/lib/workspaces/implementations/yarn.js +3 -2
  46. package/lib/workspaces/listOfWorkspacePackageNames.js +1 -0
  47. package/lib/workspaces/workspaces.js +2 -1
  48. package/package.json +3 -6
@@ -2,24 +2,41 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const setupFixture_1 = require("../helpers/setupFixture");
4
4
  const lockfile_1 = require("../lockfile");
5
+ const ERROR_MESSAGES = {
6
+ NO_LOCK: "You do not have yarn.lock, pnpm-lock.yaml or package-lock.json. Please use one of these package managers.",
7
+ UNSUPPORTED: "Your package-lock.json version is not supported: lockfileVersion is 1. You need npm version 7 or above and package-lock version 2 or above. Please, upgrade npm or choose a different package manager.",
8
+ };
5
9
  describe("parseLockFile()", () => {
6
- it("parses yarn.lock file when it is found", async () => {
7
- const packageRoot = await setupFixture_1.setupFixture("basic");
8
- const parsedLockeFile = await lockfile_1.parseLockFile(packageRoot);
10
+ // General
11
+ it("throws if it cannot find lock file", async () => {
12
+ const packageRoot = await (0, setupFixture_1.setupFixture)("basic-without-lock-file");
13
+ await expect((0, lockfile_1.parseLockFile)(packageRoot)).rejects.toThrow(ERROR_MESSAGES.NO_LOCK);
14
+ });
15
+ // NPM
16
+ it("parses package-lock.json file when it is found", async () => {
17
+ const packageRoot = await (0, setupFixture_1.setupFixture)("monorepo-npm");
18
+ const parsedLockeFile = await (0, lockfile_1.parseLockFile)(packageRoot);
9
19
  expect(parsedLockeFile).toHaveProperty("type", "success");
10
20
  });
11
- it("throws if it cannot find a yarn.lock file", async () => {
12
- const packageRoot = await setupFixture_1.setupFixture("basic-without-lock-file");
13
- await expect(lockfile_1.parseLockFile(packageRoot)).rejects.toThrow("You do not have either yarn.lock nor pnpm-lock.yaml. Please use one of these package managers");
21
+ it("throws if npm version is unsupported", async () => {
22
+ const packageRoot = await (0, setupFixture_1.setupFixture)("monorepo-npm-unsupported");
23
+ await expect((0, lockfile_1.parseLockFile)(packageRoot)).rejects.toThrow(ERROR_MESSAGES.UNSUPPORTED);
24
+ });
25
+ // Yarn
26
+ it("parses yarn.lock file when it is found", async () => {
27
+ const packageRoot = await (0, setupFixture_1.setupFixture)("basic");
28
+ const parsedLockeFile = await (0, lockfile_1.parseLockFile)(packageRoot);
29
+ expect(parsedLockeFile).toHaveProperty("type", "success");
14
30
  });
15
31
  it("parses combined ranges in yarn.lock", async () => {
16
- const packageRoot = await setupFixture_1.setupFixture("basic-yarn");
17
- const parsedLockeFile = await lockfile_1.parseLockFile(packageRoot);
32
+ const packageRoot = await (0, setupFixture_1.setupFixture)("basic-yarn");
33
+ const parsedLockeFile = await (0, lockfile_1.parseLockFile)(packageRoot);
18
34
  expect(parsedLockeFile.object["@babel/code-frame@^7.0.0"].version).toBe(parsedLockeFile.object["@babel/code-frame@^7.8.3"].version);
19
35
  });
20
- it("parses pnpm-lock.yaml properly", async () => {
21
- const packageRoot = await setupFixture_1.setupFixture("basic-pnpm");
22
- const parsedLockeFile = await lockfile_1.parseLockFile(packageRoot);
36
+ // PNPM
37
+ it("parses pnpm-lock.yaml file when it is found", async () => {
38
+ const packageRoot = await (0, setupFixture_1.setupFixture)("basic-pnpm");
39
+ const parsedLockeFile = await (0, lockfile_1.parseLockFile)(packageRoot);
23
40
  expect(Object.keys(parsedLockeFile.object["yargs@16.2.0"].dependencies)).toContain("cliui");
24
41
  });
25
42
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const setupFixture_1 = require("../helpers/setupFixture");
4
+ const __1 = require("..");
5
+ /**
6
+ * These tests rely on the "@microsoft/task-scheduler" package and its version as defined in package.json in
7
+ * fixtures:
8
+ * - monorepo-npm
9
+ * - basic-yarn
10
+ * - monorepo-pnpm
11
+ *
12
+ * If making any changes to those fixtures and "@microsoft/task-scheduler" dependency, update the `packageName` and
13
+ * `packageVersion` constants.
14
+ */
15
+ const packageName = "@microsoft/task-scheduler";
16
+ const packageVersion = "2.7.1";
17
+ describe("queryLockFile()", () => {
18
+ // NPM
19
+ it("retrieves a dependency from a lock generated by npm", async () => {
20
+ const packageRoot = await (0, setupFixture_1.setupFixture)("monorepo-npm");
21
+ const parsedLockFile = await (0, __1.parseLockFile)(packageRoot);
22
+ const result = (0, __1.queryLockFile)(packageName, packageVersion, parsedLockFile);
23
+ expect(result).toBeDefined();
24
+ expect(result.version).toBe(packageVersion);
25
+ });
26
+ // Yarn
27
+ it("retrieves a dependency from a lock generated by yarn", async () => {
28
+ const packageRoot = await (0, setupFixture_1.setupFixture)("basic-yarn");
29
+ const parsedLockFile = await (0, __1.parseLockFile)(packageRoot);
30
+ // NOTE: Yarn’s locks include ranges.
31
+ const result = (0, __1.queryLockFile)(packageName, `^${packageVersion}`, parsedLockFile);
32
+ expect(result).toBeDefined();
33
+ expect(result.version).toBe(packageVersion);
34
+ });
35
+ // PNPM
36
+ it("retrieves a dependency from a lock generated by pnpm", async () => {
37
+ const packageRoot = await (0, setupFixture_1.setupFixture)("monorepo-pnpm");
38
+ const parsedLockFile = await (0, __1.parseLockFile)(packageRoot);
39
+ const result = (0, __1.queryLockFile)(packageName, packageVersion, parsedLockFile);
40
+ expect(result).toBeDefined();
41
+ expect(result.version).toBe(packageVersion);
42
+ });
43
+ });
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getInternalDeps = exports.getTransitiveDependents = exports.getTransitiveDependencies = exports.getTransitiveProviders = exports.getTransitiveConsumers = exports.getDependentMap = void 0;
3
4
  const graphCache = new Map();
4
5
  function memoizedKey(packages, scope = []) {
5
6
  return JSON.stringify({ packages, scope });
@@ -3,21 +3,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getPackageInfos = void 0;
6
7
  const fs_1 = __importDefault(require("fs"));
7
8
  const infoFromPackageJson_1 = require("./infoFromPackageJson");
8
9
  const workspaces_1 = require("./workspaces/workspaces");
9
10
  function getPackageInfos(cwd) {
10
- const packageJsonFiles = workspaces_1.getAllPackageJsonFiles(cwd);
11
+ const packageJsonFiles = (0, workspaces_1.getAllPackageJsonFiles)(cwd);
11
12
  const packageInfos = {};
12
13
  if (packageJsonFiles && packageJsonFiles.length > 0) {
13
14
  packageJsonFiles.forEach((packageJsonPath) => {
14
15
  try {
15
16
  const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf-8"));
16
- packageInfos[packageJson.name] = infoFromPackageJson_1.infoFromPackageJson(packageJson, packageJsonPath);
17
+ packageInfos[packageJson.name] = (0, infoFromPackageJson_1.infoFromPackageJson)(packageJson, packageJsonPath);
17
18
  }
18
19
  catch (e) {
19
- // Pass, the package.json is invalid
20
- throw new Error(`Invalid package.json file detected ${packageJsonPath}: ${e.message}`);
20
+ if (e instanceof Error) {
21
+ // Pass, the package.json is invalid
22
+ throw new Error(`Invalid package.json file detected ${packageJsonPath}: ${e.message}`);
23
+ }
24
+ else {
25
+ throw e;
26
+ }
21
27
  }
22
28
  });
23
29
  return packageInfos;
@@ -3,27 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getPackagePaths = void 0;
6
7
  const path_1 = __importDefault(require("path"));
7
8
  const globby_1 = __importDefault(require("globby"));
9
+ const packagePathsCache = {};
8
10
  function getPackagePaths(workspacesRoot, packages) {
9
- const packagePaths = packages.map((glob) => {
10
- const globbed = globby_1.default
11
- .sync(path_1.default.join(glob, "package.json").replace(/\\/g, "/"), {
12
- cwd: workspacesRoot,
13
- absolute: true,
14
- ignore: ["**/node_modules/**"],
15
- })
16
- .map((p) => path_1.default.dirname(p));
17
- return globbed;
18
- });
19
- /*
20
- * fast-glob returns unix style path,
21
- * so we use path.join to align the path with the platform.
22
- */
23
- return packagePaths
24
- .reduce((acc, cur) => {
25
- return [...acc, ...cur];
11
+ if (packagePathsCache[workspacesRoot]) {
12
+ return packagePathsCache[workspacesRoot];
13
+ }
14
+ const packagePaths = globby_1.default
15
+ .sync(packages.map((glob) => path_1.default.join(glob, "package.json").replace(/\\/g, "/")), {
16
+ cwd: workspacesRoot,
17
+ absolute: true,
18
+ ignore: ["**/node_modules/**"],
19
+ stats: false,
26
20
  })
27
- .map((p) => path_1.default.join(p));
21
+ .map((p) => path_1.default.dirname(p));
22
+ if (path_1.default.sep === "/") {
23
+ packagePathsCache[workspacesRoot] = packagePaths;
24
+ }
25
+ else {
26
+ packagePathsCache[workspacesRoot] = packagePaths.map((p) => p.replace(/\//g, path_1.default.sep));
27
+ }
28
+ return packagePathsCache[workspacesRoot];
28
29
  }
29
30
  exports.getPackagePaths = getPackagePaths;
package/lib/git.js CHANGED
@@ -3,11 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.listAllTrackedFiles = exports.getDefaultRemote = exports.getDefaultBranch = exports.getDefaultRemoteBranch = exports.parseRemoteBranch = exports.getRemoteBranch = exports.getParentBranch = exports.revertLocalChanges = exports.stageAndCommit = exports.commit = exports.stage = exports.init = exports.getFileAddedHash = exports.getCurrentHash = exports.getShortBranchName = exports.getFullBranchRef = exports.getBranchName = exports.getUserEmail = exports.getRecentCommitMessages = exports.getStagedChanges = exports.getChangesBetweenRefs = exports.getBranchChanges = exports.getChanges = exports.getUnstagedChanges = exports.fetchRemoteBranch = exports.fetchRemote = exports.getUntrackedChanges = exports.gitFailFast = exports.git = exports.addGitObserver = void 0;
6
7
  const child_process_1 = require("child_process");
7
8
  const fs_1 = __importDefault(require("fs"));
8
9
  const path_1 = __importDefault(require("path"));
9
10
  const paths_1 = require("./paths");
10
11
  const git_url_parse_1 = __importDefault(require("git-url-parse"));
12
+ function gitError(message, e) {
13
+ if (e && e instanceof Error) {
14
+ return new Error(`${message}: ${e.message}`);
15
+ }
16
+ return new Error(message);
17
+ }
11
18
  /**
12
19
  * A maxBuffer override globally for all git operations
13
20
  * Bumps up the default to 500MB as opposed to the 1MB
@@ -28,7 +35,7 @@ exports.addGitObserver = addGitObserver;
28
35
  * Runs git command - use this for read only commands
29
36
  */
30
37
  function git(args, options) {
31
- const results = child_process_1.spawnSync("git", args, Object.assign({ maxBuffer: MaxBufferOption }, options));
38
+ const results = (0, child_process_1.spawnSync)("git", args, Object.assign({ maxBuffer: MaxBufferOption }, options));
32
39
  let output;
33
40
  if (results.status === 0) {
34
41
  output = {
@@ -62,9 +69,9 @@ function gitFailFast(args, options) {
62
69
  const gitResult = git(args, options);
63
70
  if (!gitResult.success) {
64
71
  process.exitCode = 1;
65
- throw new Error(`CRITICAL ERROR: running git command: git ${args.join(" ")}!
66
- ${gitResult.stdout && gitResult.stdout.toString().trimRight()}
67
- ${gitResult.stderr && gitResult.stderr.toString().trimRight()}`);
72
+ throw gitError(`CRITICAL ERROR: running git command: git ${args.join(" ")}!
73
+ ${gitResult.stdout && gitResult.stdout.toString().trimRight()}
74
+ ${gitResult.stderr && gitResult.stderr.toString().trimRight()}`);
68
75
  }
69
76
  }
70
77
  exports.gitFailFast = gitFailFast;
@@ -92,21 +99,21 @@ function getUntrackedChanges(cwd) {
92
99
  return untracked;
93
100
  }
94
101
  catch (e) {
95
- throw new Error(`Cannot gather information about untracked changes: ${e.message}`);
102
+ throw gitError(`Cannot gather information about untracked changes`, e);
96
103
  }
97
104
  }
98
105
  exports.getUntrackedChanges = getUntrackedChanges;
99
106
  function fetchRemote(remote, cwd) {
100
107
  const results = git(["fetch", remote], { cwd });
101
108
  if (!results.success) {
102
- throw new Error(`Cannot fetch remote: ${remote}`);
109
+ throw gitError(`Cannot fetch remote: ${remote}`);
103
110
  }
104
111
  }
105
112
  exports.fetchRemote = fetchRemote;
106
113
  function fetchRemoteBranch(remote, remoteBranch, cwd) {
107
114
  const results = git(["fetch", remote, remoteBranch], { cwd });
108
115
  if (!results.success) {
109
- throw new Error(`Cannot fetch remote: ${remote} ${remoteBranch}`);
116
+ throw gitError(`Cannot fetch remote: ${remote} ${remoteBranch}`);
110
117
  }
111
118
  }
112
119
  exports.fetchRemoteBranch = fetchRemoteBranch;
@@ -119,7 +126,7 @@ function getUnstagedChanges(cwd) {
119
126
  return processGitOutput(git(["--no-pager", "diff", "--name-only", "--relative"], { cwd }));
120
127
  }
121
128
  catch (e) {
122
- throw new Error(`Cannot gather information about unstaged changes: ${e.message}`);
129
+ throw gitError(`Cannot gather information about unstaged changes`, e);
123
130
  }
124
131
  }
125
132
  exports.getUnstagedChanges = getUnstagedChanges;
@@ -128,7 +135,7 @@ function getChanges(branch, cwd) {
128
135
  return processGitOutput(git(["--no-pager", "diff", "--relative", "--name-only", branch + "..."], { cwd }));
129
136
  }
130
137
  catch (e) {
131
- throw new Error(`Cannot gather information about changes: ${e.message}`);
138
+ throw gitError(`Cannot gather information about changes`, e);
132
139
  }
133
140
  }
134
141
  exports.getChanges = getChanges;
@@ -142,7 +149,7 @@ function getBranchChanges(branch, cwd) {
142
149
  return processGitOutput(git(["--no-pager", "diff", "--name-only", "--relative", branch + "..."], { cwd }));
143
150
  }
144
151
  catch (e) {
145
- throw new Error(`Cannot gather information about branch changes: ${e.message}`);
152
+ throw gitError(`Cannot gather information about branch changes`, e);
146
153
  }
147
154
  }
148
155
  exports.getBranchChanges = getBranchChanges;
@@ -153,7 +160,7 @@ function getChangesBetweenRefs(fromRef, toRef, options, pattern, cwd) {
153
160
  }));
154
161
  }
155
162
  catch (e) {
156
- throw new Error(`Cannot gather information about change between refs changes (${fromRef} to ${toRef}): ${e.message}`);
163
+ throw gitError(`Cannot gather information about change between refs changes (${fromRef} to ${toRef})`, e);
157
164
  }
158
165
  }
159
166
  exports.getChangesBetweenRefs = getChangesBetweenRefs;
@@ -162,7 +169,7 @@ function getStagedChanges(cwd) {
162
169
  return processGitOutput(git(["--no-pager", "diff", "--relative", "--staged", "--name-only"], { cwd }));
163
170
  }
164
171
  catch (e) {
165
- throw new Error(`Cannot gather information about staged changes: ${e.message}`);
172
+ throw gitError(`Cannot gather information about staged changes`, e);
166
173
  }
167
174
  }
168
175
  exports.getStagedChanges = getStagedChanges;
@@ -177,7 +184,7 @@ function getRecentCommitMessages(branch, cwd) {
177
184
  return lines.map((line) => line.trim());
178
185
  }
179
186
  catch (e) {
180
- throw new Error(`Cannot gather information about recent commits: ${e.message}`);
187
+ throw gitError(`Cannot gather information about recent commits`, e);
181
188
  }
182
189
  }
183
190
  exports.getRecentCommitMessages = getRecentCommitMessages;
@@ -190,7 +197,7 @@ function getUserEmail(cwd) {
190
197
  return results.stdout;
191
198
  }
192
199
  catch (e) {
193
- throw new Error(`Cannot gather information about user.email: ${e.message}`);
200
+ throw gitError(`Cannot gather information about user.email`, e);
194
201
  }
195
202
  }
196
203
  exports.getUserEmail = getUserEmail;
@@ -202,7 +209,7 @@ function getBranchName(cwd) {
202
209
  }
203
210
  }
204
211
  catch (e) {
205
- throw new Error(`Cannot get branch name: ${e.message}`);
212
+ throw gitError(`Cannot get branch name`, e);
206
213
  }
207
214
  return null;
208
215
  }
@@ -233,7 +240,7 @@ function getCurrentHash(cwd) {
233
240
  }
234
241
  }
235
242
  catch (e) {
236
- throw new Error(`Cannot get current git hash: ${e.message}`);
243
+ throw gitError(`Cannot get current git hash`, e);
237
244
  }
238
245
  return null;
239
246
  }
@@ -254,7 +261,7 @@ function init(cwd, email, username) {
254
261
  const configLines = git(["config", "--list"], { cwd }).stdout.split("\n");
255
262
  if (!configLines.find((line) => line.includes("user.name"))) {
256
263
  if (!username) {
257
- throw new Error("must include a username when initializing git repo");
264
+ throw gitError("must include a username when initializing git repo");
258
265
  }
259
266
  git(["config", "user.name", username], { cwd });
260
267
  }
@@ -273,7 +280,7 @@ function stage(patterns, cwd) {
273
280
  });
274
281
  }
275
282
  catch (e) {
276
- throw new Error(`Cannot stage changes: ${e.message}`);
283
+ throw gitError(`Cannot stage changes`, e);
277
284
  }
278
285
  }
279
286
  exports.stage = stage;
@@ -285,7 +292,7 @@ function commit(message, cwd, options = []) {
285
292
  }
286
293
  }
287
294
  catch (e) {
288
- throw new Error(`Cannot commit changes: ${e.message}`);
295
+ throw gitError(`Cannot commit changes`, e);
289
296
  }
290
297
  }
291
298
  exports.commit = commit;
@@ -353,7 +360,7 @@ function parseRemoteBranch(branch) {
353
360
  exports.parseRemoteBranch = parseRemoteBranch;
354
361
  function normalizeRepoUrl(repositoryUrl) {
355
362
  try {
356
- const parsed = git_url_parse_1.default(repositoryUrl);
363
+ const parsed = (0, git_url_parse_1.default)(repositoryUrl);
357
364
  return parsed
358
365
  .toString("https")
359
366
  .replace(/\.git$/, "")
@@ -375,10 +382,10 @@ function getDefaultRemoteBranch(branch, cwd) {
375
382
  * HEAD branch: main
376
383
  *
377
384
  */
378
- const headBranchLine = showRemote.stdout.split(/\n/).find(line => line.includes('HEAD branch'));
385
+ const headBranchLine = showRemote.stdout.split(/\n/).find((line) => line.includes("HEAD branch"));
379
386
  let remoteDefaultBranch;
380
387
  if (headBranchLine) {
381
- remoteDefaultBranch = headBranchLine.replace(/^\s*HEAD branch:\s+/, '');
388
+ remoteDefaultBranch = headBranchLine.replace(/^\s*HEAD branch:\s+/, "");
382
389
  }
383
390
  branch = branch || remoteDefaultBranch || getDefaultBranch(cwd);
384
391
  return `${defaultRemote}/${branch}`;
@@ -396,7 +403,7 @@ exports.getDefaultBranch = getDefaultBranch;
396
403
  function getDefaultRemote(cwd) {
397
404
  let packageJson;
398
405
  try {
399
- packageJson = JSON.parse(fs_1.default.readFileSync(path_1.default.join(paths_1.findGitRoot(cwd), "package.json")).toString());
406
+ packageJson = JSON.parse(fs_1.default.readFileSync(path_1.default.join((0, paths_1.findGitRoot)(cwd), "package.json")).toString());
400
407
  }
401
408
  catch (e) {
402
409
  throw new Error("invalid package.json detected");
package/lib/graph.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._resetGraphCache = exports.getPackageGraph = void 0;
3
4
  const dependencies_1 = require("./dependencies");
4
5
  const graphCache = new Map();
5
6
  function getPackageGraph(packages) {
@@ -8,7 +9,7 @@ function getPackageGraph(packages) {
8
9
  }
9
10
  const edges = [];
10
11
  for (const [pkg, info] of Object.entries(packages)) {
11
- const deps = dependencies_1.getInternalDeps(info, packages);
12
+ const deps = (0, dependencies_1.getInternalDeps)(info, packages);
12
13
  for (const dep of deps) {
13
14
  edges.push([dep, pkg]);
14
15
  }
@@ -3,10 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.setupLocalRemote = exports.cleanupFixtures = exports.setupFixture = void 0;
6
7
  const path_1 = __importDefault(require("path"));
7
8
  const find_up_1 = __importDefault(require("find-up"));
8
9
  const fs_extra_1 = __importDefault(require("fs-extra"));
9
- const fs_extra_2 = __importDefault(require("fs-extra"));
10
10
  const tmp_1 = __importDefault(require("tmp"));
11
11
  const git_1 = require("../git");
12
12
  // tmp is supposed to be able to clean up automatically, but this doesn't always work within jest.
@@ -34,14 +34,14 @@ function setupFixture(fixtureName) {
34
34
  const cwd = path_1.default.join(tempRoot.name, String(tempNumber++), fixtureName);
35
35
  fs_extra_1.default.mkdirpSync(cwd);
36
36
  fs_extra_1.default.copySync(fixturePath, cwd);
37
- git_1.init(cwd, "test@test.email", "test user");
37
+ (0, git_1.init)(cwd, "test@test.email", "test user");
38
38
  // Make the 'main' branch the default in the test repo
39
39
  // ensure that the configuration for this repo does not collide
40
40
  // with any global configuration the user had made, so we have
41
41
  // a 'fixed' value for our tests, regardless of user configuration
42
- git_1.gitFailFast(['symbolic-ref', 'HEAD', 'refs/heads/main'], { cwd });
43
- git_1.gitFailFast(['config', 'init.defaultBranch', 'main'], { cwd });
44
- git_1.stageAndCommit(["."], "test", cwd);
42
+ (0, git_1.gitFailFast)(["symbolic-ref", "HEAD", "refs/heads/main"], { cwd });
43
+ (0, git_1.gitFailFast)(["config", "init.defaultBranch", "main"], { cwd });
44
+ (0, git_1.stageAndCommit)(["."], "test", cwd);
45
45
  return cwd;
46
46
  }
47
47
  exports.setupFixture = setupFixture;
@@ -56,12 +56,12 @@ function setupLocalRemote(cwd, remoteName, fixtureName) {
56
56
  // Create a seperate repo and configure it as a remote
57
57
  const remoteCwd = setupFixture(fixtureName);
58
58
  const remoteUrl = remoteCwd.replace(/\\/g, "/");
59
- git_1.gitFailFast(["remote", "add", remoteName, remoteUrl], { cwd });
59
+ (0, git_1.gitFailFast)(["remote", "add", remoteName, remoteUrl], { cwd });
60
60
  // Configure url in package.json
61
61
  const pkgJsonPath = path_1.default.join(cwd, "package.json");
62
- const pkgJson = fs_extra_2.default.readJSONSync(pkgJsonPath);
63
- fs_extra_2.default.writeJSONSync(pkgJsonPath, Object.assign(Object.assign({}, pkgJson), { repository: {
64
- url: remoteUrl
65
- } }));
62
+ const pkgJson = JSON.parse(fs_extra_1.default.readFileSync(pkgJsonPath, "utf-8"));
63
+ fs_extra_1.default.writeFileSync(pkgJsonPath, JSON.stringify(Object.assign(Object.assign({}, pkgJson), { repository: {
64
+ url: remoteUrl,
65
+ } }), null, 2));
66
66
  }
67
67
  exports.setupLocalRemote = setupLocalRemote;
package/lib/index.js CHANGED
@@ -1,21 +1,30 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./dependencies"));
7
- __export(require("./getPackageInfos"));
8
- __export(require("./git"));
9
- __export(require("./graph"));
10
- __export(require("./lockfile"));
11
- __export(require("./paths"));
12
- __export(require("./scope"));
13
- __export(require("./workspaces/findWorkspacePath"));
14
- __export(require("./workspaces/getWorkspaces"));
15
- __export(require("./workspaces/getWorkspaceRoot"));
16
- __export(require("./workspaces/implementations/pnpm"));
17
- __export(require("./workspaces/implementations/rush"));
18
- __export(require("./workspaces/implementations/yarn"));
19
- __export(require("./workspaces/getChangedPackages"));
20
- __export(require("./workspaces/listOfWorkspacePackageNames"));
21
- __export(require("./workspaces/workspaces"));
13
+ __exportStar(require("./dependencies"), exports);
14
+ __exportStar(require("./getPackageInfos"), exports);
15
+ __exportStar(require("./git"), exports);
16
+ __exportStar(require("./graph"), exports);
17
+ __exportStar(require("./lockfile"), exports);
18
+ __exportStar(require("./paths"), exports);
19
+ __exportStar(require("./scope"), exports);
20
+ __exportStar(require("./types/PackageInfo"), exports);
21
+ __exportStar(require("./types/WorkspaceInfo"), exports);
22
+ __exportStar(require("./workspaces/findWorkspacePath"), exports);
23
+ __exportStar(require("./workspaces/getWorkspaces"), exports);
24
+ __exportStar(require("./workspaces/getWorkspaceRoot"), exports);
25
+ __exportStar(require("./workspaces/implementations/pnpm"), exports);
26
+ __exportStar(require("./workspaces/implementations/rush"), exports);
27
+ __exportStar(require("./workspaces/implementations/yarn"), exports);
28
+ __exportStar(require("./workspaces/getChangedPackages"), exports);
29
+ __exportStar(require("./workspaces/listOfWorkspacePackageNames"), exports);
30
+ __exportStar(require("./workspaces/workspaces"), exports);
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.infoFromPackageJson = void 0;
3
4
  function infoFromPackageJson(packageJson, packageJsonPath) {
4
5
  return Object.assign({ packageJsonPath }, packageJson);
5
6
  }
@@ -3,4 +3,4 @@ import { nameAtVersion } from "./nameAtVersion";
3
3
  export declare function parseLockFile(packageRoot: string): Promise<ParsedLock>;
4
4
  export { nameAtVersion };
5
5
  export { queryLockFile } from "./queryLockFile";
6
- export * from './types';
6
+ export * from "./types";
@@ -1,48 +1,87 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
5
14
  var __importStar = (this && this.__importStar) || function (mod) {
6
15
  if (mod && mod.__esModule) return mod;
7
16
  var result = {};
8
- if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9
- result["default"] = mod;
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
10
19
  return result;
11
20
  };
21
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23
+ };
24
+ var __importDefault = (this && this.__importDefault) || function (mod) {
25
+ return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ };
12
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.queryLockFile = exports.nameAtVersion = exports.parseLockFile = void 0;
29
+ // NOTE: never place the import of lockfile implementation here, as it slows down the library as a whole
13
30
  const find_up_1 = __importDefault(require("find-up"));
14
- const fs_extra_1 = __importDefault(require("fs-extra"));
15
- const read_yaml_file_1 = __importDefault(require("read-yaml-file"));
31
+ const fs_1 = __importDefault(require("fs"));
16
32
  const nameAtVersion_1 = require("./nameAtVersion");
17
- exports.nameAtVersion = nameAtVersion_1.nameAtVersion;
33
+ Object.defineProperty(exports, "nameAtVersion", { enumerable: true, get: function () { return nameAtVersion_1.nameAtVersion; } });
18
34
  const parsePnpmLock_1 = require("./parsePnpmLock");
35
+ const parseNpmLock_1 = require("./parseNpmLock");
19
36
  const memoization = {};
20
37
  async function parseLockFile(packageRoot) {
21
- const yarnLockPath = await find_up_1.default(["yarn.lock", "common/config/rush/yarn.lock"], { cwd: packageRoot });
38
+ const yarnLockPath = await (0, find_up_1.default)(["yarn.lock", "common/config/rush/yarn.lock"], { cwd: packageRoot });
22
39
  // First, test out whether this works for yarn
23
40
  if (yarnLockPath) {
24
41
  if (memoization[yarnLockPath]) {
25
42
  return memoization[yarnLockPath];
26
43
  }
27
44
  const parseYarnLock = (await Promise.resolve().then(() => __importStar(require("@yarnpkg/lockfile")))).parse;
28
- const yarnLock = fs_extra_1.default.readFileSync(yarnLockPath).toString();
45
+ const yarnLock = fs_1.default.readFileSync(yarnLockPath, 'utf-8');
29
46
  const parsed = parseYarnLock(yarnLock);
30
47
  memoization[yarnLockPath] = parsed;
31
48
  return parsed;
32
49
  }
33
50
  // Second, test out whether this works for pnpm
34
- let pnpmLockPath = await find_up_1.default(["pnpm-lock.yaml", "common/config/rush/pnpm-lock.yaml"], { cwd: packageRoot });
51
+ let pnpmLockPath = await (0, find_up_1.default)(["pnpm-lock.yaml", "common/config/rush/pnpm-lock.yaml"], { cwd: packageRoot });
35
52
  if (pnpmLockPath) {
36
53
  if (memoization[pnpmLockPath]) {
37
54
  return memoization[pnpmLockPath];
38
55
  }
39
- const yaml = await read_yaml_file_1.default(pnpmLockPath);
40
- const parsed = parsePnpmLock_1.parsePnpmLock(yaml);
56
+ const readYamlFile = require("read-yaml-file");
57
+ const yaml = (await readYamlFile(pnpmLockPath));
58
+ const parsed = (0, parsePnpmLock_1.parsePnpmLock)(yaml);
41
59
  memoization[pnpmLockPath] = parsed;
42
60
  return memoization[pnpmLockPath];
43
61
  }
44
- throw new Error("You do not have either yarn.lock nor pnpm-lock.yaml. Please use one of these package managers");
62
+ // Third, try for npm workspaces
63
+ let npmLockPath = await (0, find_up_1.default)(["package-lock.json"], { cwd: packageRoot });
64
+ if (npmLockPath) {
65
+ if (memoization[npmLockPath]) {
66
+ return memoization[npmLockPath];
67
+ }
68
+ let npmLockJson;
69
+ try {
70
+ npmLockJson = fs_1.default.readFileSync(npmLockPath, 'utf-8');
71
+ }
72
+ catch (_a) {
73
+ throw new Error("Couldn’t parse package-lock.json.");
74
+ }
75
+ const npmLock = JSON.parse(npmLockJson.toString());
76
+ if (!(npmLock === null || npmLock === void 0 ? void 0 : npmLock.lockfileVersion) || npmLock.lockfileVersion < 2) {
77
+ throw new Error(`Your package-lock.json version is not supported: lockfileVersion is ${npmLock.lockfileVersion}. You need npm version 7 or above and package-lock version 2 or above. Please, upgrade npm or choose a different package manager.`);
78
+ }
79
+ memoization[npmLockPath] = (0, parseNpmLock_1.parseNpmLock)(npmLock);
80
+ return memoization[npmLockPath];
81
+ }
82
+ throw new Error("You do not have yarn.lock, pnpm-lock.yaml or package-lock.json. Please use one of these package managers.");
45
83
  }
46
84
  exports.parseLockFile = parseLockFile;
47
85
  var queryLockFile_1 = require("./queryLockFile");
48
- exports.queryLockFile = queryLockFile_1.queryLockFile;
86
+ Object.defineProperty(exports, "queryLockFile", { enumerable: true, get: function () { return queryLockFile_1.queryLockFile; } });
87
+ __exportStar(require("./types"), exports);
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nameAtVersion = void 0;
3
4
  function nameAtVersion(name, version) {
4
5
  return `${name}@${version}`;
5
6
  }
@@ -0,0 +1,2 @@
1
+ import { ParsedLock, NpmLockFile } from "./types";
2
+ export declare const parseNpmLock: (lock: NpmLockFile) => ParsedLock;