@digigov/cli-lab 2.0.0 → 2.0.2-0138f8bd

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
@@ -1,6 +1,18 @@
1
1
  {
2
2
  "name": "@digigov/cli-lab",
3
3
  "entries": [
4
+ {
5
+ "version": "2.0.1",
6
+ "tag": "@digigov/cli-lab_v2.0.1",
7
+ "date": "Tue, 19 Dec 2023 15:00:14 GMT",
8
+ "comments": {
9
+ "patch": [
10
+ {
11
+ "comment": "Upgrade rush and fs-extra"
12
+ }
13
+ ]
14
+ }
15
+ },
4
16
  {
5
17
  "version": "2.0.0",
6
18
  "tag": "@digigov/cli-lab_v2.0.0",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Change Log - @digigov/cli-lab
2
2
 
3
- This log was last generated on Fri, 15 Dec 2023 15:23:56 GMT and should not be manually modified.
3
+ This log was last generated on Tue, 19 Dec 2023 15:00:14 GMT and should not be manually modified.
4
+
5
+ ## 2.0.1
6
+ Tue, 19 Dec 2023 15:00:14 GMT
7
+
8
+ ### Patches
9
+
10
+ - Upgrade rush and fs-extra
4
11
 
5
12
  ## 2.0.0
6
13
  Fri, 15 Dec 2023 15:23:56 GMT
package/index.mjs CHANGED
@@ -1,19 +1,21 @@
1
1
  #!/usr/bin/env zx
2
2
  import "zx-extra";
3
3
  import "./lib/extra/index.mjs";
4
- async function runScript(script){
5
- const scriptLocation = path.join(__dirname, 'scripts', script+'.mjs')
6
- await spawn(`zx`, [scriptLocation], {stdio: 'inherit'})
4
+ import { spawnSync } from "child_process";
7
5
 
6
+ async function runScript(script) {
7
+ const scriptLocation = path.join(__dirname, "scripts", script + ".mjs");
8
+ spawnSync(`zx`, [scriptLocation], { stdio: "inherit" });
8
9
  }
9
- if(argv.script) {
10
- await runScript(argv.script)
11
- }else {
12
- const scripts = (await find(
13
- path.join(__dirname, 'scripts', '*.mjs')
14
- )).map(
15
- filePath => filePath.replace(path.join(__dirname, 'scripts/'), '').replace('.mjs', '')
16
- )
17
- const script = await select('Select script', scripts)
18
- await runScript(script)
10
+ if (argv.script) {
11
+ await runScript(argv.script);
12
+ } else {
13
+ const scripts = (await find(path.join(__dirname, "scripts", "*.mjs"))).map(
14
+ (filePath) =>
15
+ filePath
16
+ .replace(path.join(__dirname, "scripts/"), "")
17
+ .replace(".mjs", ""),
18
+ );
19
+ const script = await select("Select script", scripts);
20
+ await runScript(script);
19
21
  }
@@ -1,24 +1,45 @@
1
1
  #!/usr/bin/env zx
2
2
  import "zx-extra";
3
3
  import "../extra/index.mjs";
4
- export default async function changedPackages(targetBranch) {
5
- const rushJSON = await readJSON(await findUp('rush.json'));
6
- const getDiffedPackages = reduceScripts(targetBranch, rushJSON.projects);
7
- return getDiffedPackages;
4
+ import { spawn } from "child_process";
5
+
6
+ export default async function getChangedPackages(targetBranch) {
7
+ const rushJSON = await readJSON(await findUp("rush.json"));
8
+ return await diffAllPackages(targetBranch, rushJSON.projects);
8
9
  }
9
10
 
10
- function reduceScripts(customTargetBranch, projects) {
11
- let targetBranch = customTargetBranch || process.env.CI_DEFAULT_BRANCH || 'origin/main';
12
- targetBranch = `origin/${targetBranch.replace(/^origin\//, '')}`;
13
- return projects.reduce((allPackages, project) => {
14
- const {
15
- projectFolder
16
- } = project
17
- const { stdout } = spawn('git', ['diff', '--merge-base', targetBranch, '--', projectFolder])
18
- if (!stdout.toString().trim()) {
19
- return allPackages
20
- }
21
- allPackages.push(projectFolder)
22
- return allPackages;
23
- }, [])
11
+ async function diffAllPackages(customTargetBranch, projects) {
12
+ let targetBranch =
13
+ customTargetBranch || process.env.CI_DEFAULT_BRANCH || "origin/main";
14
+ targetBranch = targetBranch.replace(/^origin\//, "");
15
+
16
+ const allPackages = await Promise.all(
17
+ projects.map((project) => {
18
+ return new Promise((resolve, reject) => {
19
+ const { projectFolder } = project;
20
+ const diffProcess = spawn("git", [
21
+ "diff",
22
+ "--merge-base",
23
+ targetBranch,
24
+ "--",
25
+ projectFolder,
26
+ ]);
27
+ let output = "";
28
+ diffProcess.stdout.on("data", (data) => {
29
+ output += data.toString();
30
+ });
31
+ diffProcess.stderr.on("data", (data) => {
32
+ reject(data.toString());
33
+ });
34
+
35
+ diffProcess.on("close", (code) => {
36
+ if (code !== 0) return reject(`git diff exited with code ${code}`);
37
+ if (output.trim()) return resolve(projectFolder);
38
+ else return resolve(null);
39
+ });
40
+ });
41
+ }),
42
+ );
43
+
44
+ return allPackages.filter(Boolean);
24
45
  }
@@ -1,80 +1,115 @@
1
- import { $ } from 'zx';
2
- import inquirer from 'inquirer';
3
- import glob from 'glob';
4
- import path from 'path';
5
- import { spawnSync } from 'child_process';
6
- import fs from 'fs-extra';
1
+ import { $ } from "zx";
2
+ import * as inquirer from "@inquirer/prompts";
3
+ import glob from "globby";
4
+ import path from "path";
5
+ import { spawnSync } from "child_process";
6
+ import fs from "fs-extra";
7
+
7
8
  const errorOffset = 1000;
9
+
10
+ /**
11
+ * Create a CLI command
12
+ *
13
+ * This creates a proxy object that allows you to call CLI commands
14
+ * as if they were functions. Under the hood, it uses `spawnSync` to
15
+ * run the command and return the result
16
+ *
17
+ * @param {string} commandName
18
+ * @returns {Proxy} a proxy object for the CLI command
19
+ */
8
20
  function createCli(commandName) {
9
- return new Proxy({}, {
10
- get: (_, command) => {
11
- return async function (args, options) {
12
- if (args) {
13
- args = args.split(' ');
14
- } else {
15
- args = []
16
- }
17
- const result = spawnSync(commandName, [command, ...args], { shell: true, stdio: options?.json || options?.stdout ? 'pipe' : ['inherit', 'inherit', 'pipe'], env: options?.env || process.env });
18
- if (result.status !== 0 && result.stderr) {
19
- const errMsg = result.stderr.toString();
20
- if (errMsg.includes('Error: ')) {
21
- const idx = errMsg.substring(errMsg.indexOf('Error: '));
22
- throw new Error(errMsg.substring(idx, idx + errorOffset))
21
+ return new Proxy(
22
+ {},
23
+ {
24
+ get: (_, command) => {
25
+ return async function (args, options) {
26
+ if (args) {
27
+ args = args.split(" ");
23
28
  } else {
24
- throw new Error(result.stderr.toString());
29
+ args = [];
25
30
  }
26
- }
27
- if (options?.json) {
28
- return JSON.parse(result.stdout);
29
- }
30
- if (options?.stdout) {
31
- return result.stdout.toString();
32
- }
33
- }
34
-
35
- }
36
- })
31
+ const result = spawnSync(commandName, [command, ...args], {
32
+ shell: true,
33
+ stdio:
34
+ options?.json || options?.stdout
35
+ ? "pipe"
36
+ : ["inherit", "inherit", "pipe"],
37
+ env: options?.env || process.env,
38
+ });
39
+ if (result.status !== 0 && result.stderr) {
40
+ const errMsg = result.stderr.toString();
41
+ if (errMsg.includes("Error: ")) {
42
+ const idx = errMsg.substring(errMsg.indexOf("Error: "));
43
+ throw new Error(errMsg.substring(idx, idx + errorOffset));
44
+ } else {
45
+ throw new Error(result.stderr.toString());
46
+ }
47
+ }
48
+ if (options?.json) {
49
+ return JSON.parse(result.stdout);
50
+ }
51
+ if (options?.stdout) {
52
+ return result.stdout.toString();
53
+ }
54
+ };
55
+ },
56
+ },
57
+ );
37
58
  }
38
- console.clear();
39
59
  global.find = async (patterns, options) => {
40
60
  return glob.sync(patterns, options);
41
61
  };
42
62
  global.mkdir = async (dir) => {
43
- fs.mkdirSync(dir)
44
- }
63
+ fs.mkdirSync(dir);
64
+ };
45
65
  global.rm = async (fileOrDir) => {
46
- console.log('removing ' + fileOrDir)
47
- return fs.rmdir(fileOrDir, {
48
- recursive: true
49
- })
50
- }
51
- global.confirm = async (question) => (await inquirer.prompt({ type: 'confirm', message: question, name: 'var' })).var;
52
- global.question = async (question, value) => (await inquirer.prompt({
53
- type: 'input', message: question, name: 'var', default: value
54
- })).var;
55
- global.password = async (question) => (await inquirer.prompt({ type: 'password', message: question, name: 'var' })).var;
66
+ console.log("removing " + fileOrDir);
67
+ return fs.rm(fileOrDir, {
68
+ recursive: true,
69
+ });
70
+ };
71
+ global.confirm = (question) => inquirer.confirm({ message: question });
72
+ global.question = (question, value) =>
73
+ inquirer.input({
74
+ message: question,
75
+ default: value,
76
+ });
77
+ global.password = (question) => inquirer.password({ message: question });
56
78
 
57
- global.select = async (question, choices, values) => (
58
- await inquirer.prompt({
59
- type: 'list', message: question, name: 'var', choices,
60
- default: values
61
- })).var;
62
- global.checkbox = async (question, choices, values) => (await inquirer.prompt({
63
- type: 'checkbox', message: question, name: 'var', choices,
64
- default: values
65
- })).var;
79
+ global.select = (question, choices, values) =>
80
+ inquirer.select({
81
+ message: question,
82
+ choices,
83
+ default: values,
84
+ });
85
+ global.checkbox = (question, choices) =>
86
+ inquirer.checkbox({
87
+ message: question,
88
+ choices,
89
+ });
66
90
 
67
- global.cli = new Proxy({}, {
68
- get: (_, commandName) => {
69
- return createCli(commandName);
70
- }
71
- });
91
+ global.cli = new Proxy(
92
+ {},
93
+ {
94
+ get: (_, commandName) => {
95
+ return createCli(commandName);
96
+ },
97
+ },
98
+ );
72
99
  global.pwd = async () => (await $`pwd`).stdout;
73
100
  global.path = path;
74
- global.cd = (p) => { process.chdir(p); };
75
- global.readJSON = async (file) => { return fs.readJSONSync(file); };
76
- global.readFile = async (file) => { return fs.readFileSync(file).toString(); };
77
- global.writeFile = async (file, content) => { return fs.outputFileSync(file, content); };
101
+ global.cd = (p) => {
102
+ process.chdir(p);
103
+ };
104
+ global.readJSON = async (file) => {
105
+ return fs.readJSONSync(file);
106
+ };
107
+ global.readFile = async (file) => {
108
+ return fs.readFileSync(file).toString();
109
+ };
110
+ global.writeFile = async (file, content) => {
111
+ return fs.outputFileSync(file, content);
112
+ };
78
113
  global.findUp = function findFileInParentDirectories(fileName, rootDir) {
79
114
  let currentDir = rootDir || process.cwd();
80
115
  while (currentDir) {
@@ -101,10 +136,9 @@ global.requireEnv = async (key, required, options) => {
101
136
  return val;
102
137
  };
103
138
 
104
- global.spawn = spawnSync
139
+ global.spawn = spawnSync;
105
140
 
106
- process.on('SIGINT', function () {
141
+ process.on("SIGINT", function () {
107
142
  console.log("Caught interrupt signal");
108
143
  process.exit();
109
-
110
- });
144
+ });
package/package.json CHANGED
@@ -1,25 +1,27 @@
1
1
  {
2
2
  "name": "@digigov/cli-lab",
3
- "version": "2.0.0",
3
+ "version": "2.0.2-0138f8bd",
4
4
  "description": "",
5
5
  "main": "index.mjs",
6
6
  "type": "module",
7
7
  "directories": {
8
8
  "lib": "lib"
9
9
  },
10
- "scripts": {
11
- "publint": "publint",
12
- "lab": "zx index.mjs"
13
- },
14
10
  "author": "",
15
11
  "license": "BSD-2-Clause",
16
12
  "dependencies": {
17
- "@microsoft/rush-lib": "5.83.3",
18
- "zx": "2.0.0",
19
- "zx-extra": "0.0.4",
20
- "inquirer": "8.1.2",
21
- "glob": "7.1.6",
22
- "fs-extra": "10.0.0",
13
+ "@microsoft/rush-lib": "5.151.0",
14
+ "zx": "7.2.3",
15
+ "zx-extra": "3.0.23",
16
+ "@inquirer/prompts": "7.0.1",
17
+ "globby": "11.0.0",
18
+ "fs-extra": "11.2.0"
19
+ },
20
+ "devDependencies": {
23
21
  "publint": "0.1.8"
22
+ },
23
+ "scripts": {
24
+ "publint": "publint",
25
+ "lab": "zx index.mjs"
24
26
  }
25
- }
27
+ }