diff --git a/src/cli/builders/buildOptionBuilder.ts b/src/cli/builders/buildOptionBuilder.ts index ad859d6..b405ed1 100644 --- a/src/cli/builders/buildOptionBuilder.ts +++ b/src/cli/builders/buildOptionBuilder.ts @@ -3,7 +3,6 @@ import type { Argv } from 'yargs'; export function buildOptionBuilder(args: Argv) { // option args - .option('route-base-path', { describe: 'define the route base path. The route base path is used as the base path for navbar anchor when generating HTML documents', diff --git a/src/cli/commands/buildDocumentCommandHandler.ts b/src/cli/commands/buildDocumentCommandHandler.ts index 75a6e23..9a971c2 100644 --- a/src/cli/commands/buildDocumentCommandHandler.ts +++ b/src/cli/commands/buildDocumentCommandHandler.ts @@ -41,8 +41,6 @@ import fs from 'node:fs'; import type { DataSource } from 'typeorm'; export async function buildDocumentCommandHandler(option: IBuildCommandOption) { - let localDataSource: DataSource | undefined; - try { if (option.showLogo != null) { await showLogo({ @@ -58,7 +56,7 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) { const dataSource = await getDataSource(option); const [templates] = await Promise.all([await loadTemplates(option), await dataSource.initialize()]); - const renderer = new TemplateRenderer(templates.default, templates.template); + const renderer = new TemplateRenderer(templates.template, templates.default); if (isFalse(dataSource.isInitialized)) { throw new Error(`Cannot initialize in ${fastSafeStringify(dataSource.options, undefined, 2)}`); @@ -168,8 +166,9 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) { return []; } finally { - if (localDataSource != null) { - await localDataSource.destroy(); + const dataSource = container.resolve(SymbolDataSource); + if (dataSource != null) { + await dataSource.destroy(); } } } diff --git a/src/cli/commands/cleanDocumentCommandHandler.ts b/src/cli/commands/cleanDocumentCommandHandler.ts index 23de2e8..897288c 100644 --- a/src/cli/commands/cleanDocumentCommandHandler.ts +++ b/src/cli/commands/cleanDocumentCommandHandler.ts @@ -39,14 +39,14 @@ export async function cleanDocumentCommandHandler(option: ICommonOption) { container.register(SymbolDataSource, asValue(dataSource)); const metadata = await getMetadata({ ...option, versionFrom: 'package.json', projectName: 'app' }); - const outputDir = await getOutputDirectory(option, getCwd(process.env)); + const outputDirPath = await getOutputDirectory(option, getCwd(process.env)); const filenames = [ - pathe.join(outputDir, CE_DEFAULT_VALUE.HTML_MERMAID_FILENAME), - pathe.join(outputDir, CE_DEFAULT_VALUE.HTML_INDEX_FILENAME), - pathe.join(outputDir, `${metadata.name}.md`), - pathe.join(outputDir, `${metadata.name}.png`), - pathe.join(outputDir, `${metadata.name}.svg`), + pathe.join(outputDirPath, CE_DEFAULT_VALUE.HTML_MERMAID_FILENAME), + pathe.join(outputDirPath, CE_DEFAULT_VALUE.HTML_INDEX_FILENAME), + pathe.join(outputDirPath, `${metadata.name}.md`), + pathe.join(outputDirPath, `${metadata.name}.png`), + pathe.join(outputDirPath, `${metadata.name}.svg`), ]; await del(filenames); diff --git a/src/cli/commands/initConfigCommandHandler.ts b/src/cli/commands/initConfigCommandHandler.ts index 75092ee..a456538 100644 --- a/src/cli/commands/initConfigCommandHandler.ts +++ b/src/cli/commands/initConfigCommandHandler.ts @@ -1,10 +1,20 @@ import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE'; import { getConfigContent } from '#/configs/modules/getConfigContent'; import { applyPrettier } from '#/creators/applyPretter'; +import { container } from '#/modules/containers/container'; +import { SymbolTemplateRenderer } from '#/modules/containers/keys/SymbolTemplateRenderer'; +import { TemplateRenderer } from '#/templates/TemplateRenderer'; +import { loadTemplates } from '#/templates/modules/loadTemplates'; +import { asValue } from 'awilix'; import consola from 'consola'; import fs from 'fs'; export async function initConfigCommandHandler() { + const templates = await loadTemplates(); + const renderer = new TemplateRenderer(templates.template, templates.default); + + container.register(SymbolTemplateRenderer, asValue(renderer)); + const rawConfig = await getConfigContent(); const prettiered = await applyPrettier(rawConfig, 'json'); diff --git a/src/cli/commands/templateEjectCommandHandler.ts b/src/cli/commands/templateEjectCommandHandler.ts index d32493e..bc94715 100644 --- a/src/cli/commands/templateEjectCommandHandler.ts +++ b/src/cli/commands/templateEjectCommandHandler.ts @@ -10,7 +10,7 @@ import { showLogo } from '@maeum/cli-logo'; import consola from 'consola'; import fs from 'fs'; import { Glob } from 'glob'; -import { getDirname } from 'my-node-fp'; +import { getDirname, startSepRemove } from 'my-node-fp'; import pathe from 'pathe'; export async function templateEjectCommandHandler(option: Pick) { @@ -24,15 +24,16 @@ export async function templateEjectCommandHandler(option: Pick { const subDirPath = await getDirname(originTemplateFilePath); - const subFilePath = originTemplateFilePath.replace(subDirPath, ''); - const targetTemplateSubDirPath = pathe.join(targetTemplateDirPath, subDirPath); + const subFilePath = startSepRemove(originTemplateFilePath.replace(subDirPath, '')); + const targetTemplateSubDirPath = pathe.join( + targetTemplateDirPath, + startSepRemove(subDirPath.replace(originTemplateDirPath, '')), + ); await betterMkdir(targetTemplateSubDirPath); const templateFileBuf = await fs.promises.readFile(originTemplateFilePath); - await fs.promises.writeFile(pathe.join(subFilePath, subFilePath), templateFileBuf); + await fs.promises.writeFile(pathe.join(targetTemplateSubDirPath, subFilePath), templateFileBuf); }), ); diff --git a/src/modules/files/getOutputDirectory.ts b/src/modules/files/getOutputDirectory.ts index f0edd84..c0ad5a2 100644 --- a/src/modules/files/getOutputDirectory.ts +++ b/src/modules/files/getOutputDirectory.ts @@ -5,16 +5,17 @@ import { exists, getDirname, isDirectory } from 'my-node-fp'; import pathe from 'pathe'; export async function getOutputDirectory(option: Pick, cwd: string) { - const output = option.output ?? cwd; + const outputDirPath = option.output ?? cwd; + const resolvedOutputDirPath = pathe.resolve(outputDirPath); - if (isFalse(await exists(output))) { - await betterMkdir(output); - return pathe.resolve(output); + if (isFalse(await exists(resolvedOutputDirPath))) { + await betterMkdir(pathe.join(resolvedOutputDirPath)); + return resolvedOutputDirPath; } - if (isFalse(await isDirectory(output))) { - return pathe.resolve(await getDirname(output)); + if (isFalse(await isDirectory(outputDirPath))) { + return pathe.resolve(await getDirname(outputDirPath)); } - return pathe.resolve(output); + return pathe.resolve(outputDirPath); } diff --git a/src/templates/modules/__tests__/template.path.test.ts b/src/templates/modules/__tests__/template.path.test.ts index 7352e31..3e7d4b7 100644 --- a/src/templates/modules/__tests__/template.path.test.ts +++ b/src/templates/modules/__tests__/template.path.test.ts @@ -51,7 +51,7 @@ describe('getTemplatePath', () => { .mockImplementationOnce(() => Promise.resolve(false)) .mockImplementationOnce(() => Promise.resolve(true)); - const dirnameTemplatePath = pathe.join(process.cwd(), 'src', 'templates', 'templates'); + const dirnameTemplatePath = pathe.join(process.cwd(), 'src', 'templates'); const templatePath = await getTemplatePath('1110a038cb804e8fac8161070a601f66'); expect(templatePath).toEqual(dirnameTemplatePath); }); diff --git a/src/templates/modules/getTemplatePath.ts b/src/templates/modules/getTemplatePath.ts index 4c5a1a5..8679926 100644 --- a/src/templates/modules/getTemplatePath.ts +++ b/src/templates/modules/getTemplatePath.ts @@ -24,7 +24,7 @@ export async function getTemplatePath(templatePathParam?: string): Promise) { +export async function loadTemplates(option?: Pick) { const defaultTemplatePath = await getTemplatePath(CE_DEFAULT_VALUE.TEMPLATES_PATH); const [defaultConfig, defaultHtml, defaultMarkdown, defaultImage, defaultPdf] = await Promise.all([ getTemplates(pathe.join(defaultTemplatePath, 'config'), {}), @@ -22,7 +22,7 @@ export async function loadTemplates(option: Pick [`pdf-${template.key}`, template.content]), ]); - if (option.templatePath == null) { + if (option?.templatePath == null) { return { default: defaultTemplateMap, template: defaultTemplateMap, diff --git a/src/typeorm/relations/dedupeManaToManyRelationRecord.ts b/src/typeorm/relations/dedupeManaToManyRelationRecord.ts index 4828cec..a146788 100644 --- a/src/typeorm/relations/dedupeManaToManyRelationRecord.ts +++ b/src/typeorm/relations/dedupeManaToManyRelationRecord.ts @@ -15,8 +15,17 @@ export function dedupeManaToManyRelationRecord(relations: IRelationRecord[]) { const nextRelations = Object.values(relationMap).map((chunkedRelations) => { const sortedRelations = chunkedRelations.sort((l, r) => l.dbName.localeCompare(r.dbName)); - const firstRelation = atOrThrow(sortedRelations, 0); - const secondRelation = atOrThrow(sortedRelations, 1); + + const firstRelation = atOrThrow( + sortedRelations, + 0, + new Error(`Cannot found relation: ${sortedRelations.at(0)?.entity} - ${sortedRelations.at(1)?.entity}`), + ); + const secondRelation = atOrThrow( + sortedRelations, + 1, + new Error(`Cannot found relation: ${sortedRelations.at(0)?.entity} - ${sortedRelations.at(1)?.entity}`), + ); const firstNext = { ...firstRelation, inverseJoinColumnName: secondRelation.joinColumnName }; const secondNext = { ...secondRelation, inverseJoinColumnName: firstRelation.joinColumnName, isDuplicate: true }; diff --git a/templates/config/init-command.eta b/templates/config/json.eta similarity index 93% rename from templates/config/init-command.eta rename to templates/config/json.eta index c73c729..32e2290 100644 --- a/templates/config/init-command.eta +++ b/templates/config/json.eta @@ -21,7 +21,11 @@ <% } %> // erdia entity database file path - "database-path": "<%= it.config.databasePath %>", + <% if (it.config.templatePath != null) { %> + "database-path": "<%= it.config.databasePath %>", + <% } else { %> + // "database-path": "", + <% } %> // erdia entity database file path <% if (it.config.routeBasePath != null) { %> @@ -34,7 +38,7 @@ "version-from": "<%= it.config.versionFrom %>", // If the versionFrom option set file, read the file from this path -<% if (it.config.versionFrom != null) { %> +<% if (it.config.versionPath != null) { %> "version-path": "<%= it.config.versionPath %>", <% } else { %> // "version-path": "",