@doki-land/nifty 0.0.0 → 0.0.1

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.
@@ -1,16 +1,16 @@
1
- import { existsSync } from "node:fs";
2
- import { dirname, join } from "node:path";
3
-
4
- /** Resolve npm next to the active Node binary (Windows needs `npm.cmd`). */
5
- export function resolveNpmExecutable(): string {
6
- if (process.env.NIFTY_NPM) {
7
- return process.env.NIFTY_NPM;
8
- }
9
-
10
- const sibling = join(dirname(process.execPath), process.platform === "win32" ? "npm.cmd" : "npm");
11
- if (existsSync(sibling)) {
12
- return sibling;
13
- }
14
-
15
- return process.platform === "win32" ? "npm.cmd" : "npm";
16
- }
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+
4
+ /** Resolve npm next to the active Node binary (Windows needs `npm.cmd`). */
5
+ export function resolveNpmExecutable(): string {
6
+ if (process.env.NIFTY_NPM) {
7
+ return process.env.NIFTY_NPM;
8
+ }
9
+
10
+ const sibling = join(dirname(process.execPath), process.platform === "win32" ? "npm.cmd" : "npm");
11
+ if (existsSync(sibling)) {
12
+ return sibling;
13
+ }
14
+
15
+ return process.platform === "win32" ? "npm.cmd" : "npm";
16
+ }
@@ -0,0 +1,35 @@
1
+ import type { Cli, ParsedOptions } from "@vmz/commander";
2
+
3
+ import { loadNiftyNative } from "../native.js";
4
+ import { authPayload } from "./authArgs.js";
5
+ import { bootstrapFromOptions } from "./context.js";
6
+ import { printTrustReport } from "./trust.js";
7
+ import { authFromOptions, cwdFrom, flag, str } from "./options.js";
8
+
9
+ export function registerTrustCommand(cli: Cli): void {
10
+ cli.command("trust", "cli.cmd.trust")
11
+ .option("--dry-run", "cli.opt.dry-run")
12
+ .option("--refresh", "cli.opt.refresh")
13
+ .option("--only <package>", "cli.opt.only")
14
+ .option("--otp <code>", "cli.opt.otp")
15
+ .option("--totp-secret <secret>", "cli.opt.totp-secret")
16
+ .option("--npm-token <token>", "cli.opt.npm-token")
17
+ .action(async (options) => cmdTrust(options));
18
+ }
19
+
20
+ export async function cmdTrust(options: ParsedOptions): Promise<number> {
21
+ await bootstrapFromOptions(options);
22
+ const native = loadNiftyNative();
23
+ const report = native.publisher["trust-workspace"]({
24
+ cwd: cwdFrom(options),
25
+ dryRun: flag(options, "dry-run"),
26
+ refresh: flag(options, "refresh"),
27
+ only: str(options, "only"),
28
+ ...authPayload(authFromOptions(options)),
29
+ });
30
+ printTrustReport(report);
31
+ if (report.failed.length > 0) {
32
+ throw new Error(`trust failed for ${report.failed.length} package(s)`);
33
+ }
34
+ return 0;
35
+ }
package/src/cli/trust.ts CHANGED
@@ -1,77 +1,83 @@
1
- import { loadNiftyNative } from "../native.js";
2
- import { authPayload, parseAuthArgs } from "./authArgs.js";
3
-
4
- export type TrustReport = {
5
- root: string;
6
- configured: string[];
7
- skipped: string[];
8
- failed: string[];
9
- };
10
-
11
- export async function runTrust(argv: string[]): Promise<void> {
12
- const options = parseTrustArgs(argv);
13
- const native = loadNiftyNative();
14
- const report = native.publisher["trust-workspace"]({
15
- cwd: options.cwd,
16
- dryRun: options.dryRun,
17
- only: options.only,
18
- ...authPayload(options.auth),
19
- });
20
- printReport(report);
21
- }
22
-
23
- function parseTrustArgs(argv: string[]) {
24
- let cwd: string | undefined;
25
- let dryRun = false;
26
- let only: string | undefined;
27
- const auth = parseAuthArgs(argv);
28
-
29
- for (let i = 0; i < argv.length; i += 1) {
30
- const arg = argv[i];
31
- if (arg === "--dry-run") {
32
- dryRun = true;
33
- } else if (arg === "-C" || arg === "--cwd") {
34
- cwd = argv[++i];
35
- } else if (arg === "--only") {
36
- only = argv[++i];
37
- } else if (arg === "-h" || arg === "--help") {
38
- printTrustHelp();
39
- process.exit(0);
40
- }
41
- }
42
-
43
- return { cwd, dryRun, only, auth };
44
- }
45
-
46
- function printReport(report: TrustReport): void {
47
- console.log(`trust workspace ${report.root}`);
48
- console.log(` configured: ${report.configured.length}`);
49
- for (const name of report.configured) {
50
- console.log(` + ${name}`);
51
- }
52
- console.log(` skipped: ${report.skipped.length}`);
53
- for (const name of report.skipped) {
54
- console.log(` = ${name}`);
55
- }
56
- if (report.failed.length > 0) {
57
- console.log(` failed: ${report.failed.length}`);
58
- for (const name of report.failed) {
59
- console.log(` ! ${name}`);
60
- }
61
- }
62
- }
63
-
64
- function printTrustHelp(): void {
65
- console.log(`nifty trust — configure npm Trusted Publisher for workspace packages
66
-
67
- Reads NPM_TOTP_SECRET / NPM_OTP / NPM_TOKEN from env or .env.placeholder.local.
68
-
69
- Usage:
70
- nifty trust
71
- nifty trust --only @doki-land/nifty
72
- nifty trust --dry-run
73
- nifty trust --totp-secret <base32>
74
- nifty trust --otp 123456
75
- nifty trust -C <cwd>
76
- `);
77
- }
1
+ import { loadNiftyNative } from "../native.js";
2
+ import { authPayload, parseAuthArgs } from "./authArgs.js";
3
+
4
+ export type TrustReport = {
5
+ root: string;
6
+ configured: string[];
7
+ skipped: string[];
8
+ failed: string[];
9
+ };
10
+
11
+ export async function runTrust(argv: string[]): Promise<void> {
12
+ const options = parseTrustArgs(argv);
13
+ const native = loadNiftyNative();
14
+ const report = native.publisher["trust-workspace"]({
15
+ cwd: options.cwd,
16
+ dryRun: options.dryRun,
17
+ refresh: options.refresh,
18
+ only: options.only,
19
+ ...authPayload(options.auth),
20
+ });
21
+ printTrustReport(report);
22
+ }
23
+
24
+ function parseTrustArgs(argv: string[]) {
25
+ let cwd: string | undefined;
26
+ let dryRun = false;
27
+ let refresh = false;
28
+ let only: string | undefined;
29
+ const auth = parseAuthArgs(argv);
30
+
31
+ for (let i = 0; i < argv.length; i += 1) {
32
+ const arg = argv[i];
33
+ if (arg === "--dry-run") {
34
+ dryRun = true;
35
+ } else if (arg === "--refresh") {
36
+ refresh = true;
37
+ } else if (arg === "-C" || arg === "--cwd") {
38
+ cwd = argv[++i];
39
+ } else if (arg === "--only") {
40
+ only = argv[++i];
41
+ } else if (arg === "-h" || arg === "--help") {
42
+ printTrustHelp();
43
+ process.exit(0);
44
+ }
45
+ }
46
+
47
+ return { cwd, dryRun, refresh, only, auth };
48
+ }
49
+
50
+ export function printTrustReport(report: TrustReport): void {
51
+ console.log(`trust workspace ${report.root}`);
52
+ console.log(` configured: ${report.configured.length}`);
53
+ for (const name of report.configured) {
54
+ console.log(` + ${name}`);
55
+ }
56
+ console.log(` skipped: ${report.skipped.length}`);
57
+ for (const name of report.skipped) {
58
+ console.log(` = ${name}`);
59
+ }
60
+ if (report.failed.length > 0) {
61
+ console.log(` failed: ${report.failed.length}`);
62
+ for (const name of report.failed) {
63
+ console.log(` ! ${name}`);
64
+ }
65
+ }
66
+ }
67
+
68
+ function printTrustHelp(): void {
69
+ console.log(`nifty trust — configure npm Trusted Publisher for workspace packages
70
+
71
+ Reads NPM_TOTP_SECRET / NPM_OTP / NPM_TOKEN from env or .env.placeholder.local.
72
+ Trust list cache lives in .cache/npm-placeholder.json.
73
+
74
+ Usage:
75
+ nifty trust
76
+ nifty trust --only @doki-land/nifty
77
+ nifty trust --dry-run
78
+ nifty trust --refresh
79
+ nifty trust --totp-secret <base32>
80
+ nifty trust --otp 123456
81
+ nifty trust -C <cwd>
82
+ `);
83
+ }
@@ -0,0 +1,20 @@
1
+ import type { Cli, ParsedOptions } from "@vmz/commander";
2
+
3
+ import { bootstrapFromOptions } from "./context.js";
4
+ import { updateWorkspace } from "./update.js";
5
+ import { cwdFrom, flag } from "./options.js";
6
+
7
+ export function registerUpdateCommand(cli: Cli): void {
8
+ cli.command("update", "cli.cmd.update")
9
+ .option("-i, --interactive", "cli.opt.interactive")
10
+ .action(async (options) => cmdUpdate(options));
11
+ }
12
+
13
+ export async function cmdUpdate(options: ParsedOptions): Promise<number> {
14
+ await bootstrapFromOptions(options);
15
+ await updateWorkspace({
16
+ interactive: flag(options, "interactive"),
17
+ cwd: cwdFrom(options),
18
+ });
19
+ return 0;
20
+ }
package/src/cli/update.ts CHANGED
@@ -1,182 +1,185 @@
1
- import { spawnSync } from "node:child_process";
2
- import { stdin as input, stdout as output } from "node:process";
3
- import { createInterface } from "node:readline/promises";
4
- import { dirname } from "node:path";
5
-
6
- import { detectProjectLayout, discoverPackageDirs } from "../config/detectProject.js";
7
-
8
- type UpdateOptions = {
9
- interactive: boolean;
10
- cwd?: string;
11
- };
12
-
13
- type CargoUpgrade = {
14
- crate: string;
15
- from: string;
16
- to: string;
17
- };
18
-
19
- type NpmOutdated = {
20
- name: string;
21
- current: string;
22
- wanted: string;
23
- latest: string;
24
- };
25
-
26
- export async function runUpdate(argv: string[]): Promise<void> {
27
- const options = parseUpdateArgs(argv);
28
- const cwd = options.cwd ?? process.cwd();
29
- const layout = detectProjectLayout(cwd);
30
-
31
- if (layout.kind === "unknown") {
32
- throw new Error(`no Cargo.toml or package.json found from ${cwd}`);
33
- }
34
-
35
- if (layout.kind === "cargo" || layout.kind === "hybrid") {
36
- await updateCargo(layout, options.interactive);
37
- }
38
- if (layout.kind === "npm" || layout.kind === "hybrid") {
39
- await updateNpm(layout, options.interactive);
40
- }
41
- }
42
-
43
- function parseUpdateArgs(argv: string[]): UpdateOptions {
44
- let interactive = false;
45
- let cwd: string | undefined;
46
- for (let i = 0; i < argv.length; i += 1) {
47
- const arg = argv[i];
48
- if (arg === "-i" || arg === "--interactive") {
49
- interactive = true;
50
- } else if (arg === "-C" || arg === "--cwd") {
51
- cwd = argv[++i];
52
- }
53
- }
54
- return { interactive, cwd };
55
- }
56
-
57
- function cargoRoot(layout: ReturnType<typeof detectProjectLayout>): string | undefined {
58
- return (
59
- layout.cargoWorkspaceRoot ??
60
- (layout.cargoManifest ? dirname(layout.cargoManifest) : undefined)
61
- );
62
- }
63
-
64
- async function updateCargo(layout: ReturnType<typeof detectProjectLayout>, interactive: boolean): Promise<void> {
65
- const root = cargoRoot(layout);
66
- if (!root) {
67
- throw new Error("cargo project not found");
68
- }
69
-
70
- const dry = spawnSync("cargo", ["upgrade", "--workspace", "--dry-run"], {
71
- cwd: root,
72
- encoding: "utf8",
73
- });
74
- const combined = `${dry.stdout ?? ""}${dry.stderr ?? ""}`;
75
- const upgrades = parseCargoUpgrades(combined);
76
- if (dry.status !== 0 && !combined.includes("no upgrades found") && upgrades.length === 0) {
77
- throw new Error(`cargo upgrade --dry-run failed:\n${combined}`);
78
- }
79
-
80
- if (upgrades.length === 0) {
81
- console.log("cargo: dependencies already up to date");
82
- return;
83
- }
84
-
85
- if (interactive) {
86
- const selected = await pickItems(
87
- "Select cargo upgrades",
88
- upgrades.map((item) => `${item.crate} ${item.from} -> ${item.to}`),
89
- );
90
- for (const index of selected) {
91
- const item = upgrades[index];
92
- run("cargo", ["upgrade", "--workspace", "-p", item.crate], root);
93
- }
94
- } else {
95
- run("cargo", ["upgrade", "--workspace"], root);
96
- }
97
- }
98
-
99
- async function updateNpm(layout: ReturnType<typeof detectProjectLayout>, interactive: boolean): Promise<void> {
100
- const packages = discoverPackageDirs(layout);
101
- if (packages.length === 0) {
102
- console.log("npm: no package.json directories found");
103
- return;
104
- }
105
- for (const packageDir of packages) {
106
- await updateNpmDir(packageDir, interactive);
107
- }
108
- }
109
-
110
- async function updateNpmDir(packageDir: string, interactive: boolean): Promise<void> {
111
- const outdated = listNpmOutdated(packageDir);
112
- if (outdated.length === 0) {
113
- console.log(`npm (${packageDir}): dependencies already up to date`);
114
- return;
115
- }
116
-
117
- if (interactive) {
118
- const selected = await pickItems(
119
- `Select npm upgrades (${packageDir})`,
120
- outdated.map((item) => `${item.name} ${item.current} -> ${item.wanted}`),
121
- );
122
- for (const index of selected) {
123
- const item = outdated[index];
124
- run("npm", ["install", `${item.name}@${item.wanted}`], packageDir);
125
- }
126
- } else {
127
- run("npm", ["update"], packageDir);
128
- }
129
- }
130
-
131
- function parseCargoUpgrades(text: string): CargoUpgrade[] {
132
- const upgrades: CargoUpgrade[] = [];
133
- for (const line of text.split(/\r?\n/)) {
134
- const match = line.match(/^\s*(\S+)\s+v(\S+)\s+->\s+v(\S+)/);
135
- if (match) {
136
- upgrades.push({ crate: match[1], from: match[2], to: match[3] });
137
- }
138
- }
139
- return upgrades;
140
- }
141
-
142
- function listNpmOutdated(packageDir: string): NpmOutdated[] {
143
- const result = spawnSync("npm", ["outdated", "--json"], { cwd: packageDir, encoding: "utf8" });
144
- if (!result.stdout?.trim()) {
145
- return [];
146
- }
147
- const parsed = JSON.parse(result.stdout) as Record<
148
- string,
149
- { current: string; wanted: string; latest: string }
150
- >;
151
- return Object.entries(parsed).map(([name, entry]) => ({
152
- name,
153
- current: entry.current,
154
- wanted: entry.wanted,
155
- latest: entry.latest,
156
- }));
157
- }
158
-
159
- async function pickItems(prompt: string, labels: string[]): Promise<number[]> {
160
- console.log(prompt);
161
- for (const [index, label] of labels.entries()) {
162
- console.log(` [${index}] ${label}`);
163
- }
164
- const rl = createInterface({ input, output });
165
- const answer = await rl.question("Indexes (comma-separated, empty = all): ");
166
- rl.close();
167
- const trimmed = answer.trim();
168
- if (!trimmed) {
169
- return labels.map((_, index) => index);
170
- }
171
- return trimmed
172
- .split(",")
173
- .map((part) => Number.parseInt(part.trim(), 10))
174
- .filter((index) => Number.isInteger(index) && index >= 0 && index < labels.length);
175
- }
176
-
177
- function run(command: string, args: string[], cwd: string): void {
178
- const result = spawnSync(command, args, { cwd, stdio: "inherit" });
179
- if (result.status !== 0) {
180
- throw new Error(`${command} ${args.join(" ")} failed`);
181
- }
182
- }
1
+ import { spawnSync } from "node:child_process";
2
+ import { stdin as input, stdout as output } from "node:process";
3
+ import { createInterface } from "node:readline/promises";
4
+ import { dirname } from "node:path";
5
+
6
+ import { detectProjectLayout, discoverPackageDirs } from "../config/detectProject.js";
7
+
8
+ type UpdateOptions = {
9
+ interactive: boolean;
10
+ cwd?: string;
11
+ };
12
+
13
+ type CargoUpgrade = {
14
+ crate: string;
15
+ from: string;
16
+ to: string;
17
+ };
18
+
19
+ type NpmOutdated = {
20
+ name: string;
21
+ current: string;
22
+ wanted: string;
23
+ latest: string;
24
+ };
25
+
26
+ export async function runUpdate(argv: string[]): Promise<void> {
27
+ await updateWorkspace(parseUpdateArgs(argv));
28
+ }
29
+
30
+ export async function updateWorkspace(options: UpdateOptions): Promise<void> {
31
+ const cwd = options.cwd ?? process.cwd();
32
+ const layout = detectProjectLayout(cwd);
33
+
34
+ if (layout.kind === "unknown") {
35
+ throw new Error(`no Cargo.toml or package.json found from ${cwd}`);
36
+ }
37
+
38
+ if (layout.kind === "cargo" || layout.kind === "hybrid") {
39
+ await updateCargo(layout, options.interactive);
40
+ }
41
+ if (layout.kind === "npm" || layout.kind === "hybrid") {
42
+ await updateNpm(layout, options.interactive);
43
+ }
44
+ }
45
+
46
+ function parseUpdateArgs(argv: string[]): UpdateOptions {
47
+ let interactive = false;
48
+ let cwd: string | undefined;
49
+ for (let i = 0; i < argv.length; i += 1) {
50
+ const arg = argv[i];
51
+ if (arg === "-i" || arg === "--interactive") {
52
+ interactive = true;
53
+ } else if (arg === "-C" || arg === "--cwd") {
54
+ cwd = argv[++i];
55
+ }
56
+ }
57
+ return { interactive, cwd };
58
+ }
59
+
60
+ function cargoRoot(layout: ReturnType<typeof detectProjectLayout>): string | undefined {
61
+ return (
62
+ layout.cargoWorkspaceRoot ??
63
+ (layout.cargoManifest ? dirname(layout.cargoManifest) : undefined)
64
+ );
65
+ }
66
+
67
+ async function updateCargo(layout: ReturnType<typeof detectProjectLayout>, interactive: boolean): Promise<void> {
68
+ const root = cargoRoot(layout);
69
+ if (!root) {
70
+ throw new Error("cargo project not found");
71
+ }
72
+
73
+ const dry = spawnSync("cargo", ["upgrade", "--workspace", "--dry-run"], {
74
+ cwd: root,
75
+ encoding: "utf8",
76
+ });
77
+ const combined = `${dry.stdout ?? ""}${dry.stderr ?? ""}`;
78
+ const upgrades = parseCargoUpgrades(combined);
79
+ if (dry.status !== 0 && !combined.includes("no upgrades found") && upgrades.length === 0) {
80
+ throw new Error(`cargo upgrade --dry-run failed:\n${combined}`);
81
+ }
82
+
83
+ if (upgrades.length === 0) {
84
+ console.log("cargo: dependencies already up to date");
85
+ return;
86
+ }
87
+
88
+ if (interactive) {
89
+ const selected = await pickItems(
90
+ "Select cargo upgrades",
91
+ upgrades.map((item) => `${item.crate} ${item.from} -> ${item.to}`),
92
+ );
93
+ for (const index of selected) {
94
+ const item = upgrades[index];
95
+ run("cargo", ["upgrade", "--workspace", "-p", item.crate], root);
96
+ }
97
+ } else {
98
+ run("cargo", ["upgrade", "--workspace"], root);
99
+ }
100
+ }
101
+
102
+ async function updateNpm(layout: ReturnType<typeof detectProjectLayout>, interactive: boolean): Promise<void> {
103
+ const packages = discoverPackageDirs(layout);
104
+ if (packages.length === 0) {
105
+ console.log("npm: no package.json directories found");
106
+ return;
107
+ }
108
+ for (const packageDir of packages) {
109
+ await updateNpmDir(packageDir, interactive);
110
+ }
111
+ }
112
+
113
+ async function updateNpmDir(packageDir: string, interactive: boolean): Promise<void> {
114
+ const outdated = listNpmOutdated(packageDir);
115
+ if (outdated.length === 0) {
116
+ console.log(`npm (${packageDir}): dependencies already up to date`);
117
+ return;
118
+ }
119
+
120
+ if (interactive) {
121
+ const selected = await pickItems(
122
+ `Select npm upgrades (${packageDir})`,
123
+ outdated.map((item) => `${item.name} ${item.current} -> ${item.wanted}`),
124
+ );
125
+ for (const index of selected) {
126
+ const item = outdated[index];
127
+ run("npm", ["install", `${item.name}@${item.wanted}`], packageDir);
128
+ }
129
+ } else {
130
+ run("npm", ["update"], packageDir);
131
+ }
132
+ }
133
+
134
+ function parseCargoUpgrades(text: string): CargoUpgrade[] {
135
+ const upgrades: CargoUpgrade[] = [];
136
+ for (const line of text.split(/\r?\n/)) {
137
+ const match = line.match(/^\s*(\S+)\s+v(\S+)\s+->\s+v(\S+)/);
138
+ if (match) {
139
+ upgrades.push({ crate: match[1], from: match[2], to: match[3] });
140
+ }
141
+ }
142
+ return upgrades;
143
+ }
144
+
145
+ function listNpmOutdated(packageDir: string): NpmOutdated[] {
146
+ const result = spawnSync("npm", ["outdated", "--json"], { cwd: packageDir, encoding: "utf8" });
147
+ if (!result.stdout?.trim()) {
148
+ return [];
149
+ }
150
+ const parsed = JSON.parse(result.stdout) as Record<
151
+ string,
152
+ { current: string; wanted: string; latest: string }
153
+ >;
154
+ return Object.entries(parsed).map(([name, entry]) => ({
155
+ name,
156
+ current: entry.current,
157
+ wanted: entry.wanted,
158
+ latest: entry.latest,
159
+ }));
160
+ }
161
+
162
+ async function pickItems(prompt: string, labels: string[]): Promise<number[]> {
163
+ console.log(prompt);
164
+ for (const [index, label] of labels.entries()) {
165
+ console.log(` [${index}] ${label}`);
166
+ }
167
+ const rl = createInterface({ input, output });
168
+ const answer = await rl.question("Indexes (comma-separated, empty = all): ");
169
+ rl.close();
170
+ const trimmed = answer.trim();
171
+ if (!trimmed) {
172
+ return labels.map((_, index) => index);
173
+ }
174
+ return trimmed
175
+ .split(",")
176
+ .map((part) => Number.parseInt(part.trim(), 10))
177
+ .filter((index) => Number.isInteger(index) && index >= 0 && index < labels.length);
178
+ }
179
+
180
+ function run(command: string, args: string[], cwd: string): void {
181
+ const result = spawnSync(command, args, { cwd, stdio: "inherit" });
182
+ if (result.status !== 0) {
183
+ throw new Error(`${command} ${args.join(" ")} failed`);
184
+ }
185
+ }