@docubook/cli 0.2.3 → 0.2.4
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 +7 -7
- package/src/cli/program.js +61 -1
- package/src/index.js +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docubook/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "DocuBook CLI tool that helps you initialize, update, and deploy documentation directly from your terminal.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -12,11 +12,6 @@
|
|
|
12
12
|
"docubook": "src/index.js"
|
|
13
13
|
},
|
|
14
14
|
"main": "./src/index.js",
|
|
15
|
-
"scripts": {
|
|
16
|
-
"dev": "node src/index.js",
|
|
17
|
-
"lint": "eslint src/",
|
|
18
|
-
"lint:fix": "eslint src/ --fix"
|
|
19
|
-
},
|
|
20
15
|
"keywords": [
|
|
21
16
|
"docubook",
|
|
22
17
|
"documentation",
|
|
@@ -47,5 +42,10 @@
|
|
|
47
42
|
},
|
|
48
43
|
"engines": {
|
|
49
44
|
"node": ">=18.0.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"dev": "node src/index.js",
|
|
48
|
+
"lint": "eslint src/",
|
|
49
|
+
"lint:fix": "eslint src/ --fix"
|
|
50
50
|
}
|
|
51
|
-
}
|
|
51
|
+
}
|
package/src/cli/program.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* global fetch */
|
|
1
2
|
import { program } from "commander";
|
|
2
3
|
import { collectUserInput } from "./promptHandler.js";
|
|
3
4
|
import { createProject } from "../installer/projectInstaller.js";
|
|
@@ -6,6 +7,8 @@ import { renderWelcome, renderDone, renderError } from "../tui/renderer.js";
|
|
|
6
7
|
import { CLIState } from "../tui/state.js";
|
|
7
8
|
import { detectPackageManager, getPackageManagerInfo, getPackageManagerVersion } from "../utils/packageManagerDetect.js";
|
|
8
9
|
import { getAvailableTemplates, getTemplate, getDefaultTemplate } from "../utils/templateDetect.js";
|
|
10
|
+
import { execSync } from "child_process";
|
|
11
|
+
import ora from "ora";
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
14
|
* Initializes the CLI program
|
|
@@ -14,7 +17,64 @@ import { getAvailableTemplates, getTemplate, getDefaultTemplate } from "../utils
|
|
|
14
17
|
export function initializeProgram(version) {
|
|
15
18
|
program
|
|
16
19
|
.version(version)
|
|
17
|
-
.description("CLI to create a new DocuBook project")
|
|
20
|
+
.description("CLI to create a new DocuBook project");
|
|
21
|
+
|
|
22
|
+
// Add `update` command: check npm registry and install latest globally if needed
|
|
23
|
+
program
|
|
24
|
+
.command("update")
|
|
25
|
+
.description("Check for updates and install the latest DocuBook CLI globally")
|
|
26
|
+
.action(async () => {
|
|
27
|
+
const pkgName = "@docubook/cli";
|
|
28
|
+
// declare spinner in outer scope so catch block can safely reference it
|
|
29
|
+
let spinner;
|
|
30
|
+
try {
|
|
31
|
+
// Fetch package metadata from npm registry
|
|
32
|
+
const encoded = encodeURIComponent(pkgName);
|
|
33
|
+
spinner = ora('Checking for updates...').start();
|
|
34
|
+
const res = await fetch(`https://registry.npmjs.org/${encoded}`);
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
spinner.fail(`Failed to fetch registry metadata (status ${res.status})`);
|
|
37
|
+
throw new Error(`Failed to fetch registry metadata (status ${res.status})`);
|
|
38
|
+
}
|
|
39
|
+
const data = await res.json();
|
|
40
|
+
const latest = data && data["dist-tags"] && data["dist-tags"].latest;
|
|
41
|
+
if (!latest) {
|
|
42
|
+
spinner.fail("Could not determine latest version from npm registry");
|
|
43
|
+
throw new Error("Could not determine latest version from npm registry");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Stop spinner and print a plain "Checking for updates..." line (no check mark)
|
|
47
|
+
if (spinner && typeof spinner.stop === 'function') spinner.stop();
|
|
48
|
+
console.log('Checking for updates...');
|
|
49
|
+
|
|
50
|
+
if (latest === version) {
|
|
51
|
+
console.log(`No update needed, current version is ${version}, fetched latest release is ${latest}`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(`Updating ${pkgName} from ${version} to ${latest}...`);
|
|
56
|
+
|
|
57
|
+
// Use npm to install globally. This will stream stdout/stderr to the user.
|
|
58
|
+
const cmd = `npm install -g ${pkgName}@${latest}`;
|
|
59
|
+
try {
|
|
60
|
+
execSync(cmd, { stdio: "inherit" });
|
|
61
|
+
console.log(`Successfully updated to ${latest}`);
|
|
62
|
+
} catch (installErr) {
|
|
63
|
+
// If install fails, provide a helpful message
|
|
64
|
+
console.error(`Update failed: ${installErr.message || installErr}`);
|
|
65
|
+
console.error(`Try running the following command manually:\n ${cmd}\nIf you see permissions errors, consider running with elevated privileges or using a Node version manager.`);
|
|
66
|
+
process.exitCode = 1;
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
// ensure spinner is stopped on error
|
|
70
|
+
if (spinner && typeof spinner.stop === 'function') spinner.stop();
|
|
71
|
+
console.error(err.message || err);
|
|
72
|
+
process.exitCode = 1;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Default behavior (create project)
|
|
77
|
+
program
|
|
18
78
|
.argument("[directory]", "The name of the project directory")
|
|
19
79
|
.action(async (directory) => {
|
|
20
80
|
const state = new CLIState();
|
package/src/index.js
CHANGED
|
@@ -14,6 +14,14 @@ const packageJsonPath = path.join(__dirname, '..', 'package.json');
|
|
|
14
14
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
15
15
|
const VERSION = packageJson.version;
|
|
16
16
|
|
|
17
|
+
// Handle --version / -V early to print custom output
|
|
18
|
+
const args = process.argv.slice(2);
|
|
19
|
+
if (args.includes('--version') || args.includes('-V')) {
|
|
20
|
+
console.log(`DocuBook CLI ${VERSION}`);
|
|
21
|
+
console.log("Run 'docubook update' to check for updates.");
|
|
22
|
+
process.exit(0);
|
|
23
|
+
}
|
|
24
|
+
|
|
17
25
|
// Initialize and parse CLI arguments
|
|
18
26
|
const program = initializeProgram(VERSION);
|
|
19
27
|
program.parse(process.argv);
|