workspace-tools 0.17.0 → 0.18.0

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/CHANGELOG.json CHANGED
@@ -2,7 +2,22 @@
2
2
  "name": "workspace-tools",
3
3
  "entries": [
4
4
  {
5
- "date": "Thu, 02 Dec 2021 17:11:04 GMT",
5
+ "date": "Fri, 07 Jan 2022 00:04:25 GMT",
6
+ "tag": "workspace-tools_v0.18.0",
7
+ "version": "0.18.0",
8
+ "comments": {
9
+ "minor": [
10
+ {
11
+ "author": "kchau@microsoft.com",
12
+ "package": "workspace-tools",
13
+ "comment": "speed up workspace-tools - reducing weight and adding caches - lazy load pkg mgr helpers",
14
+ "commit": "6105a8eb81e4972b9ffbdd18669a137f0f5d9400"
15
+ }
16
+ ]
17
+ }
18
+ },
19
+ {
20
+ "date": "Thu, 02 Dec 2021 17:11:12 GMT",
6
21
  "tag": "workspace-tools_v0.17.0",
7
22
  "version": "0.17.0",
8
23
  "comments": {
package/CHANGELOG.md CHANGED
@@ -1,12 +1,20 @@
1
1
  # Change Log - workspace-tools
2
2
 
3
- This log was last generated on Thu, 02 Dec 2021 17:11:04 GMT and should not be manually modified.
3
+ This log was last generated on Fri, 07 Jan 2022 00:04:25 GMT and should not be manually modified.
4
4
 
5
5
  <!-- Start content -->
6
6
 
7
+ ## 0.18.0
8
+
9
+ Fri, 07 Jan 2022 00:04:25 GMT
10
+
11
+ ### Minor changes
12
+
13
+ - speed up workspace-tools - reducing weight and adding caches - lazy load pkg mgr helpers (kchau@microsoft.com)
14
+
7
15
  ## 0.17.0
8
16
 
9
- Thu, 02 Dec 2021 17:11:04 GMT
17
+ Thu, 02 Dec 2021 17:11:12 GMT
10
18
 
11
19
  ### Minor changes
12
20
 
@@ -5,25 +5,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const path_1 = __importDefault(require("path"));
7
7
  const globby_1 = __importDefault(require("globby"));
8
+ const packagePathsCache = {};
8
9
  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];
10
+ if (packagePathsCache[workspacesRoot]) {
11
+ return packagePathsCache[workspacesRoot];
12
+ }
13
+ const packagePaths = globby_1.default
14
+ .sync(packages.map((glob) => path_1.default.join(glob, "package.json").replace(/\\/g, "/")), {
15
+ cwd: workspacesRoot,
16
+ absolute: true,
17
+ ignore: ["**/node_modules/**"],
18
+ stats: false,
26
19
  })
27
- .map((p) => path_1.default.join(p));
20
+ .map((p) => path_1.default.dirname(p));
21
+ if (path_1.default.sep === "/") {
22
+ packagePathsCache[workspacesRoot] = packagePaths;
23
+ }
24
+ else {
25
+ packagePathsCache[workspacesRoot] = packagePaths.map((p) => p.replace(/\//g, path_1.default.sep));
26
+ }
27
+ return packagePathsCache[workspacesRoot];
28
28
  }
29
29
  exports.getPackagePaths = getPackagePaths;
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const path_1 = __importDefault(require("path"));
7
7
  const find_up_1 = __importDefault(require("find-up"));
8
8
  const fs_extra_1 = __importDefault(require("fs-extra"));
9
- const fs_extra_2 = __importDefault(require("fs-extra"));
10
9
  const tmp_1 = __importDefault(require("tmp"));
11
10
  const git_1 = require("../git");
12
11
  // tmp is supposed to be able to clean up automatically, but this doesn't always work within jest.
@@ -39,8 +38,8 @@ function setupFixture(fixtureName) {
39
38
  // ensure that the configuration for this repo does not collide
40
39
  // with any global configuration the user had made, so we have
41
40
  // 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 });
41
+ git_1.gitFailFast(["symbolic-ref", "HEAD", "refs/heads/main"], { cwd });
42
+ git_1.gitFailFast(["config", "init.defaultBranch", "main"], { cwd });
44
43
  git_1.stageAndCommit(["."], "test", cwd);
45
44
  return cwd;
46
45
  }
@@ -59,9 +58,9 @@ function setupLocalRemote(cwd, remoteName, fixtureName) {
59
58
  git_1.gitFailFast(["remote", "add", remoteName, remoteUrl], { cwd });
60
59
  // Configure url in package.json
61
60
  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
- } }));
61
+ const pkgJson = JSON.parse(fs_extra_1.default.readFileSync(pkgJsonPath, "utf-8"));
62
+ fs_extra_1.default.writeFileSync(pkgJsonPath, JSON.stringify(Object.assign(Object.assign({}, pkgJson), { repository: {
63
+ url: remoteUrl,
64
+ } }), null, 2));
66
65
  }
