@vnejs/monorepo 0.0.2 → 0.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/monorepo",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Monorepo tooling for @vnejs",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -0,0 +1,86 @@
1
+ const { execSync } = require("child_process");
2
+ const { existsSync, readFileSync } = require("fs");
3
+ const path = require("path");
4
+
5
+ const readPackageJson = (pkgDir) => JSON.parse(readFileSync(path.join(pkgDir, "package.json"), "utf8"));
6
+
7
+ const findMonorepoRoot = (start = process.cwd()) => {
8
+ let dir = path.resolve(start);
9
+
10
+ while (true) {
11
+ const pkgPath = path.join(dir, "package.json");
12
+
13
+ if (existsSync(pkgPath)) {
14
+ const pkg = readPackageJson(dir);
15
+ if (pkg.workspaces) return dir;
16
+ }
17
+
18
+ const parent = path.dirname(dir);
19
+ if (parent === dir) break;
20
+ dir = parent;
21
+ }
22
+
23
+ return path.resolve(start);
24
+ };
25
+
26
+ const getChangedFiles = (root, ref = "HEAD") => {
27
+ const files = new Set();
28
+
29
+ execSync(`git -C ${JSON.stringify(root)} diff --name-only ${ref}`, { encoding: "utf8" })
30
+ .split("\n")
31
+ .filter(Boolean)
32
+ .forEach((file) => files.add(file));
33
+
34
+ execSync(`git -C ${JSON.stringify(root)} ls-files --others --exclude-standard`, { encoding: "utf8" })
35
+ .split("\n")
36
+ .filter(Boolean)
37
+ .forEach((file) => files.add(file));
38
+
39
+ return [...files];
40
+ };
41
+
42
+ const findPackageForFile = (root, filePath) => {
43
+ let dir = path.dirname(path.resolve(root, filePath));
44
+
45
+ while (dir.startsWith(root)) {
46
+ const pkgPath = path.join(dir, "package.json");
47
+
48
+ if (existsSync(pkgPath)) {
49
+ const pkg = readPackageJson(dir);
50
+ if (dir === root) return null;
51
+
52
+ return { name: pkg.name, dir: path.relative(root, dir) || "." };
53
+ }
54
+
55
+ if (dir === root) break;
56
+ dir = path.dirname(dir);
57
+ }
58
+
59
+ return null;
60
+ };
61
+
62
+ const getChangedPackages = (options = {}) => {
63
+ const root = findMonorepoRoot(options.root);
64
+ const packages = new Map();
65
+
66
+ for (const file of getChangedFiles(root, options.ref ?? "HEAD")) {
67
+ const pkg = findPackageForFile(root, file);
68
+ if (pkg?.name) packages.set(pkg.name, pkg);
69
+ }
70
+
71
+ let result = [...packages.values()].sort((a, b) => a.name.localeCompare(b.name));
72
+
73
+ if (options.script) {
74
+ result = result.filter((pkg) => {
75
+ const pkgJson = readPackageJson(path.join(root, pkg.dir));
76
+ return Boolean(pkgJson.scripts?.[options.script]);
77
+ });
78
+ }
79
+
80
+ return { root, packages: result };
81
+ };
82
+
83
+ module.exports = {
84
+ findMonorepoRoot,
85
+ getChangedPackages,
86
+ };
package/src/cli.js CHANGED
@@ -7,6 +7,8 @@ const publishAction = require("./publish");
7
7
  const publishUisAction = require("./publish-uis");
8
8
  const publishHelpersAction = require("./publish-helpers");
9
9
  const publishConfigsAction = require("./publish-configs");
10
+ const { getChangedPackages } = require("./changed-packages");
11
+ const runChangedAction = require("./run-changed");
10
12
 
11
13
  const program = new Command();
12
14
 
@@ -59,4 +61,25 @@ program
59
61
  .option("--tag <tag>", "npm publish --tag", "latest")
60
62
  .action(publishConfigsAction);
61
63
 
64
+ program
65
+ .command("changed-packages")
66
+ .description("List workspace packages changed in git")
67
+ .option("--ref <ref>", "git ref to compare against", "HEAD")
68
+ .option("--script <script>", "only packages that define this npm script")
69
+ .action((options) => {
70
+ const { packages } = getChangedPackages({ ref: options.ref, script: options.script });
71
+
72
+ packages.forEach((pkg) => console.log(`${pkg.name}\t${pkg.dir}`));
73
+ console.log("");
74
+ console.log(`Total: ${packages.length}`);
75
+ });
76
+
77
+ program
78
+ .command("run-changed")
79
+ .description("Run npm script sequentially in git-changed workspace packages")
80
+ .argument("<script>", "npm script name from package.json")
81
+ .option("--ref <ref>", "git ref to compare against", "HEAD")
82
+ .option("--dry-run", "list packages without running the script", false)
83
+ .action(runChangedAction);
84
+
62
85
  program.parse();
package/src/index.js CHANGED
@@ -4,4 +4,6 @@ module.exports = {
4
4
  publishUis: require("./publish-uis"),
5
5
  publishHelpers: require("./publish-helpers"),
6
6
  publishConfigs: require("./publish-configs"),
7
+ getChangedPackages: require("./changed-packages").getChangedPackages,
8
+ runChanged: require("./run-changed"),
7
9
  };
@@ -0,0 +1,24 @@
1
+ const path = require("path");
2
+
3
+ const { getChangedPackages } = require("../changed-packages");
4
+ const { run } = require("../publish/utils");
5
+
6
+ module.exports = (script, options = {}) => {
7
+ const { root, packages } = getChangedPackages({ ref: options.ref, script });
8
+
9
+ if (!packages.length) {
10
+ console.log(`No changed packages with script "${script}".`);
11
+ return;
12
+ }
13
+
14
+ for (const pkg of packages) {
15
+ const pkgDir = path.join(root, pkg.dir);
16
+ console.log(`> ${pkg.name} (${pkg.dir})`);
17
+
18
+ if (options.dryRun) continue;
19
+
20
+ run(`npm run ${script}`, pkgDir);
21
+ }
22
+
23
+ if (options.dryRun) console.log(`\nTotal: ${packages.length}`);
24
+ };