-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathextract-extension-data.mjs
More file actions
133 lines (117 loc) · 3.35 KB
/
Copy pathextract-extension-data.mjs
File metadata and controls
133 lines (117 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
import {isAbsolute, join} from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import {DOCS_GEN_DIR, EDITOR_PKG_DIR, REPO_ROOT} from './config.mjs';
import {ExtensionExtractor} from './extractor/index.mjs';
import {logger} from './logger.mjs';
/**
* Resolves a path from the repository root.
*/
function resolveFromRoot(path) {
return isAbsolute(path) ? path : join(REPO_ROOT, path);
}
/**
* Creates default CLI options.
*/
function createDefaultOptions() {
return {
editorPkg: EDITOR_PKG_DIR,
outDir: DOCS_GEN_DIR,
only: null,
};
}
/**
* Reads a required option value.
*/
function readOptionValue(args, index, optionName) {
const value = args[index + 1];
if (!value || value.startsWith('--')) {
throw new Error(`Missing value for ${optionName}`);
}
return value;
}
/**
* Parses comma-separated extension names.
*/
function parseOnlyOption(value) {
return value
.split(',')
.map((item) => item.trim())
.filter(Boolean);
}
/**
* Applies one CLI option to a new options object.
*/
function applyOption(opts, args, index) {
const arg = args[index];
switch (arg) {
case '--editor-pkg':
return {
nextIndex: index + 1,
opts: {...opts, editorPkg: resolveFromRoot(readOptionValue(args, index, arg))},
};
case '--out-dir':
return {
nextIndex: index + 1,
opts: {...opts, outDir: resolveFromRoot(readOptionValue(args, index, arg))},
};
case '--only':
return {
nextIndex: index + 1,
opts: {...opts, only: parseOnlyOption(readOptionValue(args, index, arg))},
};
case '--help':
return {nextIndex: index, opts: {...opts, help: true}};
default:
throw new Error(`Unknown option: ${arg}`);
}
}
/**
* Parses CLI options for extension data extraction.
*/
export function parseArgs(args = process.argv.slice(2)) {
let opts = createDefaultOptions();
for (let index = 0; index < args.length; index++) {
const arg = args[index];
if (arg === '--') return opts;
const parsedOption = applyOption(opts, args, index);
opts = parsedOption.opts;
index = parsedOption.nextIndex;
}
return opts;
}
/**
* Prints command usage information.
*/
function printHelp() {
logger.info('Usage: pnpm --filter @markdown-editor/docs-gen run extract [options]');
logger.info('');
logger.info('Options:');
logger.info(' --only Bold,Link Extract selected extension names');
logger.info(' --out-dir tmp/docs-gen Override output directory');
logger.info(' --editor-pkg path Override packages/editor path');
}
/**
* Runs extension data extraction from CLI arguments.
*/
export function main(args = process.argv.slice(2)) {
const opts = parseArgs(args);
if (opts.help) {
printHelp();
return;
}
new ExtensionExtractor({
editorPkg: opts.editorPkg,
outDir: opts.outDir,
repoRoot: REPO_ROOT,
}).run({only: opts.only});
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
try {
main();
} catch (error) {
logger.error(error);
process.exit(1);
}
}