@farris/cli 2.2.5 → 2.3.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/commands/add-app.js +6 -0
- package/lib/commands/add-app.js.map +1 -0
- package/lib/commands/add-lib.js +6 -0
- package/lib/commands/add-lib.js.map +1 -0
- package/lib/commands/add-project.js +194 -0
- package/lib/commands/add-project.js.map +1 -0
- package/lib/commands/build-components.js +57 -50
- package/lib/commands/build-components.js.map +1 -1
- package/lib/commands/build-css.js +38 -38
- package/lib/commands/build-css.js.map +1 -1
- package/lib/commands/build-lib.js +30 -25
- package/lib/commands/build-lib.js.map +1 -1
- package/lib/commands/build.js +19 -14
- package/lib/commands/build.js.map +1 -1
- package/lib/commands/create-app-options.js +72 -74
- package/lib/commands/create-app-options.js.map +1 -1
- package/lib/commands/create-app.js +131 -126
- package/lib/commands/create-app.js.map +1 -1
- package/lib/commands/create-options.js +32 -29
- package/lib/commands/create-options.js.map +1 -1
- package/lib/commands/create.js +277 -224
- package/lib/commands/create.js.map +1 -1
- package/lib/commands/dev-serve.js +18 -18
- package/lib/commands/extract-i18n.js +74 -74
- package/lib/commands/extract-i18n.js.map +1 -1
- package/lib/commands/generate/api.js +124 -124
- package/lib/commands/generate/api.js.map +1 -1
- package/lib/commands/generate/common.js +101 -100
- package/lib/commands/generate/common.js.map +1 -1
- package/lib/commands/generate/component.js +156 -156
- package/lib/commands/generate/component.js.map +1 -1
- package/lib/commands/generate/view.js +168 -168
- package/lib/commands/generate/view.js.map +1 -1
- package/lib/commands/preview-serve.js +16 -16
- package/lib/commands/workspace/add-app.js +15 -180
- package/lib/commands/workspace/add-app.js.map +1 -1
- package/lib/commands/workspace/create.js +62 -379
- package/lib/commands/workspace/create.js.map +1 -1
- package/lib/commands/workspace/inspect.js +30 -30
- package/lib/commands/workspace/inspect.js.map +1 -1
- package/lib/commands/workspace/verify.js +38 -38
- package/lib/commands/workspace/verify.js.map +1 -1
- package/lib/common/agents-doc.js +34 -0
- package/lib/common/agents-doc.js.map +1 -0
- package/lib/common/apply-catalog-deps.js +15 -15
- package/lib/common/apply-catalog-deps.js.map +1 -1
- package/lib/common/component-file-name.js +3 -3
- package/lib/common/constant.js +64 -64
- package/lib/common/constant.js.map +1 -1
- package/lib/common/create-app-target.js +46 -46
- package/lib/common/create-app-target.js.map +1 -1
- package/lib/common/generate-app.js +109 -85
- package/lib/common/generate-app.js.map +1 -1
- package/lib/common/get-dependencies.js +9 -9
- package/lib/common/get-farris-config.js +25 -25
- package/lib/common/get-farris-config.js.map +1 -1
- package/lib/common/get-version.js +6 -6
- package/lib/common/get-vite-config.js +106 -106
- package/lib/common/get-vite-config.js.map +1 -1
- package/lib/common/lib-package-exports.js +24 -24
- package/lib/common/lib-package-exports.js.map +1 -1
- package/lib/common/list-lib-components.js +23 -23
- package/lib/common/list-lib-components.js.map +1 -1
- package/lib/common/output.js +164 -94
- package/lib/common/output.js.map +1 -1
- package/lib/common/project-layout.js +143 -0
- package/lib/common/project-layout.js.map +1 -0
- package/lib/common/safety.js +173 -171
- package/lib/common/safety.js.map +1 -1
- package/lib/common/template-manifest.js +14 -16
- package/lib/common/template-manifest.js.map +1 -1
- package/lib/common/template-renderer.js +114 -114
- package/lib/common/template-renderer.js.map +1 -1
- package/lib/common/transaction.js +83 -83
- package/lib/common/transaction.js.map +1 -1
- package/lib/common/verifier.js +194 -150
- package/lib/common/verifier.js.map +1 -1
- package/lib/common/workspace-contract.js +86 -0
- package/lib/common/workspace-contract.js.map +1 -0
- package/lib/config/vite-app.js +13 -13
- package/lib/config/vite-common.js +20 -20
- package/lib/config/vite-common.js.map +1 -1
- package/lib/config/vite-component.js +32 -32
- package/lib/config/vite-component.js.map +1 -1
- package/lib/config/vite-lib.js +26 -26
- package/lib/index.js +234 -205
- package/lib/index.js.map +1 -1
- package/lib/plugins/create-app-package-json.js +24 -0
- package/lib/plugins/create-app-package-json.js.map +1 -0
- package/lib/plugins/create-component-style.js +44 -44
- package/lib/plugins/create-package-json.js +48 -48
- package/lib/plugins/create-package-json.js.map +1 -1
- package/lib/plugins/dts.js +8 -8
- package/lib/plugins/html-system.js +9 -9
- package/lib/plugins/replace.js +17 -17
- package/lib/plugins/systemjs-bundle.js +15 -15
- package/lib/plugins/systemjs-bundle.js.map +1 -1
- package/package.json +53 -52
- package/templates/create/.gitignore.tpl +12 -0
- package/templates/create/.npmrc.tpl +1 -0
- package/templates/create/AGENTS.md +62 -0
- package/templates/create/CLAUDE.md +1 -0
- package/templates/create/README.md +14 -1
- package/templates/create/package.json +11 -3
- package/templates/create/pnpm-workspace.yaml +2 -1
- package/templates/lib/.eslintrc.cjs +15 -0
- package/templates/lib/.gitignore.tpl +3 -0
- package/templates/lib/.prettierrc.json +8 -0
- package/templates/lib/index.html +1 -1
- package/templates/lib/package.json +39 -4
- package/templates/lib/{components → test/components}/button/button.spec.ts +3 -2
- package/templates/lib/test/components/button/button.spec.tsx +11 -0
- package/templates/lib/tsconfig.json +5 -1
- package/templates/lib/vitest.config.ts +1 -1
- package/templates/mobile/.gitignore.tpl +31 -0
- package/templates/mobile/.gitlab-ci.yml +3 -9
- package/templates/mobile/index.html +1 -1
- package/templates/mobile/package.json +14 -10
- package/templates/mobile/src/main.ts +5 -4
- package/templates/mobile/src/router/index.ts +17 -17
- package/templates/mobile/test/src/components/TheButton.spec.ts +15 -0
- package/templates/mobile/test/src/components/TheButton.spec.tsx +15 -0
- package/templates/mobile/tsconfig.json +3 -2
- package/templates/mobile/vitest.config.ts +2 -1
- package/templates/web/.gitignore.tpl +31 -0
- package/templates/web/.gitlab-ci.yml +3 -9
- package/templates/web/index.html +1 -1
- package/templates/web/package.json +14 -10
- package/templates/web/src/locales/index.ts +5 -0
- package/templates/web/src/locales/zh-CN.json +1 -0
- package/templates/web/src/main.ts +5 -2
- package/templates/web/src/router/index.ts +17 -17
- package/templates/web/src/styles/index.css +0 -0
- package/templates/web/src/styles/index.scss +0 -0
- package/templates/web/test/src/components/TheButton.spec.ts +15 -0
- package/templates/web/test/src/components/TheButton.spec.tsx +15 -0
- package/templates/web/tsconfig.json +3 -2
- package/templates/web/vitest.config.ts +2 -1
- package/lib/common/project-package-name.js +0 -5
- package/lib/common/project-package-name.js.map +0 -1
- package/templates/mobile/.changeset/README.md +0 -4
- package/templates/mobile/.changeset/config.json +0 -11
- package/templates/mobile/project.json +0 -4
- package/templates/mobile/src/components/TheButton.spec.ts +0 -8
- package/templates/web/.changeset/README.md +0 -4
- package/templates/web/.changeset/config.json +0 -11
- package/templates/web/project.json +0 -4
- package/templates/web/src/components/TheButton.spec.ts +0 -8
- package/templates/workspace/app/farris.config.mjs +0 -13
- package/templates/workspace/app/index.html +0 -12
- package/templates/workspace/app/package.json +0 -22
- package/templates/workspace/app/src/App.tsx +0 -11
- package/templates/workspace/app/src/main.ts +0 -12
- package/templates/workspace/app/src/router/index.ts +0 -16
- package/templates/workspace/app/src/views/home/composables/use-home.ts +0 -7
- package/templates/workspace/app/src/views/home/home.component.tsx +0 -15
- package/templates/workspace/app/src/views/home/index.ts +0 -4
- package/templates/workspace/app/tsconfig.json +0 -21
- package/templates/workspace/root/README.md +0 -28
- package/templates/workspace/root/package.json +0 -13
- package/templates/workspace/root/pnpm-workspace.yaml +0 -12
- /package/templates/{workspace/app → mobile}/src/locales/index.ts +0 -0
- /package/templates/{workspace/app → mobile}/src/locales/zh-CN.json +0 -0
- /package/templates/{workspace/app → mobile}/src/styles/index.css +0 -0
- /package/templates/{workspace/app → mobile}/src/styles/index.scss +0 -0
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import { resolve, join } from 'node:path';
|
|
3
|
-
import { findAppContext, normalizeGeneratorName, toPascalCase, toCamelCase, toReadableTitle } from './common.js';
|
|
4
|
-
import { Transaction } from '../../common/transaction.js';
|
|
5
|
-
import { sendJson, logInfo, logWarn, fail, setJsonMode, createSpinner } from '../../common/output.js';
|
|
6
|
-
import { ExitCode } from '../../common/constant.js';
|
|
7
|
-
export async function generateView(rawName, options = {}) {
|
|
8
|
-
const isJson = !!options.json;
|
|
9
|
-
setJsonMode(isJson);
|
|
10
|
-
if (!rawName) {
|
|
11
|
-
fail({
|
|
12
|
-
command: 'generate.view',
|
|
13
|
-
error: '必须指定 View 页面名称: farris-cli generate view <name>',
|
|
14
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
const normalizedName = normalizeGeneratorName(rawName);
|
|
18
|
-
if (!normalizedName.valid) {
|
|
19
|
-
fail({
|
|
20
|
-
command: 'generate.view',
|
|
21
|
-
error: `非法的 View 名称: ${normalizedName.error}`,
|
|
22
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
const name = normalizedName.name;
|
|
26
|
-
const pascalName = toPascalCase(name);
|
|
27
|
-
const camelName = toCamelCase(name);
|
|
28
|
-
const title = options.title || toReadableTitle(name);
|
|
29
|
-
const withRoute = !!options.withRoute;
|
|
30
|
-
const withProps = !!options.withProps;
|
|
31
|
-
const appContext = findAppContext(options.cwd || process.cwd());
|
|
32
|
-
const viewDir = join(appContext.srcDir, 'views', name);
|
|
33
|
-
const composablesDir = join(viewDir, 'composables');
|
|
34
|
-
const filesPlanned = [
|
|
35
|
-
join(viewDir, 'index.ts'),
|
|
36
|
-
join(viewDir, `${name}.component.tsx`),
|
|
37
|
-
join(composablesDir, `use-${name}.ts`)
|
|
38
|
-
];
|
|
39
|
-
if (withProps) {
|
|
40
|
-
filesPlanned.push(join(viewDir, `${name}.props.ts`));
|
|
41
|
-
}
|
|
42
|
-
// 冲突检查
|
|
43
|
-
const conflicts = filesPlanned.filter((f) => existsSync(f));
|
|
44
|
-
if (conflicts.length > 0) {
|
|
45
|
-
fail({
|
|
46
|
-
command: 'generate.view',
|
|
47
|
-
error: `页面文件已存在,为保护已有代码已阻断生成: ${conflicts.map((c) => resolve(c)).join(', ')}`,
|
|
48
|
-
exitCode: ExitCode.TARGET_CONFLICT,
|
|
49
|
-
extra: { conflicts }
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
const transaction = new Transaction();
|
|
53
|
-
const spinner = createSpinner(`正在生成 View: ${name}...`);
|
|
54
|
-
spinner.start();
|
|
55
|
-
try {
|
|
56
|
-
mkdirSync(composablesDir, { recursive: true });
|
|
57
|
-
transaction.recordCreatedDir(viewDir);
|
|
58
|
-
transaction.recordCreatedDir(composablesDir);
|
|
59
|
-
const filesCreated = [];
|
|
60
|
-
const filesModified = [];
|
|
61
|
-
// 1. 生成 composable: src/views/<name>/composables/use-<name>.ts
|
|
62
|
-
const composablePath = join(composablesDir, `use-${name}.ts`);
|
|
63
|
-
transaction.prepareWrite(composablePath);
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { resolve, join } from 'node:path';
|
|
3
|
+
import { findAppContext, normalizeGeneratorName, toPascalCase, toCamelCase, toReadableTitle } from './common.js';
|
|
4
|
+
import { Transaction } from '../../common/transaction.js';
|
|
5
|
+
import { sendJson, logInfo, logWarn, fail, setJsonMode, createSpinner } from '../../common/output.js';
|
|
6
|
+
import { ExitCode } from '../../common/constant.js';
|
|
7
|
+
export async function generateView(rawName, options = {}) {
|
|
8
|
+
const isJson = !!options.json;
|
|
9
|
+
setJsonMode(isJson);
|
|
10
|
+
if (!rawName) {
|
|
11
|
+
fail({
|
|
12
|
+
command: 'generate.view',
|
|
13
|
+
error: '必须指定 View 页面名称: farris-cli generate view <name>',
|
|
14
|
+
exitCode: ExitCode.GENERAL_ERROR
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const normalizedName = normalizeGeneratorName(rawName);
|
|
18
|
+
if (!normalizedName.valid) {
|
|
19
|
+
fail({
|
|
20
|
+
command: 'generate.view',
|
|
21
|
+
error: `非法的 View 名称: ${normalizedName.error}`,
|
|
22
|
+
exitCode: ExitCode.GENERAL_ERROR
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const name = normalizedName.name;
|
|
26
|
+
const pascalName = toPascalCase(name);
|
|
27
|
+
const camelName = toCamelCase(name);
|
|
28
|
+
const title = options.title || toReadableTitle(name);
|
|
29
|
+
const withRoute = !!options.withRoute;
|
|
30
|
+
const withProps = !!options.withProps;
|
|
31
|
+
const appContext = findAppContext(options.cwd || process.cwd());
|
|
32
|
+
const viewDir = join(appContext.srcDir, 'views', name);
|
|
33
|
+
const composablesDir = join(viewDir, 'composables');
|
|
34
|
+
const filesPlanned = [
|
|
35
|
+
join(viewDir, 'index.ts'),
|
|
36
|
+
join(viewDir, `${name}.component.tsx`),
|
|
37
|
+
join(composablesDir, `use-${name}.ts`)
|
|
38
|
+
];
|
|
39
|
+
if (withProps) {
|
|
40
|
+
filesPlanned.push(join(viewDir, `${name}.props.ts`));
|
|
41
|
+
}
|
|
42
|
+
// 冲突检查
|
|
43
|
+
const conflicts = filesPlanned.filter((f) => existsSync(f));
|
|
44
|
+
if (conflicts.length > 0) {
|
|
45
|
+
fail({
|
|
46
|
+
command: 'generate.view',
|
|
47
|
+
error: `页面文件已存在,为保护已有代码已阻断生成: ${conflicts.map((c) => resolve(c)).join(', ')}`,
|
|
48
|
+
exitCode: ExitCode.TARGET_CONFLICT,
|
|
49
|
+
extra: { conflicts }
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const transaction = new Transaction();
|
|
53
|
+
const spinner = createSpinner(`正在生成 View: ${name}...`);
|
|
54
|
+
spinner.start();
|
|
55
|
+
try {
|
|
56
|
+
mkdirSync(composablesDir, { recursive: true });
|
|
57
|
+
transaction.recordCreatedDir(viewDir);
|
|
58
|
+
transaction.recordCreatedDir(composablesDir);
|
|
59
|
+
const filesCreated = [];
|
|
60
|
+
const filesModified = [];
|
|
61
|
+
// 1. 生成 composable: src/views/<name>/composables/use-<name>.ts
|
|
62
|
+
const composablePath = join(composablesDir, `use-${name}.ts`);
|
|
63
|
+
transaction.prepareWrite(composablePath);
|
|
64
64
|
const composableContent = `export function use${pascalName}() {
|
|
65
65
|
const title = ${JSON.stringify(title)};
|
|
66
66
|
|
|
@@ -68,13 +68,13 @@ export async function generateView(rawName, options = {}) {
|
|
|
68
68
|
title
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
|
-
`;
|
|
72
|
-
writeFileSync(composablePath, composableContent, 'utf-8');
|
|
73
|
-
filesCreated.push(composablePath);
|
|
74
|
-
// 2. 生成 props (若启用): src/views/<name>/<name>.props.ts
|
|
75
|
-
if (withProps) {
|
|
76
|
-
const propsPath = join(viewDir, `${name}.props.ts`);
|
|
77
|
-
transaction.prepareWrite(propsPath);
|
|
71
|
+
`;
|
|
72
|
+
writeFileSync(composablePath, composableContent, 'utf-8');
|
|
73
|
+
filesCreated.push(composablePath);
|
|
74
|
+
// 2. 生成 props (若启用): src/views/<name>/<name>.props.ts
|
|
75
|
+
if (withProps) {
|
|
76
|
+
const propsPath = join(viewDir, `${name}.props.ts`);
|
|
77
|
+
transaction.prepareWrite(propsPath);
|
|
78
78
|
const propsContent = `import type { ExtractPropTypes } from 'vue';
|
|
79
79
|
|
|
80
80
|
export const ${camelName}ViewProps = {
|
|
@@ -82,13 +82,13 @@ export const ${camelName}ViewProps = {
|
|
|
82
82
|
};
|
|
83
83
|
|
|
84
84
|
export type ${pascalName}ViewProps = ExtractPropTypes<typeof ${camelName}ViewProps>;
|
|
85
|
-
`;
|
|
86
|
-
writeFileSync(propsPath, propsContent, 'utf-8');
|
|
87
|
-
filesCreated.push(propsPath);
|
|
88
|
-
}
|
|
89
|
-
// 3. 生成 component: src/views/<name>/<name>.component.tsx
|
|
90
|
-
const componentPath = join(viewDir, `${name}.component.tsx`);
|
|
91
|
-
transaction.prepareWrite(componentPath);
|
|
85
|
+
`;
|
|
86
|
+
writeFileSync(propsPath, propsContent, 'utf-8');
|
|
87
|
+
filesCreated.push(propsPath);
|
|
88
|
+
}
|
|
89
|
+
// 3. 生成 component: src/views/<name>/<name>.component.tsx
|
|
90
|
+
const componentPath = join(viewDir, `${name}.component.tsx`);
|
|
91
|
+
transaction.prepareWrite(componentPath);
|
|
92
92
|
const componentContent = `import { defineComponent } from 'vue';
|
|
93
93
|
import { use${pascalName} } from './composables/use-${name}';
|
|
94
94
|
|
|
@@ -104,95 +104,95 @@ export default defineComponent({
|
|
|
104
104
|
);
|
|
105
105
|
}
|
|
106
106
|
});
|
|
107
|
-
`;
|
|
108
|
-
writeFileSync(componentPath, componentContent, 'utf-8');
|
|
109
|
-
filesCreated.push(componentPath);
|
|
110
|
-
// 4. 生成 index: src/views/<name>/index.ts
|
|
111
|
-
const indexPath = join(viewDir, 'index.ts');
|
|
112
|
-
transaction.prepareWrite(indexPath);
|
|
113
|
-
let indexContent = `import ${pascalName}View from './${name}.component';\n\n`;
|
|
114
|
-
if (withProps) {
|
|
115
|
-
indexContent += `export { ${camelName}ViewProps, type ${pascalName}ViewProps } from './${name}.props';\n`;
|
|
116
|
-
}
|
|
117
|
-
indexContent += `export { ${pascalName}View };\nexport default ${pascalName}View;\n`;
|
|
118
|
-
writeFileSync(indexPath, indexContent, 'utf-8');
|
|
119
|
-
filesCreated.push(indexPath);
|
|
120
|
-
// 5. 若指定 --with-route,尝试在路由中幂等注册
|
|
121
|
-
let routeRegistered = false;
|
|
122
|
-
let routeNotice = '';
|
|
123
|
-
if (withRoute) {
|
|
124
|
-
const routerIndexPath = join(appContext.srcDir, 'router', 'index.ts');
|
|
125
|
-
const routesFilePath = join(appContext.srcDir, 'router', 'routes.ts');
|
|
126
|
-
const targetRouterFile = existsSync(routerIndexPath)
|
|
127
|
-
? routerIndexPath
|
|
128
|
-
: existsSync(routesFilePath)
|
|
129
|
-
? routesFilePath
|
|
130
|
-
: null;
|
|
131
|
-
if (targetRouterFile) {
|
|
132
|
-
transaction.prepareWrite(targetRouterFile);
|
|
133
|
-
let routerContent = readFileSync(targetRouterFile, 'utf-8');
|
|
134
|
-
// 检查是否已存在对应路由 name 或 path
|
|
135
|
-
const routeRegex = new RegExp(`name:\\s*['"]${pascalName}['"]|path:\\s*['"]/${name}['"]`);
|
|
136
|
-
if (!routeRegex.test(routerContent)) {
|
|
137
|
-
// 查找 routes 数组位置并插入
|
|
138
|
-
const routesArrayMatch = routerContent.match(/(routes\s*(?::\s*RouteRecordRaw\[\])?\s*=\s*\[)([\s\S]*?)(\];)/);
|
|
139
|
-
if (routesArrayMatch) {
|
|
140
|
-
const existingEntries = routesArrayMatch[2].trimEnd();
|
|
141
|
-
const needsComma = existingEntries.trim().length > 0 && !existingEntries.endsWith(',');
|
|
142
|
-
const separator = needsComma ? ',' : '';
|
|
143
|
-
const newRouteEntry = `\n {\n path: '/${name}',\n name: '${pascalName}',\n component: () => import('../views/${name}')\n },`;
|
|
144
|
-
routerContent = routerContent.replace(routesArrayMatch[0], `${routesArrayMatch[1]}${existingEntries}${separator}${newRouteEntry}\n${routesArrayMatch[3]}`);
|
|
145
|
-
writeFileSync(targetRouterFile, routerContent, 'utf-8');
|
|
146
|
-
filesModified.push(targetRouterFile);
|
|
147
|
-
routeRegistered = true;
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
routeNotice = `未在 ${targetRouterFile} 中识别出标准 routes 数组结构,请手动注册路由`;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
routeRegistered = true; // 已经存在,幂等跳过
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
routeNotice = '未找到标准 src/router 路由文件,请手动注册路由配置';
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
transaction.commit();
|
|
162
|
-
spinner.succeed(`View ${name} 生成成功!`);
|
|
163
|
-
if (isJson) {
|
|
164
|
-
sendJson({
|
|
165
|
-
command: 'generate.view',
|
|
166
|
-
ok: true,
|
|
167
|
-
viewName: name,
|
|
168
|
-
pascalName,
|
|
169
|
-
title,
|
|
170
|
-
viewDir,
|
|
171
|
-
withRoute,
|
|
172
|
-
routeRegistered,
|
|
173
|
-
routeNotice: routeNotice || undefined,
|
|
174
|
-
filesCreated,
|
|
175
|
-
filesModified
|
|
176
|
-
});
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
logInfo(`\n✨ 已成功生成页面: src/views/${name}/`);
|
|
180
|
-
filesCreated.forEach((f) => logInfo(` + ${f}`));
|
|
181
|
-
if (filesModified.length > 0) {
|
|
182
|
-
filesModified.forEach((f) => logInfo(` ~ ${f} (已自动注册路由)`));
|
|
183
|
-
}
|
|
184
|
-
if (routeNotice) {
|
|
185
|
-
logWarn(` ! 路由提示: ${routeNotice}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
catch (err) {
|
|
189
|
-
spinner.fail('生成 View 失败,正在回滚...');
|
|
190
|
-
transaction.rollback();
|
|
191
|
-
fail({
|
|
192
|
-
command: 'generate.view',
|
|
193
|
-
error: `生成 View 失败: ${err.message}`,
|
|
194
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
107
|
+
`;
|
|
108
|
+
writeFileSync(componentPath, componentContent, 'utf-8');
|
|
109
|
+
filesCreated.push(componentPath);
|
|
110
|
+
// 4. 生成 index: src/views/<name>/index.ts
|
|
111
|
+
const indexPath = join(viewDir, 'index.ts');
|
|
112
|
+
transaction.prepareWrite(indexPath);
|
|
113
|
+
let indexContent = `import ${pascalName}View from './${name}.component';\n\n`;
|
|
114
|
+
if (withProps) {
|
|
115
|
+
indexContent += `export { ${camelName}ViewProps, type ${pascalName}ViewProps } from './${name}.props';\n`;
|
|
116
|
+
}
|
|
117
|
+
indexContent += `export { ${pascalName}View };\nexport default ${pascalName}View;\n`;
|
|
118
|
+
writeFileSync(indexPath, indexContent, 'utf-8');
|
|
119
|
+
filesCreated.push(indexPath);
|
|
120
|
+
// 5. 若指定 --with-route,尝试在路由中幂等注册
|
|
121
|
+
let routeRegistered = false;
|
|
122
|
+
let routeNotice = '';
|
|
123
|
+
if (withRoute) {
|
|
124
|
+
const routerIndexPath = join(appContext.srcDir, 'router', 'index.ts');
|
|
125
|
+
const routesFilePath = join(appContext.srcDir, 'router', 'routes.ts');
|
|
126
|
+
const targetRouterFile = existsSync(routerIndexPath)
|
|
127
|
+
? routerIndexPath
|
|
128
|
+
: existsSync(routesFilePath)
|
|
129
|
+
? routesFilePath
|
|
130
|
+
: null;
|
|
131
|
+
if (targetRouterFile) {
|
|
132
|
+
transaction.prepareWrite(targetRouterFile);
|
|
133
|
+
let routerContent = readFileSync(targetRouterFile, 'utf-8');
|
|
134
|
+
// 检查是否已存在对应路由 name 或 path
|
|
135
|
+
const routeRegex = new RegExp(`name:\\s*['"]${pascalName}['"]|path:\\s*['"]/${name}['"]`);
|
|
136
|
+
if (!routeRegex.test(routerContent)) {
|
|
137
|
+
// 查找 routes 数组位置并插入
|
|
138
|
+
const routesArrayMatch = routerContent.match(/(routes\s*(?::\s*RouteRecordRaw\[\])?\s*=\s*\[)([\s\S]*?)(\];)/);
|
|
139
|
+
if (routesArrayMatch) {
|
|
140
|
+
const existingEntries = routesArrayMatch[2].trimEnd();
|
|
141
|
+
const needsComma = existingEntries.trim().length > 0 && !existingEntries.endsWith(',');
|
|
142
|
+
const separator = needsComma ? ',' : '';
|
|
143
|
+
const newRouteEntry = `\n {\n path: '/${name}',\n name: '${pascalName}',\n component: () => import('../views/${name}')\n },`;
|
|
144
|
+
routerContent = routerContent.replace(routesArrayMatch[0], `${routesArrayMatch[1]}${existingEntries}${separator}${newRouteEntry}\n${routesArrayMatch[3]}`);
|
|
145
|
+
writeFileSync(targetRouterFile, routerContent, 'utf-8');
|
|
146
|
+
filesModified.push(targetRouterFile);
|
|
147
|
+
routeRegistered = true;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
routeNotice = `未在 ${targetRouterFile} 中识别出标准 routes 数组结构,请手动注册路由`;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
routeRegistered = true; // 已经存在,幂等跳过
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
routeNotice = '未找到标准 src/router 路由文件,请手动注册路由配置';
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
transaction.commit();
|
|
162
|
+
spinner.succeed(`View ${name} 生成成功!`);
|
|
163
|
+
if (isJson) {
|
|
164
|
+
sendJson({
|
|
165
|
+
command: 'generate.view',
|
|
166
|
+
ok: true,
|
|
167
|
+
viewName: name,
|
|
168
|
+
pascalName,
|
|
169
|
+
title,
|
|
170
|
+
viewDir,
|
|
171
|
+
withRoute,
|
|
172
|
+
routeRegistered,
|
|
173
|
+
routeNotice: routeNotice || undefined,
|
|
174
|
+
filesCreated,
|
|
175
|
+
filesModified
|
|
176
|
+
});
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
logInfo(`\n✨ 已成功生成页面: src/views/${name}/`);
|
|
180
|
+
filesCreated.forEach((f) => logInfo(` + ${f}`));
|
|
181
|
+
if (filesModified.length > 0) {
|
|
182
|
+
filesModified.forEach((f) => logInfo(` ~ ${f} (已自动注册路由)`));
|
|
183
|
+
}
|
|
184
|
+
if (routeNotice) {
|
|
185
|
+
logWarn(` ! 路由提示: ${routeNotice}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
spinner.fail('生成 View 失败,正在回滚...');
|
|
190
|
+
transaction.rollback();
|
|
191
|
+
fail({
|
|
192
|
+
command: 'generate.view',
|
|
193
|
+
error: `生成 View 失败: ${err.message}`,
|
|
194
|
+
exitCode: ExitCode.GENERAL_ERROR
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
198
|
//# sourceMappingURL=view.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/commands/generate/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAA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AAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;SAC7D;QACD,IAAI,WAAW,EAAE;YACf,OAAO,CAAC,aAAa,WAAW,EAAE,CAAC,CAAC;SACrC;KACF;IAAC,OAAO,GAAQ,EAAE;QACjB,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACnC,WAAW,CAAC,QAAQ,EAAE,CAAC;QACvB,IAAI,CAAC;YACH,OAAO,EAAE,eAAe;YACxB,KAAK,EAAE,eAAe,GAAG,CAAC,OAAO,EAAE;YACnC,QAAQ,EAAE,QAAQ,CAAC,aAAa;SACjC,CAAC,CAAC;KACJ;AACH,CAAC"}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { preview, mergeConfig } from 'vite';
|
|
2
|
-
import { getViteConfig } from '../common/get-vite-config.js';
|
|
3
|
-
export const previewServe = async (options) => {
|
|
4
|
-
const { port = 4137, host = true } = options;
|
|
5
|
-
const config = await getViteConfig();
|
|
6
|
-
const previewConfig = mergeConfig({
|
|
7
|
-
configFile: false,
|
|
8
|
-
preview: {
|
|
9
|
-
port,
|
|
10
|
-
host,
|
|
11
|
-
open: true
|
|
12
|
-
}
|
|
13
|
-
}, config);
|
|
14
|
-
const server = await preview(previewConfig);
|
|
15
|
-
server.printUrls();
|
|
16
|
-
};
|
|
1
|
+
import { preview, mergeConfig } from 'vite';
|
|
2
|
+
import { getViteConfig } from '../common/get-vite-config.js';
|
|
3
|
+
export const previewServe = async (options) => {
|
|
4
|
+
const { port = 4137, host = true } = options;
|
|
5
|
+
const config = await getViteConfig();
|
|
6
|
+
const previewConfig = mergeConfig({
|
|
7
|
+
configFile: false,
|
|
8
|
+
preview: {
|
|
9
|
+
port,
|
|
10
|
+
host,
|
|
11
|
+
open: true
|
|
12
|
+
}
|
|
13
|
+
}, config);
|
|
14
|
+
const server = await preview(previewConfig);
|
|
15
|
+
server.printUrls();
|
|
16
|
+
};
|
|
17
17
|
//# sourceMappingURL=preview-serve.js.map
|
|
@@ -1,181 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return current;
|
|
17
|
-
}
|
|
18
|
-
const parent = dirname(current);
|
|
19
|
-
if (parent === current) {
|
|
20
|
-
break;
|
|
21
|
-
}
|
|
22
|
-
current = parent;
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
export async function addAppToWorkspace(name, options = {}) {
|
|
27
|
-
const isJson = !!options.json;
|
|
28
|
-
setJsonMode(isJson);
|
|
29
|
-
if (!name) {
|
|
30
|
-
fail({
|
|
31
|
-
command: 'workspace.add-app',
|
|
32
|
-
error: '必须指定应用名称: farris-cli workspace add-app <name>',
|
|
33
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
if (!validatePackageName(name)) {
|
|
37
|
-
fail({
|
|
38
|
-
command: 'workspace.add-app',
|
|
39
|
-
error: `非法的应用包名: "${name}"`,
|
|
40
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
const wsRoot = findWorkspaceRoot(options.cwd || process.cwd());
|
|
44
|
-
if (!wsRoot) {
|
|
45
|
-
fail({
|
|
46
|
-
command: 'workspace.add-app',
|
|
47
|
-
error: '当前目录或上级目录未找到标准 pnpm workspace (缺少 pnpm-workspace.yaml)',
|
|
48
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
const appDir = deriveAppDir(name);
|
|
52
|
-
if (!appDir || !/^[a-z0-9-_]+$/.test(appDir)) {
|
|
53
|
-
fail({
|
|
54
|
-
command: 'workspace.add-app',
|
|
55
|
-
error: `无法从应用名 "${name}" 推导出合法的子目录名`,
|
|
56
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
const title = options.title || name;
|
|
60
|
-
const router = options.router ?? false;
|
|
61
|
-
const routerMode = options.routerMode || 'hash';
|
|
62
|
-
const format = options.format || 'es';
|
|
63
|
-
const style = options.style || 'css';
|
|
64
|
-
const appTargetDir = join(wsRoot, 'packages', appDir);
|
|
65
|
-
if (existsSync(appTargetDir)) {
|
|
66
|
-
fail({
|
|
67
|
-
command: 'workspace.add-app',
|
|
68
|
-
error: `子应用目录已存在: packages/${appDir}`,
|
|
69
|
-
exitCode: ExitCode.TARGET_CONFLICT
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
const templatesBase = resolve(getDirname(import.meta.url), '../../../templates/workspace');
|
|
73
|
-
const appTemplateDir = join(templatesBase, 'app');
|
|
74
|
-
const transaction = new Transaction();
|
|
75
|
-
const spinner = createSpinner(`正在向 Workspace 添加子应用 ${name}...`);
|
|
76
|
-
spinner.start();
|
|
77
|
-
try {
|
|
78
|
-
mkdirSync(appTargetDir, { recursive: true });
|
|
79
|
-
transaction.recordCreatedDir(appTargetDir);
|
|
80
|
-
const routerDependency = router ? ',\n "vue-router": "catalog:"' : '';
|
|
81
|
-
const sassDevDependency = style === 'scss' ? ',\n "sass": "catalog:"' : '';
|
|
82
|
-
let styleImport = '';
|
|
83
|
-
if (style === 'css') {
|
|
84
|
-
styleImport = "\nimport './styles/index.css';";
|
|
85
|
-
}
|
|
86
|
-
else if (style === 'scss') {
|
|
87
|
-
styleImport = "\nimport './styles/index.scss';";
|
|
88
|
-
}
|
|
89
|
-
const routerImport = router ? "\nimport router from './router';" : '';
|
|
90
|
-
const routerUse = router ? '\napp.use(router);' : '';
|
|
91
|
-
const i18nImport = '';
|
|
92
|
-
const i18nUse = '';
|
|
93
|
-
const appRouterImport = router ? "\nimport { RouterView } from 'vue-router';" : '';
|
|
94
|
-
const appTemplateBody = router
|
|
95
|
-
? ' <RouterView />'
|
|
96
|
-
: ` <h1>${title}</h1>`;
|
|
97
|
-
const appVariables = {
|
|
98
|
-
appName: name,
|
|
99
|
-
title,
|
|
100
|
-
appDir,
|
|
101
|
-
base: '/',
|
|
102
|
-
routerMode: router ? routerMode : 'none',
|
|
103
|
-
format,
|
|
104
|
-
routerDependency,
|
|
105
|
-
sassDevDependency,
|
|
106
|
-
styleImport,
|
|
107
|
-
routerImport,
|
|
108
|
-
routerUse,
|
|
109
|
-
i18nImport,
|
|
110
|
-
i18nUse,
|
|
111
|
-
appRouterImport,
|
|
112
|
-
appTemplateBody,
|
|
113
|
-
routerHistoryFunction: routerMode === 'history' ? 'createWebHistory' : 'createWebHashHistory',
|
|
114
|
-
routerHistoryCall: routerMode === 'history'
|
|
115
|
-
? 'createWebHistory(import.meta.env.BASE_URL)'
|
|
116
|
-
: 'createWebHashHistory()'
|
|
117
|
-
};
|
|
118
|
-
const appFilter = (relPath) => {
|
|
119
|
-
const normalized = relPath.replace(/\\/g, '/');
|
|
120
|
-
if (style === 'none' && normalized.startsWith('src/styles')) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
if (style === 'css' && (normalized === 'src/styles/index.scss' || normalized === 'src/styles/index.scss.tpl')) {
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
if (style === 'scss' && (normalized === 'src/styles/index.css' || normalized === 'src/styles/index.css.tpl')) {
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
if (!router && (normalized.startsWith('src/router') || normalized.startsWith('src/views'))) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
return true;
|
|
133
|
-
};
|
|
134
|
-
const appRender = renderTemplateDir({
|
|
135
|
-
sourceDir: appTemplateDir,
|
|
136
|
-
targetDir: appTargetDir,
|
|
137
|
-
variables: appVariables,
|
|
138
|
-
skipExisting: false,
|
|
139
|
-
filter: appFilter
|
|
140
|
-
});
|
|
141
|
-
if (appRender.unreplacedPlaceholders.length > 0) {
|
|
142
|
-
throw new Error(`子应用渲染出现未替换占位符: ${appRender.unreplacedPlaceholders.map((p) => `${p.file} (${p.placeholder})`).join(', ')}`);
|
|
143
|
-
}
|
|
144
|
-
appRender.filesCreated.forEach((f) => transaction.prepareWrite(f));
|
|
145
|
-
// 执行 verify
|
|
146
|
-
const verifyResult = verifyWorkspace(wsRoot);
|
|
147
|
-
if (!verifyResult.ok) {
|
|
148
|
-
throw new Error(`Workspace 校验失败: ${verifyResult.errors.join('; ')}`);
|
|
149
|
-
}
|
|
150
|
-
transaction.commit();
|
|
151
|
-
spinner.succeed(`子应用 ${name} 添加成功!`);
|
|
152
|
-
if (isJson) {
|
|
153
|
-
sendJson({
|
|
154
|
-
command: 'workspace.add-app',
|
|
155
|
-
ok: true,
|
|
156
|
-
workspaceRoot: wsRoot,
|
|
157
|
-
appName: name,
|
|
158
|
-
appDir,
|
|
159
|
-
title,
|
|
160
|
-
router,
|
|
161
|
-
format,
|
|
162
|
-
style,
|
|
163
|
-
filesCreated: appRender.filesCreated
|
|
164
|
-
});
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
logInfo(`\n✨ 已在 packages/${appDir} 成功创建应用 ${name}`);
|
|
168
|
-
logInfo('\n启动该应用:');
|
|
169
|
-
logInfo(` pnpm --filter "${name}" dev\n`);
|
|
170
|
-
}
|
|
171
|
-
catch (err) {
|
|
172
|
-
spinner.fail('添加子应用失败,正在回滚...');
|
|
173
|
-
transaction.rollback();
|
|
174
|
-
fail({
|
|
175
|
-
command: 'workspace.add-app',
|
|
176
|
-
error: `添加应用失败: ${err.message}`,
|
|
177
|
-
exitCode: ExitCode.GENERAL_ERROR
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
1
|
+
import { addProject } from '../add-project.js';
|
|
2
|
+
import { findWorkspaceRoot } from '../../common/project-layout.js';
|
|
3
|
+
import { logWarn } from '../../common/output.js';
|
|
4
|
+
export { findWorkspaceRoot };
|
|
5
|
+
/**
|
|
6
|
+
* [已废弃] 请改用 `farris-cli add-app`。保留本命令只为兼容既有脚本。
|
|
7
|
+
*/
|
|
8
|
+
export async function addAppToWorkspace(name, options = {}) {
|
|
9
|
+
logWarn('`farris-cli workspace add-app` 已废弃,请改用 `farris-cli add-app`。');
|
|
10
|
+
const { router, routerMode, ...rest } = options;
|
|
11
|
+
if (router !== undefined || routerMode !== undefined) {
|
|
12
|
+
logWarn('--router / --router-mode 已废弃:新 app 模板始终包含路由,参数被忽略。');
|
|
13
|
+
}
|
|
14
|
+
await addProject('app', name, rest);
|
|
15
|
+
}
|
|
181
16
|
//# sourceMappingURL=add-app.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"add-app.js","sourceRoot":"","sources":["../../../src/commands/workspace/add-app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"add-app.js","sourceRoot":"","sources":["../../../src/commands/workspace/add-app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA0B,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEjD,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAQ7B;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,IAAY,EAAE,UAAkC,EAAE;IACtF,OAAO,CAAC,8DAA8D,CAAC,CAAC;IAExE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IAChD,IAAI,MAAM,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS,EAAE;QAClD,OAAO,CAAC,oDAAoD,CAAC,CAAC;KACjE;IAED,MAAM,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACxC,CAAC"}
|