Skip to content

Commit

Permalink
fix(#153): fixed the issue with @ctix-declaration statement generation
Browse files Browse the repository at this point in the history
  - fixed the issue with @ctix-declaration statement generation
    - fixed the problem where the fileExt option was not working
  • Loading branch information
imjuni committed Aug 19, 2024
1 parent c2d7e8f commit 51f4a6f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 29 deletions.
30 changes: 2 additions & 28 deletions src/modules/commands/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down Expand Up @@ -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),
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof getInlineCommentedFiles> = [
{
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<typeof getInlineCommentedFiles> = [
{
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' },
},
]);
});
});
40 changes: 40 additions & 0 deletions src/templates/modules/getInlineDeclarationRenderData.ts
Original file line number Diff line number Diff line change
@@ -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<typeof getInlineCommentedFiles>,
options: SetOptional<Pick<TBundleOptions, 'output' | 'fileExt'>, '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;
}
2 changes: 1 addition & 1 deletion src/templates/templates/declarationFileTemplate.ts
Original file line number Diff line number Diff line change
@@ -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 -%>
<%- }) %>
`;

0 comments on commit 51f4a6f

Please sign in to comment.