67
66
  exports.setupLocalRemote = setupLocalRemote;
@@ -12,8 +12,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  // NOTE: never place the import of lockfile implementation here, as it slows down the library as a whole
14
14
  const find_up_1 = __importDefault(require("find-up"));
15
- const fs_extra_1 = __importDefault(require("fs-extra"));
16
- const read_yaml_file_1 = __importDefault(require("read-yaml-file"));
15
+ const fs_1 = __importDefault(require("fs"));
17
16
  const nameAtVersion_1 = require("./nameAtVersion");
18
17
  exports.nameAtVersion = nameAtVersion_1.nameAtVersion;
19
18
  const parsePnpmLock_1 = require("./parsePnpmLock");
@@ -27,7 +26,7 @@ async function parseLockFile(packageRoot) {
27
26
  return memoization[yarnLockPath];
28
27
  }
29
28
  const parseYarnLock = (await Promise.resolve().then(() => __importStar(require("@yarnpkg/lockfile")))).parse;
30
- const yarnLock = fs_extra_1.default.readFileSync(yarnLockPath).toString();
29
+ const yarnLock = fs_1.default.readFileSync(yarnLockPath, 'utf-8');
31
30
  const parsed = parseYarnLock(yarnLock);
32
31
  memoization[yarnLockPath] = parsed;
33
32
  return parsed;
