Skip to content

Commit

Permalink
fix(template): fix config template problem
Browse files Browse the repository at this point in the history
- The `eject` command now creates a template file in the `templatePath` option instead of `output`.
- This change was made because the `output` option is redundant with the `output` option of the `build` command, and because the `output` directory was being mixed up with templates and documentation when using `.erdiarc` files.
  • Loading branch information
imjuni committed Apr 12, 2024
1 parent e3186f3 commit 9570923
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/common/__tests__/version.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getFileVersion } from '#/common/getFileVersion';
import { getVersion } from '#/common/getVersion';
import * as getFindFile from '#/modules/files/getFindFile';
import * as getOutputDirectory from '#/modules/files/getOutputDirectory';
import * as getOutputDirPath from '#/modules/files/getOutputDirPath';
import dayjs from 'dayjs';
import fs from 'fs';
import { describe, expect, test, vitest } from 'vitest';
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('getVersion', () => {
.spyOn(fs.promises, 'readFile')
.mockImplementation(() => Promise.resolve(Buffer.from('1.1.1')));
const tspSpyOn04 = vitest
.spyOn(getOutputDirectory, 'getOutputDirectory')
.spyOn(getOutputDirPath, 'getOutputDirPath')
.mockImplementation(() => Promise.resolve('/a/b/c'));

const version = await getVersion({ version: '1.1.1' }, { versionFrom: 'file', versionPath: '/a/b/c' });
Expand Down
4 changes: 2 additions & 2 deletions src/common/getVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import type { IBuildCommandOption } from '#/configs/interfaces/IBuildCommandOption';
import { getCwd } from '#/configs/modules/getCwd';
import { getFindFile } from '#/modules/files/getFindFile';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import dayjs from 'dayjs';
import fs from 'fs';
import pathe from 'pathe';
Expand All @@ -14,7 +14,7 @@ async function getVersionFilename(
) {
if (option.versionPath != null) {
const filename = await getFindFile(
pathe.join(await getOutputDirectory({ output: option.versionPath }, getCwd(process.env)), versionFilename),
pathe.join(await getOutputDirPath({ output: option.versionPath }, getCwd(process.env)), versionFilename),
{ cwd: getCwd(process.env) },
);

Expand Down
8 changes: 4 additions & 4 deletions src/configs/modules/getConfigContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export async function getConfigContent() {
*/

const sourceGlobFiles = new Glob(['**/*.js', '**/*.cjs', '**/*.mjs', '**/*.ts', '**/*.cts', '**/*.mts'], {
absolute: true,
// absolute: true,
ignore: defaultExclude,
cwd: process.cwd(),
onlyFiles: true,
});
const sourceFiles = getGlobFiles(sourceGlobFiles);

const everyGlobFiles = new Glob(['**/*'], {
absolute: true,
// absolute: true,
ignore: defaultExclude,
cwd: process.cwd(),
dot: true,
Expand All @@ -53,7 +53,7 @@ export async function getConfigContent() {
const everyFiles = getGlobFiles(everyGlobFiles);

const directoryGlobDirPaths = new Glob(['**/*'], {
absolute: true,
// absolute: true,
ignore: defaultExclude,
cwd: process.cwd(),
dot: true,
Expand Down Expand Up @@ -214,7 +214,7 @@ export async function getConfigContent() {
]);

const templateDir = await (answer.isEjectTemplate
? ejecting({ output: getCwd(process.env), showLogo: false })
? ejecting({ templatePath: pathe.join(getCwd(process.env), CE_DEFAULT_VALUE.TEMPLATES_PATH), showLogo: false })
: Promise.resolve(undefined));
const renderer = container.resolve<TemplateRenderer>(SymbolTemplateRenderer);
const file = await renderer.evaluate(CE_TEMPLATE_NAME.CONFIG_JSON, {
Expand Down
4 changes: 2 additions & 2 deletions src/creators/createHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { getRenderData } from '#/creators/getRenderData';
import type { IErdiaDocument } from '#/creators/interfaces/IErdiaDocument';
import { container } from '#/modules/containers/container';
import { SymbolTemplateRenderer } from '#/modules/containers/keys/SymbolTemplateRenderer';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import type { TemplateRenderer } from '#/templates/TemplateRenderer';
import { CE_TEMPLATE_NAME } from '#/templates/cosnt-enum/CE_TEMPLATE_NAME';
import consola from 'consola';
Expand Down Expand Up @@ -65,7 +65,7 @@ export async function createHtml(
option: Pick<IBuildCommandOption, 'output' | 'components' | 'prettierConfig'>,
renderData: AsyncReturnType<typeof getRenderData>,
) {
const outputDir = await getOutputDirectory(option, getCwd(process.env));
const outputDir = await getOutputDirPath(option, getCwd(process.env));

consola.info(`export component: ${option.components.join(', ')}`);

Expand Down
5 changes: 3 additions & 2 deletions src/creators/createMarkdown.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { IBuildCommandOption } from '#/configs/interfaces/IBuildCommandOption';
import { getCwd } from '#/configs/modules/getCwd';
import { applyPrettier } from '#/creators/applyPretter';
import type { getRenderData } from '#/creators/getRenderData';
import type { IErdiaDocument } from '#/creators/interfaces/IErdiaDocument';
import { container } from '#/modules/containers/container';
import { SymbolTemplateRenderer } from '#/modules/containers/keys/SymbolTemplateRenderer';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import type { TemplateRenderer } from '#/templates/TemplateRenderer';
import { CE_TEMPLATE_NAME } from '#/templates/cosnt-enum/CE_TEMPLATE_NAME';
import { getDirname } from 'my-node-fp';
import pathe from 'pathe';
import type { AsyncReturnType } from 'type-fest';

Expand All @@ -18,7 +19,7 @@ export async function createMarkdown(
const rawMarkdown = await renderer.evaluate(CE_TEMPLATE_NAME.MARKDOWN_DOCUMENT, renderData);
const prettiedMarkdown = await applyPrettier(rawMarkdown, 'md', option.prettierConfig);
const markdownFileName = `${renderData.metadata.name}.md`;
const outputDir = await getDirname(option.output ?? process.cwd());
const outputDir = await getOutputDirPath(option, getCwd(process.env));

return {
filename: pathe.resolve(pathe.join(outputDir, markdownFileName)),
Expand Down
4 changes: 2 additions & 2 deletions src/databases/flushDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import type { IBuildCommandOption } from '#/configs/interfaces/IBuildCommandOption';
import type { TDatabaseRecord } from '#/databases/interfaces/TDatabaseRecord';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import fs from 'node:fs';
import pathe from 'pathe';

export async function flushDatabase(
option: Pick<IBuildCommandOption, 'databasePath'>,
records: TDatabaseRecord[],
): Promise<TDatabaseRecord[]> {
const dirname = await getOutputDirectory({ output: option.databasePath }, process.cwd());
const dirname = await getOutputDirPath({ output: option.databasePath }, process.cwd());
const filename = pathe.join(dirname, CE_DEFAULT_VALUE.DATABASE_FILENAME);

if (filename == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/databases/openDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import type { IBuildCommandOption } from '#/configs/interfaces/IBuildCommandOption';
import type { TDatabaseRecord } from '#/databases/interfaces/TDatabaseRecord';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import { parse } from 'jsonc-parser';
import { isFalse } from 'my-easy-fp';
import { exists } from 'my-node-fp';
import fs from 'node:fs';
import pathe from 'pathe';

export async function openDatabase(option: Pick<IBuildCommandOption, 'databasePath'>): Promise<TDatabaseRecord[]> {
const dirname = await getOutputDirectory({ output: option.databasePath }, process.cwd());
const dirname = await getOutputDirPath({ output: option.databasePath }, process.cwd());
const filename = pathe.join(dirname, CE_DEFAULT_VALUE.DATABASE_FILENAME);

if (filename == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/commands/cleaning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ICommonOption } from '#/configs/interfaces/ICommonOption';
import { getCwd } from '#/configs/modules/getCwd';
import { container } from '#/modules/containers/container';
import { SymbolDataSource } from '#/modules/containers/keys/SymbolDataSource';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import { getDataSource } from '#/typeorm/getDataSource';
import { showLogo } from '@maeum/cli-logo';
import { asValue } from 'awilix';
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function cleaning(option: ICommonOption) {
container.register(SymbolDataSource, asValue(dataSource));

const metadata = await getMetadata({ ...option, versionFrom: 'package.json', projectName: 'app' });
const outputDirPath = await getOutputDirectory(option, getCwd(process.env));
const outputDirPath = await getOutputDirPath(option, getCwd(process.env));

const filenames = [
pathe.join(outputDirPath, CE_DEFAULT_VALUE.HTML_MERMAID_FILENAME),
Expand Down
18 changes: 11 additions & 7 deletions src/modules/commands/ejecting.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import type { ICommonOption } from '#/configs/interfaces/ICommonOption';
import type { IDocumentOption } from '#/configs/interfaces/IDocumentOption';
import { getCwd } from '#/configs/modules/getCwd';
import { container } from '#/modules/containers/container';
import { SymbolLogger } from '#/modules/containers/keys/SymbolLogger';
import { betterMkdir } from '#/modules/files/betterMkdir';
import { getGlobFiles } from '#/modules/files/getGlobFiles';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getTemplateDirPath } from '#/modules/files/getTemplateDirPath';
import type { Logger } from '#/modules/loggers/Logger';
import { createLogger } from '#/modules/loggers/createLogger';
import { defaultExclude } from '#/modules/scopes/defaultExclude';
import { getTemplatePath } from '#/templates/modules/getTemplatePath';
import { getTemplateModulePath } from '#/templates/modules/getTemplateModulePath';
import { Glob } from 'glob';
import { isError } from 'my-easy-fp';
import { getDirname, startSepRemove } from 'my-node-fp';
import fs from 'node:fs';
import pathe from 'pathe';

export async function ejecting(option: Pick<ICommonOption, 'output' | 'showLogo'>, logging?: boolean) {
export async function ejecting(
option: Pick<ICommonOption & IDocumentOption, 'showLogo' | 'templatePath'>,
logging?: boolean,
) {
createLogger(logging);
const logger = container.resolve<Logger>(SymbolLogger);

try {
const outputDirPath = await getOutputDirectory(option, getCwd(process.env));
const originTemplateDirPath = await getTemplatePath(CE_DEFAULT_VALUE.TEMPLATES_PATH);
const templateDirPath = await getTemplateDirPath(option, getCwd(process.env));
const originTemplateDirPath = await getTemplateModulePath(CE_DEFAULT_VALUE.TEMPLATES_PATH);
const targetTemplateDirPath =
option.output == null ? pathe.join(outputDirPath, CE_DEFAULT_VALUE.TEMPLATES_PATH) : outputDirPath;
option.templatePath == null ? pathe.join(templateDirPath, CE_DEFAULT_VALUE.TEMPLATES_PATH) : templateDirPath;

logger.info('Output directory: ', targetTemplateDirPath);
logger.info('Template directory: ', targetTemplateDirPath);

const originTemplateGlobPaths = new Glob(pathe.join(originTemplateDirPath, `**`, '*.eta'), {
absolute: true,
Expand Down
8 changes: 4 additions & 4 deletions src/modules/files/__tests__/file.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getOutputDirPath } from '#/modules/files/getOutputDirPath';
import { getPuppeteerConfig } from '#/modules/getPuppeteerConfig';
import { getSlashEndRoutePath } from '#/modules/getSlashEndRoutePath';
import * as mnf from 'my-node-fp';
Expand Down Expand Up @@ -31,7 +31,7 @@ describe('getOutputDirectory', () => {
const existsSpyOn = vitest.spyOn(mnf, 'exists').mockImplementation(() => Promise.resolve(false));
const mkdirSpyOn = vitest.spyOn(fs.promises, 'mkdir').mockImplementation(() => Promise.resolve(''));

const p = await getOutputDirectory({ output: undefined }, 'i-am-cwd');
const p = await getOutputDirPath({ output: undefined }, 'i-am-cwd');

existsSpyOn.mockRestore();
mkdirSpyOn.mockRestore();
Expand All @@ -44,7 +44,7 @@ describe('getOutputDirectory', () => {
const isDirectorySpyOn = vitest.spyOn(mnf, 'isDirectory').mockImplementation(() => Promise.resolve(false));
const mkdirSpyOn = vitest.spyOn(fs.promises, 'mkdir').mockImplementation(() => Promise.resolve(''));

const p = await getOutputDirectory({ output: undefined }, 'examples');
const p = await getOutputDirPath({ output: undefined }, 'examples');

existsSpyOn.mockRestore();
isDirectorySpyOn.mockRestore();
Expand All @@ -58,7 +58,7 @@ describe('getOutputDirectory', () => {
const isDirectorySpyOn = vitest.spyOn(mnf, 'isDirectory').mockImplementation(() => Promise.resolve(true));
const mkdirSpyOn = vitest.spyOn(fs.promises, 'mkdir').mockImplementation(() => Promise.resolve(''));

const p = await getOutputDirectory({ output: undefined }, 'examples');
const p = await getOutputDirPath({ output: undefined }, 'examples');

existsSpyOn.mockRestore();
isDirectorySpyOn.mockRestore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isFalse } from 'my-easy-fp';
import { exists, getDirname, isDirectory } from 'my-node-fp';
import pathe from 'pathe';

export async function getOutputDirectory(option: Pick<IBuildCommandOption, 'output'>, cwd: string) {
export async function getOutputDirPath(option: Pick<IBuildCommandOption, 'output'>, cwd: string) {
const outputDirPath = option.output ?? cwd;
const resolvedOutputDirPath = pathe.resolve(outputDirPath);

Expand Down
22 changes: 22 additions & 0 deletions src/modules/files/getTemplateDirPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import type { IDocumentOption } from '#/configs/interfaces/IDocumentOption';
import { betterMkdir } from '#/modules/files/betterMkdir';
import { isFalse } from 'my-easy-fp';
import { exists, getDirname, isDirectory } from 'my-node-fp';
import pathe from 'pathe';

export async function getTemplateDirPath(option: Pick<IDocumentOption, 'templatePath'>, cwd: string) {
const templateDirPath = pathe.join(option.templatePath ?? pathe.join(cwd, CE_DEFAULT_VALUE.TEMPLATES_PATH));
const resolvedTemplateDirPath = pathe.resolve(templateDirPath);

if (isFalse(await exists(resolvedTemplateDirPath))) {
await betterMkdir(pathe.join(resolvedTemplateDirPath));
return resolvedTemplateDirPath;
}

if (isFalse(await isDirectory(templateDirPath))) {
return pathe.resolve(await getDirname(templateDirPath));
}

return pathe.resolve(templateDirPath);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const configTemplate = `
{
// directory for output files
"output": "<%= it.config.output %>",
Expand All @@ -21,7 +22,7 @@
<% } %>
// erdia entity database file path
<% if (it.config.templatePath != null) { %>
<% if (it.config.databasePath != null) { %>
"database-path": "<%= it.config.databasePath %>",
<% } else { %>
// "database-path": "",
Expand All @@ -46,7 +47,7 @@
// output format of generated documents
// - html
// - markdown
// - md
// - pdf
// - image
"format": "<%= it.config.format %>",
Expand Down Expand Up @@ -104,3 +105,4 @@
// "image-format": "svg",
<% } %>
}
`;
29 changes: 29 additions & 0 deletions src/templates/modules/getTemplateModulePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import { exists } from 'my-node-fp';
import pathe from 'pathe';

export async function getTemplateModulePath(templatePathParam?: string): Promise<string> {
const currentFilePath = pathe.resolve(__dirname);

if (templatePathParam != null) {
const currentWithTemplatePath = pathe.resolve(pathe.join(currentFilePath, templatePathParam));
if (await exists(currentWithTemplatePath)) {
return currentWithTemplatePath;
}
}

const packageRootTemplatePath = pathe.resolve(
pathe.join(currentFilePath, '..', '..', '..', CE_DEFAULT_VALUE.TEMPLATES_PATH),
);

if (await exists(packageRootTemplatePath)) {
return packageRootTemplatePath;
}

const distTemplatePath = pathe.resolve(pathe.join(currentFilePath, '..', '..', CE_DEFAULT_VALUE.TEMPLATES_PATH));
if (await exists(distTemplatePath)) {
return distTemplatePath;
}

throw new Error('cannot found template directory!');
}
26 changes: 2 additions & 24 deletions src/templates/modules/getTemplatePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import { getTemplateModulePath } from '#/templates/modules/getTemplateModulePath';
import { exists } from 'my-node-fp';
import pathe from 'pathe';

Expand All @@ -7,27 +7,5 @@ export async function getTemplatePath(templatePathParam?: string): Promise<strin
return pathe.resolve(templatePathParam);
}

const currentFilePath = pathe.resolve(__dirname);

if (templatePathParam != null) {
const currentWithTemplatePath = pathe.resolve(pathe.join(currentFilePath, templatePathParam));
if (await exists(currentWithTemplatePath)) {
return currentWithTemplatePath;
}
}

const packageRootTemplatePath = pathe.resolve(
pathe.join(currentFilePath, '..', '..', '..', CE_DEFAULT_VALUE.TEMPLATES_PATH),
);

if (await exists(packageRootTemplatePath)) {
return packageRootTemplatePath;
}

const distTemplatePath = pathe.resolve(pathe.join(currentFilePath, '..', '..', CE_DEFAULT_VALUE.TEMPLATES_PATH));
if (await exists(distTemplatePath)) {
return distTemplatePath;
}

throw new Error('cannot found template directory!');
return getTemplateModulePath(templatePathParam);
}
Loading

0 comments on commit 9570923

Please sign in to comment.