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

changed to have exclude options take precedence over comment directives #156

Merged
merged 2 commits into from
Aug 19, 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
46 changes: 11 additions & 35 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 @@ -88,14 +85,16 @@ export async function bundling(buildOptions: TCommandBuildOptions, bundleOption:
project,
filePaths,
keyword: CE_INLINE_COMMENT_KEYWORD.FILE_DECLARATION_KEYWORD,
}).filter((declaration) => {
const sourceFile = project.getSourceFile(declaration.filePath);
if (sourceFile == null) {
return false;
}
})
.filter((declaration) => !exclude.isExclude(declaration.filePath))
.filter((declaration) => {
const sourceFile = project.getSourceFile(declaration.filePath);
if (sourceFile == null) {
return false;
}

return isDeclarationFile(sourceFile);
});
return isDeclarationFile(sourceFile);
});

const filenames = filePaths
.filter((filename) => include.isInclude(filename))
Expand Down Expand Up @@ -194,30 +193,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',
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',
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')}`,
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')}`,
extname: { origin: '.ts', render: '.js' },
},
]);
});
});
38 changes: 38 additions & 0 deletions src/templates/modules/getInlineDeclarationRenderData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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)))
: replaceSepToPosix(`.${path.posix.sep}${pathe.join(dirname, basename)}`);

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 -%>

<%- }) %>
`;
Loading