@@ -38,7 +37,8 @@ async function parseLockFile(packageRoot) {
38
37
  if (memoization[pnpmLockPath]) {
39
38
  return memoization[pnpmLockPath];
40
39
  }
41
- const yaml = await read_yaml_file_1.default(pnpmLockPath);
40
+ const readYamlFile = require("read-yaml-file");
41
+ const yaml = (await readYamlFile(pnpmLockPath));
42
42
  const parsed = parsePnpmLock_1.parsePnpmLock(yaml);
43
43
  memoization[pnpmLockPath] = parsed;
44
44
  return memoization[pnpmLockPath];
@@ -51,7 +51,7 @@ async function parseLockFile(packageRoot) {
51
51
  }
52
52
  let npmLockJson;
53
53
  try {
54
- npmLockJson = fs_extra_1.default.readFileSync(npmLockPath);
54
+ npmLockJson = fs_1.default.readFileSync(npmLockPath, 'utf-8');
55
55
  }
56
56
  catch (_a) {
57
57
  throw new Error("Couldn’t parse package-lock.json.");
@@ -1,25 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const implementations_1 = require("./implementations");
4
- const pnpm_1 = require("./implementations/pnpm");
5
- const yarn_1 = require("./implementations/yarn");
6
- const rush_1 = require("./implementations/rush");
7
- const npm_1 = require("./implementations/npm");
8
- const lerna_1 = require("./implementations/lerna");
9
- const workspaceGetter = {
10
- yarn: yarn_1.getYarnWorkspaceRoot,
11
- pnpm: pnpm_1.getPnpmWorkspaceRoot,
12
- rush: rush_1.getRushWorkspaceRoot,
13
- npm: npm_1.getNpmWorkspaceRoot,
14
- lerna: lerna_1.getLernaWorkspaceRoot,
15
- };
16
- const preferred = process.env
17
- .PREFERRED_WORKSPACE_MANAGER;
4
+ const preferred = process.env.PREFERRED_WORKSPACE_MANAGER;
18
5
  function getWorkspaceRoot(cwd) {
19
6
  const workspaceImplementation = preferred || implementations_1.getWorkspaceImplementation(cwd);
20
- if (!workspaceImplementation || !workspaceGetter[workspaceImplementation]) {
7
+ if (!workspaceImplementation) {
21
8
  return;
22
9
  }
23
- return workspaceGetter[workspaceImplementation](cwd);
10
+ switch (workspaceImplementation) {
11
+ case "yarn":
12
+ return require(`./implementations/yarn`).getYarnWorkspaceRoot(cwd);
13
+ case "pnpm":
14
+ return require(`./implementations/pnpm`).getPnpmWorkspaceRoot(cwd);
15
+ case "rush":
16
+ return require(`./implementations/rush`).getRushWorkspaceRoot(cwd);
17
+ case "npm":
18
+ return require(`./implementations/npm`).getNpmWorkspaceRoot(cwd);
19
+ case "lerna":
20
+ return require(`./implementations/lerna`).getLernaWorkspaceRoot(cwd);
21
+ }
24
22
  }
25
23
  exports.getWorkspaceRoot = getWorkspaceRoot;
@@ -1,25 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const implementations_1 = require("./implementations");
4
- const lerna_1 = require("./implementations/lerna");
5
- const npm_1 = require("./implementations/npm");
6
- const pnpm_1 = require("./implementations/pnpm");
7
- const rush_1 = require("./implementations/rush");
8
- const yarn_1 = require("./implementations/yarn");
9
- const workspaceGetter = {
10
- yarn: yarn_1.getYarnWorkspaces,
11
- pnpm: pnpm_1.getPnpmWorkspaces,
12
- rush: rush_1.getRushWorkspaces,
13
- npm: npm_1.getNpmWorkspaces,
14
- lerna: lerna_1.getLernaWorkspaces,
15
- };
16
- const preferred = process.env
17
- .PREFERRED_WORKSPACE_MANAGER;
4
+ const preferred = process.env.PREFERRED_WORKSPACE_MANAGER;
18
5
  function getWorkspaces(cwd) {
19
6
  const workspaceImplementation = preferred || implementations_1.getWorkspaceImplementation(cwd);
20
- if (!workspaceImplementation || !workspaceGetter[workspaceImplementation]) {
7
+ if (!workspaceImplementation) {
21
8
  return [];
22
9
  }
23
- return workspaceGetter[workspaceImplementation](cwd);
10
+ switch (workspaceImplementation) {
11
+ case "yarn":
12
+ return require(`./implementations/yarn`).getYarnWorkspaces(cwd);
13
+ case "pnpm":
14
+ return require(`./implementations/pnpm`).getPnpmWorkspaces(cwd);
15
+ case "rush":
16
+ return require(`./implementations/rush`).getRushWorkspaces(cwd);
17
+ case "npm":
18
+ return require(`./implementations/npm`).getNpmWorkspaces(cwd);
19
+ case "lerna":
20
+ return require(`./implementations/lerna`).getLernaWorkspaces(cwd);
21
+ }
24
22
  }
25
23
  exports.getWorkspaces = getWorkspaces;
@@ -1,2 +1,10 @@
1
- export declare type WorkspaceImplementations = "yarn" | "pnpm" | "rush" | "npm" | 'lerna';
1
+ export declare type WorkspaceImplementations = "yarn" | "pnpm" | "rush" | "npm" | "lerna";
2
+ export interface ImplementationAndLockFile {
3
+ implementation: WorkspaceImplementations | undefined;
4
+ lockFile: string;
5
+ }
6
+ export declare function getWorkspaceImplementationAndLockFile(cwd: string): {
7
+ implementation: WorkspaceImplementations | undefined;
8
+ lockFile: string;
9
+ } | undefined;
2
10
  export declare function getWorkspaceImplementation(cwd: string): WorkspaceImplementations | undefined;
@@ -4,28 +4,55 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const find_up_1 = __importDefault(require("find-up"));
7
- function getWorkspaceImplementation(cwd) {
8
- // This needs to come before Yarn and NPM because
9
- // lerna can be used with either package manager
10
- const lernaJsonPath = find_up_1.default.sync("lerna.json", { cwd });
11
- if (lernaJsonPath) {
12
- return "lerna";
13
- }
14
- const yarnLockPath = find_up_1.default.sync("yarn.lock", { cwd });
15
- if (yarnLockPath) {
16
- return "yarn";
17
- }
18
- const pnpmLockPath = find_up_1.default.sync("pnpm-workspace.yaml", { cwd });
19
- if (pnpmLockPath) {
20
- return "pnpm";
7
+ const path_1 = __importDefault(require("path"));
8
+ const cache = {};
9
+ function getWorkspaceImplementationAndLockFile(cwd) {
10
+ if (cache[cwd]) {
11
+ return cache[cwd];
21
12
  }
22
- const rushJsonPath = find_up_1.default.sync("rush.json", { cwd });
23
- if (rushJsonPath) {
24
- return "rush";
13
+ const lockFile = find_up_1.default.sync(["lerna.json", "yarn.lock", "pnpm-workspace.yaml", "rush.json", "package-lock.json"], {
14
+ cwd,
15
+ });
16
+ if (!lockFile) {
17
+ return;
25
18
  }
26
- const npmLockPath = find_up_1.default.sync("package-lock.json", { cwd });
27
- if (npmLockPath) {
28
- return "npm";
19
+ switch (path_1.default.basename(lockFile)) {
20
+ case "lerna.json":
21
+ cache[cwd] = {
22
+ implementation: "lerna",
23
+ lockFile,
24
+ };
25
+ break;
26
+ case "yarn.lock":
27
+ cache[cwd] = {
28
+ implementation: "yarn",
29
+ lockFile,
30
+ };
31
+ break;
32
+ case "pnpm-workspace.yaml":
33
+ cache[cwd] = {
34
+ implementation: "pnpm",
35
+ lockFile,
36
+ };
37
+ break;
38
+ case "rush.json":
39
+ cache[cwd] = {
40
+ implementation: "rush",
41
+ lockFile,
42
+ };
43
+ break;
44
+ case "package-lock.json":
45
+ cache[cwd] = {
46
+ implementation: "npm",
47
+ lockFile,
48
+ };
49
+ break;
29
50
  }
51
+ return cache[cwd];
52
+ }
53
+ exports.getWorkspaceImplementationAndLockFile = getWorkspaceImplementationAndLockFile;
54
+ function getWorkspaceImplementation(cwd) {
55
+ var _a;
56
+ return (_a = getWorkspaceImplementationAndLockFile(cwd)) === null || _a === void 0 ? void 0 : _a.implementation;
30
57
  }
31
58
  exports.getWorkspaceImplementation = getWorkspaceImplementation;
@@ -3,13 +3,15 @@ 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
- const find_yarn_workspace_root_1 = __importDefault(require("find-yarn-workspace-root"));
7
6
  const fs_1 = __importDefault(require("fs"));
8
7
  const path_1 = __importDefault(require("path"));
8
+ const _1 = require(".");
9
9
  const getPackagePaths_1 = require("../../getPackagePaths");
10
10
  const getWorkspacePackageInfo_1 = require("../getWorkspacePackageInfo");
11
11
  function getPackageJsonWorkspaceRoot(cwd) {
12
- const packageJsonWorkspacesRoot = find_yarn_workspace_root_1.default(cwd);
12
+ var _a;
13
+ const lockFile = (_a = _1.getWorkspaceImplementationAndLockFile(cwd)) === null || _a === void 0 ? void 0 : _a.lockFile;
14
+ const packageJsonWorkspacesRoot = lockFile ? path_1.default.dirname(lockFile) : cwd;
13
15
  return packageJsonWorkspacesRoot;
14
16
  }
15
17
  exports.getPackageJsonWorkspaceRoot = getPackageJsonWorkspaceRoot;
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const path_1 = __importDefault(require("path"));
7
7
  const find_up_1 = __importDefault(require("find-up"));
8
- const read_yaml_file_1 = require("read-yaml-file");
9
8
  const getPackagePaths_1 = require("../../getPackagePaths");
10
9
  const getWorkspacePackageInfo_1 = require("../getWorkspacePackageInfo");
11
10
  function getPnpmWorkspaceRoot(cwd) {
@@ -20,7 +19,8 @@ function getPnpmWorkspaces(cwd) {
20
19
  try {
21
20
  const pnpmWorkspacesRoot = getPnpmWorkspaceRoot(cwd);
22
21
  const pnpmWorkspacesFile = path_1.default.join(pnpmWorkspacesRoot, "pnpm-workspace.yaml");
23
- const pnpmWorkspaces = read_yaml_file_1.sync(pnpmWorkspacesFile);
22
+ const readYaml = require("read-yaml-file").sync;
23
+ const pnpmWorkspaces = readYaml(pnpmWorkspacesFile);
24
24
  const packagePaths = getPackagePaths_1.getPackagePaths(pnpmWorkspacesRoot, pnpmWorkspaces.packages);
25
25
  const workspaceInfo = getWorkspacePackageInfo_1.getWorkspacePackageInfo(packagePaths);
26
26
  return workspaceInfo;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workspace-tools",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,8 +19,6 @@
19
19
  "dependencies": {
20
20
  "@yarnpkg/lockfile": "^1.1.0",
21
21
  "find-up": "^4.1.0",
22
- "find-yarn-workspace-root": "^1.2.1",
23
- "fs-extra": "^9.0.0",
24
22
  "git-url-parse": "^11.1.2",
25
23
  "globby": "^11.0.0",
26
24
  "jju": "^1.4.0",
@@ -28,7 +26,6 @@
28
26
  "read-yaml-file": "^2.0.0"
29
27
  },
30
28
  "devDependencies": {
31
- "@types/fs-extra": "^8.1.0",
32
29
  "@types/git-url-parse": "^9.0.0",
33
30
  "@types/glob": "^7.1.1",
34
31
  "@types/jest": "^25.2.2",