forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-solution.ts
More file actions
65 lines (56 loc) · 2.04 KB
/
init-solution.ts
File metadata and controls
65 lines (56 loc) · 2.04 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
/* eslint-disable no-console */
import * as fs from 'fs-extra';
import * as path from 'path';
import chalk from 'chalk';
import * as changeCase from 'change-case';
import { getErrorMessage } from '../utils/errors';
import { getLowcodeSolutionTemplateFiles } from './solutions/example-solution';
export async function initSolution(args: string[], options: {
quiet?: boolean;
verbose?: boolean;
}) {
try {
const cwd = process.cwd();
let solutionName = args[0] || 'hello';
let solutionPath = path.resolve(cwd, solutionName);
if (solutionName === '.') {
solutionName = path.basename(cwd);
solutionPath = cwd;
}
const modifyFileContent = (content: string) =>
content
.replace(/hello-world/g, changeCase.paramCase(solutionName))
.replace(/HelloWorld/g, changeCase.pascalCase(solutionName))
.replace(/Hello World/g, changeCase.titleCase(solutionName));
await ensureDirExists(solutionPath);
const templateFiles = getLowcodeSolutionTemplateFiles();
for (const templateFile of templateFiles) {
if (options.verbose) {
console.log('%s', chalk.gray(`creating file ${templateFile.file}`));
}
const templateFilePath = path.join(solutionPath, templateFile.file);
await ensureDirExists(path.dirname(templateFilePath));
await fs.writeFile(templateFilePath, modifyFileContent(templateFile.content), { encoding: 'utf-8' });
}
if (!options.quiet) {
console.log('%s', chalk.green(`solution ${solutionName} created successfully`));
}
return 0;
} catch (e) {
console.log(chalk.red(getErrorMessage(e) || `Unexpected error: ${e}`));
if (typeof e === 'object' && (e as { stack: string } | null)?.stack && options.verbose) {
console.log(chalk.gray((e as { stack: string }).stack));
}
return 1;
}
}
async function ensureDirExists(dirPath: string) {
try {
await fs.mkdir(dirPath, { recursive: true });
} catch (e) {
if ((e as { code: string }).code === 'EEXIST') {
return;// ignore existing error
}
throw e;
}
}