diff --git a/src/modules/commands/bundling.ts b/src/modules/commands/bundling.ts index ba79cd8..d3cbc6f 100644 --- a/src/modules/commands/bundling.ts +++ b/src/modules/commands/bundling.ts @@ -16,9 +16,7 @@ import { ProjectContainer } from '#/modules/file/ProjectContainer'; import { checkOutputFile } from '#/modules/file/checkOutputFile'; import { getTsExcludeFiles } from '#/modules/file/getTsExcludeFiles'; import { getTsIncludeFiles } from '#/modules/file/getTsIncludeFiles'; -import { addCurrentDirPrefix } from '#/modules/path/addCurrentDirPrefix'; import { posixJoin } from '#/modules/path/modules/posixJoin'; -import { posixRelative } from '#/modules/path/modules/posixRelative'; import { posixResolve } from '#/modules/path/modules/posixResolve'; import { ExcludeContainer } from '#/modules/scope/ExcludeContainer'; import { IncludeContainer } from '#/modules/scope/IncludeContainer'; @@ -28,12 +26,11 @@ import { CE_TEMPLATE_NAME } from '#/templates/const-enum/CE_TEMPLATE_NAME'; import type { IIndexFileWriteParams } from '#/templates/interfaces/IIndexFileWriteParams'; import type { IIndexRenderData } from '#/templates/interfaces/IIndexRenderData'; import { TemplateContainer } from '#/templates/modules/TemplateContainer'; +import { getInlineDeclarationRenderData } from '#/templates/modules/getInlineDeclarationRenderData'; import { getRenderData } from '#/templates/modules/getRenderData'; import { getSelectStyle } from '#/templates/modules/getSelectStyle'; import chalk from 'chalk'; import dayjs from 'dayjs'; -import { replaceSepToPosix } from 'my-node-fp'; -import path from 'node:path'; import type * as tsm from 'ts-morph'; export async function bundling(buildOptions: TCommandBuildOptions, bundleOption: TBundleOptions) { @@ -194,30 +191,7 @@ export async function bundling(buildOptions: TCommandBuildOptions, bundleOption: CE_TEMPLATE_NAME.DECLARATION_FILE_TEMPLATE, { options: { quote: bundleOption.quote }, - declarations: await Promise.all( - inlineDeclarations - .filter((inlineDeclaration) => statementMap.get(inlineDeclaration.filePath) == null) - .map((declaration) => { - const relativePath = - bundleOption.output != null - ? addCurrentDirPrefix( - posixRelative( - bundleOption.output, - path.join( - path.dirname(declaration.filePath), - path.basename(declaration.filePath, path.extname(declaration.filePath)), - ), - ), - ) - : replaceSepToPosix( - `.${path.posix.sep}${path.join( - path.dirname(declaration.filePath), - path.basename(declaration.filePath, path.extname(declaration.filePath)), - )}`, - ); - return { ...declaration, relativePath }; - }), - ), + declarations: getInlineDeclarationRenderData(inlineDeclarations, bundleOption), }, ); diff --git a/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts b/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts new file mode 100644 index 0000000..d368eb9 --- /dev/null +++ b/src/templates/modules/__tests__/get.inline.declaration.render.data.test.ts @@ -0,0 +1,112 @@ +import { CE_INLINE_COMMENT_KEYWORD } from '#/comments/const-enum/CE_INLINE_COMMENT_KEYWORD'; +import type { getInlineCommentedFiles } from '#/comments/getInlineCommentedFiles'; +import { getInlineDeclarationRenderData } from '#/templates/modules/getInlineDeclarationRenderData'; +import pathe from 'pathe'; +import { describe, expect, it } from 'vitest'; + +describe('getInlineDeclarationRenderData', () => { + it('successfully generate render data, with output directory', () => { + const declarations: ReturnType = [ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + ]; + + const data = getInlineDeclarationRenderData(declarations, { + output: pathe.join(process.cwd(), '/a/b/e'), + fileExt: 'to-js', + }); + + expect(data).toMatchObject([ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: '../c/d/a.ts', + extname: { origin: '.ts', render: '.js' }, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: '../c/e/b.ts', + extname: { origin: '.ts', render: '.js' }, + }, + ]); + }); + + it('successfully generate render data, without output directory', () => { + const declarations: ReturnType = [ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: `// ${CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD}`, + tag: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD, + pos: { + line: 0, + column: 0, + start: 0, + }, + workspaces: undefined, + }, + ]; + + const data = getInlineDeclarationRenderData(declarations, { + fileExt: 'to-js', + }); + + expect(data).toMatchObject([ + { + filePath: pathe.join(process.cwd(), '/a/b/c/d/a.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: `./${pathe.join(process.cwd(), '/a/b/c/d/a.ts')}`, + extname: { origin: '.ts', render: '.js' }, + }, + { + filePath: pathe.join(process.cwd(), '/a/b/c/e/b.ts'), + commentCode: '// @ctix-declaration', + tag: '@ctix-declaration', + pos: { line: 0, column: 0, start: 0 }, + workspaces: undefined, + relativePath: `./${pathe.join(process.cwd(), '/a/b/c/e/b.ts')}`, + extname: { origin: '.ts', render: '.js' }, + }, + ]); + }); +}); diff --git a/src/templates/modules/getInlineDeclarationRenderData.ts b/src/templates/modules/getInlineDeclarationRenderData.ts new file mode 100644 index 0000000..c61988c --- /dev/null +++ b/src/templates/modules/getInlineDeclarationRenderData.ts @@ -0,0 +1,40 @@ +import type { getInlineCommentedFiles } from '#/comments/getInlineCommentedFiles'; +import type { TBundleOptions } from '#/configs/interfaces/TBundleOptions'; +import { addCurrentDirPrefix } from '#/modules/path/addCurrentDirPrefix'; +import { getExtname } from '#/modules/path/getExtname'; +import { getImportStatementExtname } from '#/modules/path/getImportStatementExtname'; +import { posixRelative } from '#/modules/path/modules/posixRelative'; +import { replaceSepToPosix } from 'my-node-fp'; +import path from 'path'; +import pathe from 'pathe'; +import type { SetOptional } from 'type-fest'; + +export function getInlineDeclarationRenderData( + declarations: ReturnType, + options: SetOptional, 'output'>, +) { + const renderDatas = declarations.map((declaration) => { + const extname = getExtname(declaration.filePath); + const renderExtname = getImportStatementExtname(options.fileExt, extname); + const dirname = pathe.dirname(declaration.filePath); + const basename = pathe.basename(declaration.filePath, extname); + + const relativePath = + options.output != null + ? addCurrentDirPrefix( + posixRelative(options.output, pathe.join(dirname, `${basename}${extname}`)), + ) + : replaceSepToPosix(`.${path.posix.sep}${pathe.join(dirname, `${basename}${extname}`)}`); + + return { + ...declaration, + relativePath, + extname: { + origin: extname, + render: renderExtname, + }, + }; + }); + + return renderDatas; +} diff --git a/src/templates/templates/declarationFileTemplate.ts b/src/templates/templates/declarationFileTemplate.ts index 1c63d9b..25b444d 100644 --- a/src/templates/templates/declarationFileTemplate.ts +++ b/src/templates/templates/declarationFileTemplate.ts @@ -1,6 +1,6 @@ export const declarationFileTemplate = ` <%- it.declarations.forEach((declaration) => { -%> -import <%-= it.options.quote %><%= declaration.relativePath %><%= it.options.quote -%> +import <%-= it.options.quote %><%= declaration.relativePath %><%= declaration.extname.render %><%= it.options.quote -%> <%- }) %> `;