Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(di): fix infer template-path problem that use default templates #67

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/cli/builders/buildOptionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Argv } from 'yargs';
export function buildOptionBuilder<T>(args: Argv<T>) {
// 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',
Expand Down
9 changes: 4 additions & 5 deletions src/cli/commands/buildDocumentCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)}`);
Expand Down Expand Up @@ -168,8 +166,9 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) {

return [];
} finally {
if (localDataSource != null) {
await localDataSource.destroy();
const dataSource = container.resolve<DataSource>(SymbolDataSource);
if (dataSource != null) {
await dataSource.destroy();
}
}
}
12 changes: 6 additions & 6 deletions src/cli/commands/cleanDocumentCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/cli/commands/initConfigCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
18 changes: 11 additions & 7 deletions src/cli/commands/templateEjectCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICommonOption, 'output' | 'showLogo'>) {
Expand All @@ -24,15 +24,16 @@ export async function templateEjectCommandHandler(option: Pick<ICommonOption, 'o
consola.info('erdia build start');
}

const outputDir = await getOutputDirectory(option, getCwd(process.env));
const outputDirPath = await getOutputDirectory(option, getCwd(process.env));
const originTemplateDirPath = await getTemplatePath(CE_DEFAULT_VALUE.TEMPLATES_PATH);
const targetTemplateDirPath = pathe.join(outputDir, '__templates');
const targetTemplateDirPath =
option.output == null ? pathe.join(outputDirPath, CE_DEFAULT_VALUE.TEMPLATES_PATH) : outputDirPath;

consola.info('Output directory: ', targetTemplateDirPath);

const originTemplateGlobPaths = new Glob(pathe.join(originTemplateDirPath, `**`, '*.eta'), {
absolute: true,
ignore: defaultExclude,
ignore: [...defaultExclude, 'config/**'],
cwd: originTemplateDirPath,
windowsPathsNoEscape: true,
});
Expand All @@ -41,13 +42,16 @@ export async function templateEjectCommandHandler(option: Pick<ICommonOption, 'o
await Promise.all(
originTemplateFilePaths.map(async (originTemplateFilePath) => {
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);
}),
);

Expand Down
15 changes: 8 additions & 7 deletions src/modules/files/getOutputDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { exists, getDirname, isDirectory } from 'my-node-fp';
import pathe from 'pathe';

export async function getOutputDirectory(option: Pick<IBuildCommandOption, 'output'>, 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);
}
2 changes: 1 addition & 1 deletion src/templates/modules/__tests__/template.path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion src/templates/modules/getTemplatePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function getTemplatePath(templatePathParam?: string): Promise<strin
return packageRootTemplatePath;
}

const distTemplatePath = pathe.resolve(pathe.join(currentFilePath, '..', CE_DEFAULT_VALUE.TEMPLATES_PATH));
const distTemplatePath = pathe.resolve(pathe.join(currentFilePath, '..', '..', CE_DEFAULT_VALUE.TEMPLATES_PATH));
if (await exists(distTemplatePath)) {
return distTemplatePath;
}
Expand Down
4 changes: 2 additions & 2 deletions src/templates/modules/loadTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getTemplatePath } from '#/templates/modules/getTemplatePath';
import { getTemplates } from '#/templates/modules/getTemplates';
import pathe from 'pathe';

export async function loadTemplates(option: Pick<IDocumentOption, 'templatePath'>) {
export async function loadTemplates(option?: Pick<IDocumentOption, 'templatePath'>) {
const defaultTemplatePath = await getTemplatePath(CE_DEFAULT_VALUE.TEMPLATES_PATH);
const [defaultConfig, defaultHtml, defaultMarkdown, defaultImage, defaultPdf] = await Promise.all([
getTemplates(pathe.join(defaultTemplatePath, 'config'), {}),
Expand All @@ -22,7 +22,7 @@ export async function loadTemplates(option: Pick<IDocumentOption, 'templatePath'
...defaultPdf.map((template): [string, string] => [`pdf-${template.key}`, template.content]),
]);

if (option.templatePath == null) {
if (option?.templatePath == null) {
return {
default: defaultTemplateMap,
template: defaultTemplateMap,
Expand Down
13 changes: 11 additions & 2 deletions src/typeorm/relations/dedupeManaToManyRelationRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { %>
Expand All @@ -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": "",
Expand Down
Loading