@alauda/doom 0.4.2 → 0.5.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/lib/cli/index.js +11 -6
- package/lib/cli/load-config.js +6 -2
- package/lib/cli/new.d.ts +44 -0
- package/lib/cli/new.js +185 -0
- package/package.json +5 -1
package/lib/cli/index.js
CHANGED
|
@@ -94,8 +94,9 @@ program
|
|
|
94
94
|
};
|
|
95
95
|
process.on('SIGINT', exitProcess);
|
|
96
96
|
process.on('SIGTERM', exitProcess);
|
|
97
|
-
})
|
|
98
|
-
|
|
97
|
+
});
|
|
98
|
+
program
|
|
99
|
+
.command('build')
|
|
99
100
|
.description('Build the documentation')
|
|
100
101
|
.argument('[root]', 'Root directory of the documentation')
|
|
101
102
|
.action(async function (root) {
|
|
@@ -108,8 +109,9 @@ program
|
|
|
108
109
|
appDirectory: CWD,
|
|
109
110
|
docDirectory,
|
|
110
111
|
});
|
|
111
|
-
})
|
|
112
|
-
|
|
112
|
+
});
|
|
113
|
+
program
|
|
114
|
+
.command('preview')
|
|
113
115
|
.alias('serve')
|
|
114
116
|
.description('Preview the built documentation')
|
|
115
117
|
.argument('[root]', 'Root directory of the documentation')
|
|
@@ -123,5 +125,8 @@ program
|
|
|
123
125
|
config,
|
|
124
126
|
...server,
|
|
125
127
|
});
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
+
});
|
|
129
|
+
program.command('new', 'Create a new documentation site/module/page', {
|
|
130
|
+
executableFile: 'new',
|
|
131
|
+
});
|
|
132
|
+
program.parseAsync().catch(console.error);
|
package/lib/cli/load-config.js
CHANGED
|
@@ -182,8 +182,12 @@ export async function loadConfig(root, configFile, version) {
|
|
|
182
182
|
else {
|
|
183
183
|
mergedConfig.i18nSourcePath = path.resolve(mergedConfig.root, I18N_FILE);
|
|
184
184
|
}
|
|
185
|
-
if (
|
|
186
|
-
mergedConfig.outDir =
|
|
185
|
+
if (mergedConfig.outDir) {
|
|
186
|
+
mergedConfig.outDir =
|
|
187
|
+
addTrailingSlash(mergedConfig.outDir) + normalizedVersion;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
mergedConfig.outDir = `dist${normalizedVersion === 'unversioned' ? `/unversioned${base}` : base}`;
|
|
187
191
|
}
|
|
188
192
|
if (mergedConfig.builderConfig?.server?.open === true) {
|
|
189
193
|
mergedConfig.builderConfig.server.open = base;
|
package/lib/cli/new.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as prompts from '@inquirer/prompts';
|
|
2
|
+
export declare const DEFAULT_PATH = "templates/scaffolding.yaml";
|
|
3
|
+
export interface ScaffoldingTemplate {
|
|
4
|
+
repo: string;
|
|
5
|
+
branches?: string[];
|
|
6
|
+
scaffoldingPath?: string;
|
|
7
|
+
}
|
|
8
|
+
type Prompts = Omit<typeof prompts, 'Separator'>;
|
|
9
|
+
type PromptOptions<T extends keyof Prompts> = Prompts[T] extends (config: infer R) => any ? R : never;
|
|
10
|
+
export interface ScaffoldingParameter<T extends keyof Prompts = keyof Prompts> {
|
|
11
|
+
name: string;
|
|
12
|
+
type: T;
|
|
13
|
+
options: PromptOptions<T>;
|
|
14
|
+
}
|
|
15
|
+
export type ScaffoldingLayoutProcessor = {
|
|
16
|
+
type: 'ejsTemplate';
|
|
17
|
+
};
|
|
18
|
+
export type ScaffoldingLayoutWriteMode = 'append' | 'write';
|
|
19
|
+
export interface ScaffoldingLayoutBase {
|
|
20
|
+
processors?: ScaffoldingLayoutProcessor[];
|
|
21
|
+
writeMode?: ScaffoldingLayoutWriteMode;
|
|
22
|
+
}
|
|
23
|
+
export interface ScaffoldingLayoutMatcher extends ScaffoldingLayoutBase {
|
|
24
|
+
match: string[];
|
|
25
|
+
}
|
|
26
|
+
export interface ScaffoldingLayoutFolder extends ScaffoldingLayoutBase {
|
|
27
|
+
type: 'folder';
|
|
28
|
+
matchers?: ScaffoldingLayoutMatcher[];
|
|
29
|
+
}
|
|
30
|
+
export interface ScaffoldingLayoutFile extends ScaffoldingLayoutBase {
|
|
31
|
+
type: 'file';
|
|
32
|
+
}
|
|
33
|
+
export type ScaffoldingLayout = {
|
|
34
|
+
source: string;
|
|
35
|
+
target: string;
|
|
36
|
+
when?: string;
|
|
37
|
+
} & (ScaffoldingLayoutFile | ScaffoldingLayoutFolder);
|
|
38
|
+
export interface Scaffolding {
|
|
39
|
+
name: string;
|
|
40
|
+
description?: string;
|
|
41
|
+
parameters?: ScaffoldingParameter[];
|
|
42
|
+
layout?: ScaffoldingLayout[];
|
|
43
|
+
}
|
|
44
|
+
export {};
|
package/lib/cli/new.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import * as prompts from '@inquirer/prompts';
|
|
5
|
+
import { logger } from '@rsbuild/core';
|
|
6
|
+
import { program } from 'commander';
|
|
7
|
+
import { render } from 'ejs';
|
|
8
|
+
import { simpleGit } from 'simple-git';
|
|
9
|
+
import { glob } from 'tinyglobby';
|
|
10
|
+
import { cyan, magenta } from 'yoctocolors';
|
|
11
|
+
import { resolveStaticConfig } from '../utils/helpers.js';
|
|
12
|
+
export const DEFAULT_PATH = 'templates/scaffolding.yaml';
|
|
13
|
+
const scaffoldingTemplates = {
|
|
14
|
+
'product-doc': {
|
|
15
|
+
repo: 'alauda/product-doc-guide',
|
|
16
|
+
branches: ['master', 'feat/templates'],
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const scaffoldingsFolder = path.resolve(os.homedir(), '.doom/scaffoldings');
|
|
20
|
+
const getScaffoldings = async (repoFolder, scaffoldingPath, branch) => {
|
|
21
|
+
const scaffoldingFile = path.resolve(repoFolder, scaffoldingPath);
|
|
22
|
+
try {
|
|
23
|
+
const stat = await fs.stat(scaffoldingFile);
|
|
24
|
+
if (stat.isFile()) {
|
|
25
|
+
const { scaffolding } = await resolveStaticConfig(scaffoldingFile);
|
|
26
|
+
return scaffolding;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (err_) {
|
|
30
|
+
const err = err_;
|
|
31
|
+
if (err.name === 'YAMLParseError') {
|
|
32
|
+
logger.error(`Failed to parse \`${magenta(scaffoldingPath)}\` on branch \`${magenta(branch)}\`: ${err.message}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const resolveScaffoldings = async (name, force) => {
|
|
37
|
+
if (!Object.hasOwn(scaffoldingTemplates, name)) {
|
|
38
|
+
logger.error(`Template \`${magenta(name)}\` not found, current available templates are: ${Object.keys(scaffoldingTemplates)
|
|
39
|
+
.map((t) => cyan(`\`${t}\``))
|
|
40
|
+
.join(', ')}`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const repoFolder = path.resolve(scaffoldingsFolder, name);
|
|
44
|
+
const { repo, branches: branches_, scaffoldingPath = 'templates/scaffolding.yaml', } = scaffoldingTemplates[name];
|
|
45
|
+
let branches = branches_;
|
|
46
|
+
let created = false;
|
|
47
|
+
try {
|
|
48
|
+
const stat = await fs.stat(path.resolve(repoFolder, '.git'));
|
|
49
|
+
if (stat.isDirectory()) {
|
|
50
|
+
created = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// ignore
|
|
55
|
+
}
|
|
56
|
+
if (!created) {
|
|
57
|
+
await fs.mkdir(repoFolder, { recursive: true });
|
|
58
|
+
}
|
|
59
|
+
const git = simpleGit(repoFolder);
|
|
60
|
+
if (!created) {
|
|
61
|
+
await git.clone(/^((\w+):)?\/\//.test(repo)
|
|
62
|
+
? repo
|
|
63
|
+
: `https://gitlab-ce.alauda.cn/${repo}`, repoFolder, ['--depth', '1']);
|
|
64
|
+
}
|
|
65
|
+
let currentBranch = (await git.raw(['branch', '--show-current'])).trim();
|
|
66
|
+
let scaffoldings;
|
|
67
|
+
const base = path.resolve(repoFolder, scaffoldingPath, '..');
|
|
68
|
+
if (!force &&
|
|
69
|
+
(scaffoldings = await getScaffoldings(repoFolder, scaffoldingPath, currentBranch))) {
|
|
70
|
+
return { base, scaffoldings };
|
|
71
|
+
}
|
|
72
|
+
if (!branches?.length) {
|
|
73
|
+
const defaultBranch = (await git.raw(['rev-parse', '--abbrev-ref', 'origin/HEAD']))
|
|
74
|
+
.trim()
|
|
75
|
+
.replace('origin/', '');
|
|
76
|
+
branches = [defaultBranch];
|
|
77
|
+
}
|
|
78
|
+
const options = ['--depth', '1', '--force'];
|
|
79
|
+
for (const branch of branches) {
|
|
80
|
+
if (branch === currentBranch) {
|
|
81
|
+
await git.pull([...options, '--no-rebase']);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
await git.fetch('origin', `${branch}:${branch}`, options);
|
|
85
|
+
await git.checkout((currentBranch = branch));
|
|
86
|
+
}
|
|
87
|
+
const scaffoldings = await getScaffoldings(repoFolder, scaffoldingPath, branch);
|
|
88
|
+
if (scaffoldings) {
|
|
89
|
+
return { base, scaffoldings };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const handleTemplateFile = async ({ source, target, processors, parameters, writeMode = 'write', }) => {
|
|
94
|
+
let content = await fs.readFile(source, 'utf-8');
|
|
95
|
+
for (const processor of processors || []) {
|
|
96
|
+
switch (processor.type) {
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
98
|
+
case 'ejsTemplate': {
|
|
99
|
+
content = render(content, { parameters });
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
default: {
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
104
|
+
throw new Error(`Unknown processor type: \`${processor.type}\``);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
109
|
+
await fs[`${writeMode}File`](target, content);
|
|
110
|
+
};
|
|
111
|
+
program
|
|
112
|
+
.argument('[template]', 'Scaffolding template name, format: `[name][:type]`')
|
|
113
|
+
.option('-f, --force [boolean]', 'Force to fetch latest scaffolding template, otherwise use local cache', (value) => !!value && value !== 'false', false)
|
|
114
|
+
.action(async (template = '', { force }) => {
|
|
115
|
+
let [name, type] = template.split(':');
|
|
116
|
+
if (!name) {
|
|
117
|
+
name = 'product-doc';
|
|
118
|
+
}
|
|
119
|
+
const { base, scaffoldings } = (await resolveScaffoldings(name, force)) || {};
|
|
120
|
+
if (!scaffoldings) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (!type) {
|
|
124
|
+
type = await prompts.select({
|
|
125
|
+
message: '请选择脚手架类型',
|
|
126
|
+
choices: scaffoldings.map((s) => ({
|
|
127
|
+
value: s.name,
|
|
128
|
+
description: s.description,
|
|
129
|
+
})),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const scaffolding = scaffoldings.find((s) => s.name === type);
|
|
133
|
+
if (!scaffolding) {
|
|
134
|
+
logger.error(`Scaffolding \`${magenta(type)}\` not found, current available scaffoldings are: ${scaffoldings
|
|
135
|
+
.map((s) => cyan(`\`${s.name}\``))
|
|
136
|
+
.join(', ')}`);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const parameters = {};
|
|
140
|
+
for (const param of scaffolding.parameters || []) {
|
|
141
|
+
parameters[param.name] = await prompts[param.type](param.options);
|
|
142
|
+
}
|
|
143
|
+
for (const layout of scaffolding.layout || []) {
|
|
144
|
+
const source = path.resolve(base, render(layout.source, { parameters }));
|
|
145
|
+
const target = path.resolve(render(layout.target, { parameters }));
|
|
146
|
+
const when = layout.when && render(layout.when, { parameters });
|
|
147
|
+
if (['', 'null', 'undefined', 'false'].includes(when)) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
switch (layout.type) {
|
|
151
|
+
case 'file': {
|
|
152
|
+
await handleTemplateFile({
|
|
153
|
+
source,
|
|
154
|
+
target,
|
|
155
|
+
parameters,
|
|
156
|
+
processors: layout.processors,
|
|
157
|
+
writeMode: layout.writeMode,
|
|
158
|
+
});
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case 'folder': {
|
|
162
|
+
for (const matcher of layout.matchers || []) {
|
|
163
|
+
const matched = await glob(matcher.match.map((m) => render(m, { parameters })), { cwd: source });
|
|
164
|
+
for (const file of matched) {
|
|
165
|
+
await handleTemplateFile({
|
|
166
|
+
source: path.resolve(source, file),
|
|
167
|
+
target: path.resolve(target, file.endsWith('.ejs') ? file.slice(0, -4) : file),
|
|
168
|
+
parameters,
|
|
169
|
+
processors: matcher.processors,
|
|
170
|
+
writeMode: matcher.writeMode,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
.parseAsync(process.argv)
|
|
180
|
+
.catch((err) => {
|
|
181
|
+
if (err instanceof Error && err.name === 'ExitPromptError') {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
logger.error(err);
|
|
185
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alauda/doom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Doctor Doom making docs.",
|
|
6
6
|
"repository": {
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"swc-node": "node --enable-source-maps --import @swc-node/register/esm-register"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
+
"@inquirer/prompts": "^7.2.1",
|
|
41
42
|
"@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
|
|
42
43
|
"@rspress/core": "^1.38.0",
|
|
43
44
|
"@rspress/plugin-preview": "^1.38.0",
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
"chokidar": "^4.0.3",
|
|
46
47
|
"clsx": "^2.1.1",
|
|
47
48
|
"commander": "^12.1.0",
|
|
49
|
+
"ejs": "^3.1.10",
|
|
48
50
|
"es-toolkit": "^1.30.1",
|
|
49
51
|
"get-port": "^7.1.0",
|
|
50
52
|
"openapi-types": "^12.1.3",
|
|
@@ -52,6 +54,7 @@
|
|
|
52
54
|
"rehype-raw": "^6.1.1",
|
|
53
55
|
"remark-gfm": "^3.0.1",
|
|
54
56
|
"shiki": "^1.24.3",
|
|
57
|
+
"simple-git": "^3.27.0",
|
|
55
58
|
"swagger2openapi": "^7.0.8",
|
|
56
59
|
"tinyglobby": "^0.2.10",
|
|
57
60
|
"type-fest": "^4.30.2",
|
|
@@ -62,6 +65,7 @@
|
|
|
62
65
|
"@eslint/js": "^9.17.0",
|
|
63
66
|
"@swc-node/register": "^1.10.9",
|
|
64
67
|
"@swc/core": "1.10.1",
|
|
68
|
+
"@types/ejs": "^3",
|
|
65
69
|
"@types/eslint__js": "^8.42.3",
|
|
66
70
|
"@types/fs-extra": "^11.0.4",
|
|
67
71
|
"@types/node": "^18.19.68",
|