Add --config, --help, and --version flags to edit-docs

- Implement --config (-c) flag to allow custom configuration file paths.
- Add --help (-h) flag to display tool usage and available options.
- Add --version (-v) flag to display the current Trilium version.
- Update electron-start.mts to correctly pass command-line arguments to Electron.
- Synchronize edit-docs version with the root package.json via update-version.ts.
- Resolve config paths relative to the configuration file's directory.

This makes edit-docs more robust and easier to use from external projects
and immutable environments like Nix.
This commit is contained in:
Wael Nasreddine 2026-01-10 12:09:18 -08:00
parent 015e41e792
commit fb4d63b049
4 changed files with 67 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@triliumnext/edit-docs", "name": "@triliumnext/edit-docs",
"version": "0.0.1", "version": "0.101.3",
"private": true, "private": true,
"description": "Desktop version of Trilium which imports the demo database (presented to new users at start-up) or the user guide and other documentation and saves the modifications for committing.", "description": "Desktop version of Trilium which imports the demo database (presented to new users at start-up) or the user guide and other documentation and saves the modifications for committing.",
"dependencies": { "dependencies": {

View File

@ -10,6 +10,7 @@ import fsExtra from "fs-extra";
import yaml from "js-yaml"; import yaml from "js-yaml";
import path from "path"; import path from "path";
import packageJson from "../package.json" with { type: "json" };
import { extractZip, importData, initializeDatabase, startElectron } from "./utils.js"; import { extractZip, importData, initializeDatabase, startElectron } from "./utils.js";
interface NoteMapping { interface NoteMapping {
@ -25,26 +26,82 @@ interface Config {
noteMappings: NoteMapping[]; noteMappings: NoteMapping[];
} }
// Configuration variables // Parse command-line arguments
function parseArgs() {
const args = process.argv.slice(2);
let configPath: string | undefined;
let showHelp = false;
let showVersion = false;
for (let i = 0; i < args.length; i++) {
if (args[i] === '--config' || args[i] === '-c') {
configPath = args[i + 1];
i++; // Skip the next argument as it's the value
} else if (args[i] === '--help' || args[i] === '-h') {
showHelp = true;
} else if (args[i] === '--version' || args[i] === '-v') {
showVersion = true;
}
}
return { configPath, showHelp, showVersion };
}
function getVersion(): string {
return packageJson.version;
}
function printHelp() {
const version = getVersion();
console.log(`
Usage: trilium-edit-docs [options]
Options:
-c, --config <path> Path to the configuration file (default: edit-docs-config.yaml in the root)
-h, --help Display this help message
-v, --version Display version information
Version: ${version}
`);
}
function printVersion() {
const version = getVersion();
console.log(version);
}
const { configPath, showHelp, showVersion } = parseArgs();
if (showHelp) {
printHelp();
process.exit(0);
} else if (showVersion) {
printVersion();
process.exit(0);
}
// Configuration variables to be initialized
let BASE_URL: string; let BASE_URL: string;
let NOTE_MAPPINGS: NoteMapping[]; let NOTE_MAPPINGS: NoteMapping[];
// Load configuration from edit-docs-config.yaml // Load configuration from edit-docs-config.yaml
async function loadConfig() { async function loadConfig() {
const CONFIG_PATH = path.join(__dirname, "../../../edit-docs-config.yaml"); const CONFIG_PATH = configPath
? path.resolve(configPath)
: path.join(__dirname, "../../../edit-docs-config.yaml");
const configContent = await fs.readFile(CONFIG_PATH, "utf-8"); const configContent = await fs.readFile(CONFIG_PATH, "utf-8");
const config = yaml.load(configContent) as Config; const config = yaml.load(configContent) as Config;
BASE_URL = config.baseUrl; BASE_URL = config.baseUrl;
// Resolve all paths relative to the repository root // Resolve all paths relative to the config file's directory (for flexibility with external configs)
const REPO_ROOT = path.join(__dirname, "../../.."); const CONFIG_DIR = path.dirname(CONFIG_PATH);
NOTE_MAPPINGS = config.noteMappings.map((mapping) => ({ NOTE_MAPPINGS = config.noteMappings.map((mapping) => ({
...mapping, ...mapping,
path: path.join(REPO_ROOT, mapping.path) path: path.resolve(CONFIG_DIR, mapping.path)
})); }));
} }
async function main() { async function main() {
await loadConfig(); await loadConfig();
const initializedPromise = startElectron(() => { const initializedPromise = startElectron(() => {

View File

@ -3,7 +3,8 @@ import { getElectronPath, isNixOS } from "./utils.mjs";
const LD_LIBRARY_PATH = isNixOS() && execSync("nix eval --raw nixpkgs#gcc.cc.lib").toString("utf-8") + "/lib"; const LD_LIBRARY_PATH = isNixOS() && execSync("nix eval --raw nixpkgs#gcc.cc.lib").toString("utf-8") + "/lib";
execSync(`${getElectronPath()} ${process.argv[2]} --no-sandbox`, { const args = process.argv.slice(2);
execSync(`${getElectronPath()} ${args.join(" ")} --no-sandbox`, {
stdio: "inherit", stdio: "inherit",
env: { env: {
...process.env, ...process.env,

View File

@ -26,7 +26,7 @@ function getVersion(packageJsonPath: string) {
function main() { function main() {
const version = getVersion(join(__dirname, "..", "package.json")); const version = getVersion(join(__dirname, "..", "package.json"));
for (const appName of ["server", "client", "desktop"]) { for (const appName of ["server", "client", "desktop", "edit-docs"]) {
patchPackageJson(join(__dirname, "..", "apps", appName, "package.json"), version); patchPackageJson(join(__dirname, "..", "apps", appName, "package.json"), version);
} }