feat(codegen): add demo slots & add new cli option solutionOptions

This commit is contained in:
eternalsky 2022-12-14 10:00:27 +08:00
parent 1ca940c880
commit 2bd40d7589
4 changed files with 27 additions and 2 deletions

View File

@ -14,6 +14,7 @@ program
.option('-c, --cwd <cwd>', 'specify the working directory', '.')
.option('-q, --quiet', 'be quiet, do not output anything unless get error', false)
.option('-v, --verbose', 'be verbose, output more information', false)
.option('--solution-options <options>', 'specify the solution options', '{}')
.arguments('[input-schema] ali lowcode schema JSON file')
.action(function doGenerate(inputSchema, command) {
var options = command.opts();

View File

@ -25,6 +25,7 @@ export async function run(
output?: string;
quiet?: boolean;
verbose?: boolean;
solutionOptions?: string;
},
): Promise<number> {
try {
@ -41,6 +42,19 @@ export async function run(
);
}
let solutionOptions = {};
if (options.solutionOptions) {
try {
solutionOptions = JSON.parse(options.solutionOptions);
} catch (err: any) {
throw new Error(
`solution options parse error, error message is "${err.message}"`,
);
}
}
// 读取 Schema
const schema = await loadSchemaFile(schemaFile);
@ -48,7 +62,7 @@ export async function run(
const createProjectBuilder = await getProjectBuilderFactory(options.solution, {
quiet: options.quiet,
});
const builder = createProjectBuilder();
const builder = createProjectBuilder(solutionOptions);
// 生成代码
const generatedSourceCodes = await builder.generateProject(schema);
@ -75,7 +89,7 @@ export async function run(
async function getProjectBuilderFactory(
solution: string,
{ quiet }: { quiet?: boolean },
): Promise<() => IProjectBuilder> {
): Promise<(options: {[prop: string]: any}) => IProjectBuilder> {
if (solution in CodeGenerator.solutions) {
return CodeGenerator.solutions[solution as 'icejs' | 'rax'];
}

View File

@ -241,6 +241,15 @@ export class ProjectBuilder implements IProjectBuilder {
});
}
// demo
if (parseResult.project && builders.demo) {
const { files } = await builders.demo.generateModule(parseResult.project);
buildResult.push({
path: this.template.slots.demo.path,
files,
});
}
// TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下?
// Post Process

View File

@ -25,6 +25,7 @@ export enum FileType {
TS = 'ts',
TSX = 'tsx',
JSON = 'json',
MD = 'md',
}
export enum ChunkType {