nanosights-cli 0.1.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/dist/cli.js ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ import { Command } from "commander";
5
+ import inquirer from "inquirer";
6
+ import { spawnSync } from "child_process";
7
+ var MAP = {
8
+ analytics: "nano-analytics",
9
+ insights: "nano-insights",
10
+ custom: "nano-custom"
11
+ };
12
+ var program = new Command();
13
+ program.name("nanos").description("NanoSights CLI").version("0.1.0");
14
+ program.command("install [packages...]").action((pkgs) => run(pkgs, "install"));
15
+ program.command("update [packages...]").action((pkgs) => run(pkgs, "update"));
16
+ program.command("remove [packages...]").action((pkgs) => run(pkgs, "remove"));
17
+ program.parse();
18
+ async function run(args, action) {
19
+ const packages = args.length === 0 ? await interactive(action) : parse(args);
20
+ for (const pkg of packages) runNpm(pkg, action);
21
+ }
22
+ async function interactive(action) {
23
+ const { items } = await inquirer.prompt([
24
+ {
25
+ type: "checkbox",
26
+ name: "items",
27
+ message: `What do you want to ${action}?`,
28
+ choices: Object.keys(MAP)
29
+ }
30
+ ]);
31
+ return items.map((k) => ({ name: MAP[k] }));
32
+ }
33
+ function parse(args) {
34
+ return args.map((a) => {
35
+ const [k, v] = a.split("@");
36
+ if (!MAP[k]) throw new Error(`Unknown package: ${k}`);
37
+ return { name: MAP[k], version: v };
38
+ });
39
+ }
40
+ function runNpm(pkg, action) {
41
+ if (action === "install")
42
+ exec(["install", spec(pkg)]);
43
+ if (action === "update")
44
+ pkg.version ? exec(["install", spec(pkg)]) : exec(["update", pkg.name]);
45
+ if (action === "remove")
46
+ exec(["uninstall", pkg.name]);
47
+ }
48
+ function spec(pkg) {
49
+ return pkg.version ? `${pkg.name}@${pkg.version}` : pkg.name;
50
+ }
51
+ function exec(args) {
52
+ const r = spawnSync("npm", args, { stdio: "inherit" });
53
+ if (r.status !== 0) process.exit(1);
54
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "nanosights-cli",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "nanos": "dist/cli.js"
9
+ },
10
+ "scripts": {
11
+ "build": "tsup src/cli.ts --format esm --out-dir dist --clean",
12
+ "dev": "tsx src/cli.ts"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/aspecsweb/nanosights-cli.git"
17
+ },
18
+ "keywords": [],
19
+ "author": "",
20
+ "license": "ISC",
21
+ "bugs": {
22
+ "url": "https://github.com/aspecsweb/nanosights-cli/issues"
23
+ },
24
+ "homepage": "https://github.com/aspecsweb/nanosights-cli#readme",
25
+ "dependencies": {
26
+ "commander": "^14.0.2",
27
+ "inquirer": "^12.11.1"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^25.0.10",
31
+ "tsup": "^8.5.1",
32
+ "typescript": "^5.9.3"
33
+ }
34
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from "commander";
3
+ import inquirer from "inquirer";
4
+ import { spawnSync } from "node:child_process";
5
+
6
+ type Action = "install" | "update" | "remove";
7
+
8
+ type Package = {
9
+ name: string;
10
+ version?: string;
11
+ };
12
+
13
+ const MAP: Record<string, string> = {
14
+ analytics: "nano-analytics",
15
+ insights: "nano-insights",
16
+ custom: "nano-custom",
17
+ };
18
+
19
+ const program = new Command();
20
+
21
+ program
22
+ .name("nanos")
23
+ .description("NanoSights CLI")
24
+ .version("0.1.0");
25
+
26
+ program.command("install [packages...]")
27
+ .action(pkgs => run(pkgs, "install"));
28
+
29
+ program.command("update [packages...]")
30
+ .action(pkgs => run(pkgs, "update"));
31
+
32
+ program.command("remove [packages...]")
33
+ .action(pkgs => run(pkgs, "remove"));
34
+
35
+ program.parse();
36
+
37
+ async function run(args: string[], action: Action) {
38
+ const packages =
39
+ args.length === 0
40
+ ? await interactive(action)
41
+ : parse(args);
42
+
43
+ for (const pkg of packages) runNpm(pkg, action);
44
+ }
45
+
46
+ async function interactive(action: Action): Promise<Package[]> {
47
+ const { items } = await inquirer.prompt([
48
+ {
49
+ type: "checkbox",
50
+ name: "items",
51
+ message: `What do you want to ${action}?`,
52
+ choices: Object.keys(MAP),
53
+ },
54
+ ]);
55
+
56
+ return items.map((k: string) => ({ name: MAP[k] }));
57
+ }
58
+
59
+ function parse(args: string[]): Package[] {
60
+ return args.map(a => {
61
+ const [k, v] = a.split("@");
62
+ if (!MAP[k]) throw new Error(`Unknown package: ${k}`);
63
+ return { name: MAP[k], version: v };
64
+ });
65
+ }
66
+
67
+ function runNpm(pkg: Package, action: Action) {
68
+ if (action === "install")
69
+ exec(["install", spec(pkg)]);
70
+
71
+ if (action === "update")
72
+ pkg.version
73
+ ? exec(["install", spec(pkg)])
74
+ : exec(["update", pkg.name]);
75
+
76
+ if (action === "remove")
77
+ exec(["uninstall", pkg.name]);
78
+ }
79
+
80
+ function spec(pkg: Package) {
81
+ return pkg.version ? `${pkg.name}@${pkg.version}` : pkg.name;
82
+ }
83
+
84
+ function exec(args: string[]) {
85
+ const r = spawnSync("npm", args, { stdio: "inherit" });
86
+ if (r.status !== 0) process.exit(1);
87
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "outDir": "dist",
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true
10
+ },
11
+ "include": ["src"]
12
+ }