Skip to content

Commit

Permalink
feat(di): add class container for dependency injection
Browse files Browse the repository at this point in the history
- add class container for dependency injection
- add new testcase
- migrate template file: keep tempalte file format, not TypeScript source code
  • Loading branch information
imjuni committed Apr 7, 2024
1 parent 46038b4 commit 17df557
Show file tree
Hide file tree
Showing 97 changed files with 1,274 additions and 1,013 deletions.
1 change: 0 additions & 1 deletion .configs/jest.setup.cjs

This file was deleted.

27 changes: 16 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ jobs:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build
- run: npm run test:silent
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- run: pnpm i
- run: pnpm run build
- run: pnpm run test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
36 changes: 0 additions & 36 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,11 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Jest",
"request": "launch",
"runtimeArgs": ["run-script", "test:debug", "${input:testPath}"],
"runtimeExecutable": "npm",
"skipFiles": ["<node_internals>/**"],
"stopOnEntry": true,
"type": "node"
},
{
"type": "node",
"request": "launch",
"name": "Launch Test",
"args": ["--runInBand", "--no-cache", "${input:testPath}"],
"runtimeArgs": ["-r", "ts-node/register", "--inspect"],
"stopOnEntry": false,
"sourceMaps": true,
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"env": {
"TS_NODE_PROJECT": "tsconfig.json"
}
},
{
"type": "node",
"request": "attach",
"name": "Attach Process",
"port": 9229
}
],
"inputs": [
{
"type": "promptString",
"id": "testPath",
"description": "Path Name your testcase",
"default": ""
},
{
"type": "promptString",
"id": "testName",
"description": "Name your testcase",
"default": ""
}
]
}
14 changes: 0 additions & 14 deletions jest.config.cjs

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"bundle-cli": "FORMAT=cjs node .configs/esbuild.cli.mjs",
"bundle": "run-s clean bundle-cli bundle-lib",
"build": "pnpm run clean && tsc --incremental --project tsconfig.prod.json",
"test": "vitest --run",
"test": "vitest --run --coverage",
"lint": "eslint --cache .",
"lint-staged": "lint-staged",
"prettier": "prettier --write src/**/*.ts",
Expand Down Expand Up @@ -70,7 +70,8 @@
}
},
"files": [
"dist"
"dist",
"templates"
],
"bin": {
"erdia": "dist/cjs/cli.cjs"
Expand Down Expand Up @@ -129,6 +130,7 @@
"dependencies": {
"@maeum/cli-logo": "^1.0.0",
"alasql": "^4.1.3",
"awilix": "^10.0.2",
"chalk": "^4.1.2",
"compare-versions": "^6.1.0",
"consola": "^3.2.3",
Expand All @@ -140,6 +142,7 @@
"filenamify": "^4.3.0",
"find-up": "^5.0.0",
"fuse.js": "^6.6.2",
"glob": "^10.3.12",
"globby": "^11.1.0",
"inquirer": "^8.2.4",
"inquirer-autocomplete-prompt": "^2.0.0",
Expand All @@ -150,6 +153,7 @@
"my-easy-fp": "^0.22.0",
"my-node-fp": "^0.10.3",
"my-only-either": "^1.1.2",
"pathe": "^1.1.2",
"prettier": "^3.0.3",
"puppeteer": "^22.6.1",
"read-pkg": "^5.2.0",
Expand Down
22 changes: 17 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CE_COMMAND_LIST } from '#/configs/const-enum/CE_COMMAND_LIST';
import type { IBuildCommandOption } from '#/configs/interfaces/IBuildCommandOption';
import type { ICommonOption } from '#/configs/interfaces/ICommonOption';
import { preLoadConfig } from '#/configs/modules/preLoadConfig';
import '#/modules/containers/container';
import consola from 'consola';
import { isError } from 'my-easy-fp';
import sourceMapSupport from 'source-map-support';
Expand Down
30 changes: 24 additions & 6 deletions src/cli/commands/buildDocumentCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ import { flushDatabase } from '#/databases/flushDatabase';
import type { IRelationRecord } from '#/databases/interfaces/IRelationRecord';
import { openDatabase } from '#/databases/openDatabase';
import { processDatabase } from '#/databases/processDatabase';
import { loadTemplates } from '#/template/loadTemplates';
import { container } from '#/modules/containers/container';
import { SymbolDataSource } from '#/modules/containers/keys/SymbolDataSource';
import { SymbolDefaultTemplate } from '#/modules/containers/keys/SymbolDefaultTemplate';
import { SymbolTemplate } from '#/modules/containers/keys/SymbolTemplate';
import { SymbolTemplateRenderer } from '#/modules/containers/keys/SymbolTemplateRenderer';
import { betterMkdir } from '#/modules/files/betterMkdir';
import { TemplateRenderer } from '#/templates/TemplateRenderer';
import { loadTemplates } from '#/templates/modules/loadTemplates';
import { getColumnRecord } from '#/typeorm/columns/getColumnRecord';
import { getEntityRecords } from '#/typeorm/entities/getEntityRecords';
import { getDataSource } from '#/typeorm/getDataSource';
import { getIndexRecords } from '#/typeorm/indices/getIndexRecords';
import { dedupeManaToManyRelationRecord } from '#/typeorm/relations/dedupeManaToManyRelationRecord';
import { getRelationRecords } from '#/typeorm/relations/getRelationRecords';
import { showLogo } from '@maeum/cli-logo';
import { asValue } from 'awilix';
import chalk from 'chalk';
import consola from 'consola';
import fastSafeStringify from 'fast-safe-stringify';
Expand All @@ -49,16 +57,19 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) {
consola.info(`connection initialize: "${chalk.yellowBright(`${option.dataSourcePath}`)}"`);

const dataSource = await getDataSource(option);
await dataSource.initialize();
const [templates] = await Promise.all([await loadTemplates(option), await dataSource.initialize()]);
const renderer = new TemplateRenderer(templates.default, templates.template);

if (isFalse(dataSource.isInitialized)) {
throw new Error(`Cannot initialize in ${fastSafeStringify(dataSource.options, undefined, 2)}`);
}

localDataSource = dataSource;
await loadTemplates(option);
container.register(SymbolDefaultTemplate, asValue(templates.default));
container.register(SymbolTemplate, asValue(templates.template));
container.register(SymbolDataSource, asValue(dataSource));
container.register(SymbolTemplateRenderer, asValue(renderer));

const metadata = await getMetadata(dataSource, option);
const metadata = await getMetadata(option);

consola.success('connection initialized');
consola.info(`version: ${metadata.version}`);
Expand Down Expand Up @@ -113,7 +124,12 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) {
};

