(args: string[], options: {
quiet?: boolean;
verbose?: boolean;
})
| 8 | |
| 9 | |
| 10 | export async function initSolution(args: string[], options: { |
| 11 | quiet?: boolean; |
| 12 | verbose?: boolean; |
| 13 | }) { |
| 14 | try { |
| 15 | const cwd = process.cwd(); |
| 16 | let solutionName = args[0] || 'hello'; |
| 17 | let solutionPath = path.resolve(cwd, solutionName); |
| 18 | if (solutionName === '.') { |
| 19 | solutionName = path.basename(cwd); |
| 20 | solutionPath = cwd; |
| 21 | } |
| 22 | |
| 23 | const modifyFileContent = (content: string) => |
| 24 | content |
| 25 | .replace(/hello-world/g, changeCase.paramCase(solutionName)) |
| 26 | .replace(/HelloWorld/g, changeCase.pascalCase(solutionName)) |
| 27 | .replace(/Hello World/g, changeCase.titleCase(solutionName)); |
| 28 | |
| 29 | await ensureDirExists(solutionPath); |
| 30 | |
| 31 | const templateFiles = getLowcodeSolutionTemplateFiles(); |
| 32 | for (const templateFile of templateFiles) { |
| 33 | if (options.verbose) { |
| 34 | console.log('%s', chalk.gray(`creating file ${templateFile.file}`)); |
| 35 | } |
| 36 | |
| 37 | const templateFilePath = path.join(solutionPath, templateFile.file); |
| 38 | await ensureDirExists(path.dirname(templateFilePath)); |
| 39 | await fs.writeFile(templateFilePath, modifyFileContent(templateFile.content), { encoding: 'utf-8' }); |
| 40 | } |
| 41 | |
| 42 | if (!options.quiet) { |
| 43 | console.log('%s', chalk.green(`solution ${solutionName} created successfully`)); |
| 44 | } |
| 45 | |
| 46 | return 0; |
| 47 | } catch (e) { |
| 48 | console.log(chalk.red(getErrorMessage(e) || `Unexpected error: ${e}`)); |
| 49 | if (typeof e === 'object' && (e as { stack: string } | null)?.stack && options.verbose) { |
| 50 | console.log(chalk.gray((e as { stack: string }).stack)); |
| 51 | } |
| 52 | return 1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | async function ensureDirExists(dirPath: string) { |
| 57 | try { |
nothing calls this directly
no test coverage detected
searching dependent graphs…