const documents = await createHtml(option, renderData);
await Promise.all(documents.map((document) => fs.promises.writeFile(document.filename, document.content)));
await Promise.all(
documents.map(async (document) => {
await betterMkdir(document.dirname);
await fs.promises.writeFile(document.filename, document.content);
}),
);

if (!option.skipImageInHtml) {
const imageDocument = await createImageHtml(imageOption, renderData);
Expand All @@ -125,6 +141,7 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) {

if (option.format === CE_OUTPUT_FORMAT.MARKDOWN) {
const document = await createMarkdown(option, renderData);
await betterMkdir(document.dirname);
await fs.promises.writeFile(document.filename, document.content);
return [document.filename];
}
Expand All @@ -137,6 +154,7 @@ export async function buildDocumentCommandHandler(option: IBuildCommandOption) {

if (option.format === CE_OUTPUT_FORMAT.IMAGE) {
const document = await createImageHtml(option, renderData);
await betterMkdir(document.dirname);
const filenames = await writeToImage(document, option, renderData);
return filenames;
}
Expand Down
21 changes: 12 additions & 9 deletions src/cli/commands/cleanDocumentCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { getMetadata } from '#/common/getMetadata';
import { CE_DEFAULT_VALUE } from '#/configs/const-enum/CE_DEFAULT_VALUE';
import type { ICommonOption } from '#/configs/interfaces/ICommonOption';
import { getCwd } from '#/configs/modules/getCwd';
import { getOutputDirectory } from '#/tools/files/getOutputDirectory';
import { container } from '#/modules/containers/container';
import { SymbolDataSource } from '#/modules/containers/keys/SymbolDataSource';
import { getOutputDirectory } from '#/modules/files/getOutputDirectory';
import { getDataSource } from '#/typeorm/getDataSource';
import { showLogo } from '@maeum/cli-logo';
import { asValue } from 'awilix';
import consola from 'consola';
import del from 'del';
import fastSafeStringify from 'fast-safe-stringify';
import { isError, isFalse } from 'my-easy-fp';
import path from 'path';
import pathe from 'pathe';
import type { DataSource } from 'typeorm';

export async function cleanDocumentCommandHandler(option: ICommonOption) {
Expand All @@ -33,17 +36,17 @@ export async function cleanDocumentCommandHandler(option: ICommonOption) {
throw new Error(`Cannot initialize in ${fastSafeStringify(dataSource.options, undefined, 2)}`);
}

localDataSource = dataSource;
container.register(SymbolDataSource, asValue(dataSource));

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

const filenames = [
path.join(outputDir, CE_DEFAULT_VALUE.HTML_MERMAID_FILENAME),
path.join(outputDir, CE_DEFAULT_VALUE.HTML_INDEX_FILENAME),
path.join(outputDir, `${metadata.name}.md`),
path.join(outputDir, `${metadata.name}.png`),
path.join(outputDir, `${metadata.name}.svg`),
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`),
];

await del(filenames);
Expand Down
Loading

0 comments on commit 17df557

Please sign in to comment.