From aa4df2ae2c1f4c139a74ef64f97822ded70b3503 Mon Sep 17 00:00:00 2001 From: ByungJoon Lee Date: Wed, 15 Nov 2023 00:29:32 +0900 Subject: [PATCH] Added: added removing programming interface - [Add] Added removing programming interface - [Change] changed mermaid graph from `TD` to `LR` in README.md --- README.md | 2 +- src/cli/commands/removeCommand.ts | 65 ++--------------- .../__tests__/inline.generation.style.test.ts | 4 +- .../__tests__/namespace.comment.test.ts | 2 +- src/comments/getInlineExclude.ts | 2 +- src/comments/getInlineStyle.ts | 2 +- src/compilers/__tests__/symbol.table.test.ts | 2 +- src/configs/transforms/createRemoveOptions.ts | 25 +++++++ src/modules/commands/removing.ts | 71 +++++++++++++++++++ src/templates/__tests__/select.style.test.ts | 2 +- 10 files changed, 108 insertions(+), 69 deletions(-) create mode 100644 src/configs/transforms/createRemoveOptions.ts create mode 100644 src/modules/commands/removing.ts diff --git a/README.md b/README.md index c481b04..ef08fe0 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ npx ctix build The graph below outlines the behavioral flow of `ctix`. ```mermaid -flowchart TD +flowchart LR START(start) --> |execute cli|ctix ctix --> |TypeScript Compiler API| INP01[Source Code files] ctix --> |TypeScript Compiler API| INP02["tsconfig.json"] diff --git a/src/cli/commands/removeCommand.ts b/src/cli/commands/removeCommand.ts index d5b9cff..730d40a 100644 --- a/src/cli/commands/removeCommand.ts +++ b/src/cli/commands/removeCommand.ts @@ -1,78 +1,21 @@ -import { askRemoveFiles } from '#/cli/questions/askRemoveFiles'; import { ProgressBar } from '#/cli/ux/ProgressBar'; import { Reasoner } from '#/cli/ux/Reasoner'; import { Spinner } from '#/cli/ux/Spinner'; import type { TCommandBuildArgvOptions } from '#/configs/interfaces/TCommandBuildArgvOptions'; import type { TCommandRemoveOptions } from '#/configs/interfaces/TCommandRemoveOptions'; import { createBuildOptions } from '#/configs/transforms/createBuildOptions'; -import { getRemoveFileGlobPattern } from '#/modules/file/getRemoveFileGlobPattern'; -import { unlinks } from '#/modules/file/unlinks'; -import { IncludeContainer } from '#/modules/scope/IncludeContainer'; -import chalk from 'chalk'; +import { createRemoveOptions } from '#/configs/transforms/createRemoveOptions'; +import { removing } from '#/modules/commands/removing'; import consola from 'consola'; -import path from 'node:path'; import type yargs from 'yargs'; async function removeCommandCode( argv: yargs.ArgumentsCamelCase, ) { - Spinner.it.start(`'index.ts' file remove start`); - const options = await createBuildOptions(argv); - const patterns = await getRemoveFileGlobPattern(argv, options.options); - - const include = new IncludeContainer({ - config: { include: patterns.map((projectDir) => projectDir.pattern) }, - }); - const filePaths = include.files(); - - if (argv.forceYes) { - Spinner.it.succeed('enable force-yes, file removing without question'); - Spinner.it.stop(); - - ProgressBar.it.start(filePaths.length); - - await unlinks(filePaths, () => { - ProgressBar.it.increment(); - }); - - ProgressBar.it.stop(); - - await filePaths.reduce(async (prevHandle: Promise, filePath: string) => { - const handle = async () => { - Spinner.it.succeed( - `${chalk.redBright('removed:')} ${path.relative(process.cwd(), filePath)}`, - ); - }; - - await prevHandle; - return handle(); - }, Promise.resolve()); - - return; - } - - Spinner.it.stop(); - ProgressBar.it.start(filePaths.length); - - const indexFiles = await askRemoveFiles(filePaths); - - await unlinks(indexFiles, () => { - ProgressBar.it.increment(); - }); - - ProgressBar.it.stop(); - - await filePaths.reduce(async (prevHandle: Promise, filePath: string) => { - const handle = async () => { - Spinner.it.succeed( - `${chalk.redBright('removed:')} ${path.relative(process.cwd(), filePath)}`, - ); - }; + const removeOptions = createRemoveOptions(argv); - await prevHandle; - return handle(); - }, Promise.resolve()); + await removing({ ...options, ...removeOptions }); } export async function removeCommand( diff --git a/src/comments/__tests__/inline.generation.style.test.ts b/src/comments/__tests__/inline.generation.style.test.ts index 10e5893..87be910 100644 --- a/src/comments/__tests__/inline.generation.style.test.ts +++ b/src/comments/__tests__/inline.generation.style.test.ts @@ -1,11 +1,11 @@ +import { CE_INLINE_COMMENT_KEYWORD } from '#/comments/const-enum/CE_INLINE_COMMENT_KEYWORD'; import { getInlineStyle } from '#/comments/getInlineStyle'; +import { getSourceFileComments } from '#/comments/getSourceFileComments'; import { describe, expect, it, jest } from '@jest/globals'; import * as cp from 'comment-parser'; import { randomUUID } from 'node:crypto'; import path from 'node:path'; import * as tsm from 'ts-morph'; -import { CE_INLINE_COMMENT_KEYWORD } from '../const-enum/CE_INLINE_COMMENT_KEYWORD'; -import { getSourceFileComments } from '../getSourceFileComments'; const tsconfigPath = path.join(process.cwd(), 'example', 'tsconfig.example.json'); const context = { diff --git a/src/comments/__tests__/namespace.comment.test.ts b/src/comments/__tests__/namespace.comment.test.ts index 4bfdf8e..3693bcc 100644 --- a/src/comments/__tests__/namespace.comment.test.ts +++ b/src/comments/__tests__/namespace.comment.test.ts @@ -1,6 +1,6 @@ import { getCommentNamespace } from '#/comments/getCommentNamespace'; +import { getCommentNamespaces } from '#/comments/getCommentNamespaces'; import { describe, expect, it } from '@jest/globals'; -import { getCommentNamespaces } from '../getCommentNamespaces'; describe('getCommentNamespace', () => { it('ends-with ,', () => { diff --git a/src/comments/getInlineExclude.ts b/src/comments/getInlineExclude.ts index 1396b31..bba715b 100644 --- a/src/comments/getInlineExclude.ts +++ b/src/comments/getInlineExclude.ts @@ -1,9 +1,9 @@ +import { getCommentNamespaces } from '#/comments/getCommentNamespaces'; import { getJsDocComment } from '#/comments/getJsDocComment'; import { getJsDocTag } from '#/comments/getJsDocTag'; import type { IInlineExcludeInfo } from '#/comments/interfaces/IInlineExcludeInfo'; import type { IStatementComments } from '#/comments/interfaces/IStatementComments'; import { parse } from 'comment-parser'; -import { getCommentNamespaces } from './getCommentNamespaces'; export function getInlineExclude(params: { comment: IStatementComments; diff --git a/src/comments/getInlineStyle.ts b/src/comments/getInlineStyle.ts index 2873a2c..a242dcf 100644 --- a/src/comments/getInlineStyle.ts +++ b/src/comments/getInlineStyle.ts @@ -1,10 +1,10 @@ +import { getCommentNamespaces } from '#/comments/getCommentNamespaces'; import { getJsDocComment } from '#/comments/getJsDocComment'; import { getJsDocTag } from '#/comments/getJsDocTag'; import type { IInlineGenerationStyleInfo } from '#/comments/interfaces/IInlineGenerationStyleInfo'; import type { IStatementComments } from '#/comments/interfaces/IStatementComments'; import { getGenerationStyle } from '#/templates/modules/getGenerationStyle'; import { parse } from 'comment-parser'; -import { getCommentNamespaces } from './getCommentNamespaces'; export function getInlineStyle(params: { comment: IStatementComments; diff --git a/src/compilers/__tests__/symbol.table.test.ts b/src/compilers/__tests__/symbol.table.test.ts index 05becfa..97f3f98 100644 --- a/src/compilers/__tests__/symbol.table.test.ts +++ b/src/compilers/__tests__/symbol.table.test.ts @@ -1,8 +1,8 @@ +import { SymbolTable } from '#/compilers/SymbolTable'; import { describe, expect, it } from '@jest/globals'; import { randomUUID } from 'node:crypto'; import path from 'node:path'; import * as tsm from 'ts-morph'; -import { SymbolTable } from '../SymbolTable'; const tsconfigPath = path.join(process.cwd(), 'example', 'tsconfig.example.json'); const context = { diff --git a/src/configs/transforms/createRemoveOptions.ts b/src/configs/transforms/createRemoveOptions.ts new file mode 100644 index 0000000..90103b7 --- /dev/null +++ b/src/configs/transforms/createRemoveOptions.ts @@ -0,0 +1,25 @@ +import { CE_CTIX_COMMAND } from '#/configs/const-enum/CE_CTIX_COMMAND'; +import type { TBundleOptions } from '#/configs/interfaces/TBundleOptions'; +import type { TCommandBuildArgvOptions } from '#/configs/interfaces/TCommandBuildArgvOptions'; +import type { TCommandRemoveOptions } from '#/configs/interfaces/TCommandRemoveOptions'; +import type { TCreateOptions } from '#/configs/interfaces/TCreateOptions'; +import type { ArgumentsCamelCase } from 'yargs'; + +export function createRemoveOptions( + argv: ArgumentsCamelCase & { + options?: (TCreateOptions | TBundleOptions)[]; + }, +): TCommandRemoveOptions { + const options: TCommandRemoveOptions = { + $kind: CE_CTIX_COMMAND.REMOVE_COMMAND, + config: argv.config, + spinnerStream: argv.spinnerStream, + progressStream: argv.progressStream, + reasonerStream: argv.reasonerStream, + removeBackup: argv.removeBackup, + exportFilename: argv.exportFilename, + forceYes: argv.forceYes, + }; + + return options; +} diff --git a/src/modules/commands/removing.ts b/src/modules/commands/removing.ts new file mode 100644 index 0000000..3d720ae --- /dev/null +++ b/src/modules/commands/removing.ts @@ -0,0 +1,71 @@ +import { askRemoveFiles } from '#/cli/questions/askRemoveFiles'; +import { ProgressBar } from '#/cli/ux/ProgressBar'; +import { Spinner } from '#/cli/ux/Spinner'; +import type { TCommandBuildOptions } from '#/configs/interfaces/TCommandBuildOptions'; +import type { TCommandRemoveOptions } from '#/configs/interfaces/TCommandRemoveOptions'; +import { getRemoveFileGlobPattern } from '#/modules/file/getRemoveFileGlobPattern'; +import { unlinks } from '#/modules/file/unlinks'; +import { IncludeContainer } from '#/modules/scope/IncludeContainer'; +import chalk from 'chalk'; +import path from 'node:path'; + +export async function removing( + options: TCommandRemoveOptions & Omit, +) { + Spinner.it.start(`'index.ts' file remove start`); + + const patterns = await getRemoveFileGlobPattern(options, options.options); + + const include = new IncludeContainer({ + config: { include: patterns.map((projectDir) => projectDir.pattern) }, + }); + const filePaths = include.files(); + + if (options.forceYes) { + Spinner.it.succeed('enable force-yes, file removing without question'); + Spinner.it.stop(); + + ProgressBar.it.start(filePaths.length); + + await unlinks(filePaths, () => { + ProgressBar.it.increment(); + }); + + ProgressBar.it.stop(); + + await filePaths.reduce(async (prevHandle: Promise, filePath: string) => { + const handle = async () => { + Spinner.it.succeed( + `${chalk.redBright('removed:')} ${path.relative(process.cwd(), filePath)}`, + ); + }; + + await prevHandle; + return handle(); + }, Promise.resolve()); + + return; + } + + Spinner.it.stop(); + ProgressBar.it.start(filePaths.length); + + const indexFiles = await askRemoveFiles(filePaths); + + await unlinks(indexFiles, () => { + ProgressBar.it.increment(); + }); + + ProgressBar.it.stop(); + + await filePaths.reduce(async (prevHandle: Promise, filePath: string) => { + const handle = async () => { + Spinner.it.succeed( + `${chalk.redBright('removed:')} ${path.relative(process.cwd(), filePath)}`, + ); + }; + + await prevHandle; + return handle(); + }, Promise.resolve()); +} diff --git a/src/templates/__tests__/select.style.test.ts b/src/templates/__tests__/select.style.test.ts index 0d386e7..f716258 100644 --- a/src/templates/__tests__/select.style.test.ts +++ b/src/templates/__tests__/select.style.test.ts @@ -1,6 +1,7 @@ import type { IStatementComments } from '#/comments/interfaces/IStatementComments'; import { CE_GENERATION_STYLE } from '#/configs/const-enum/CE_GENERATION_STYLE'; import { filenamify } from '#/modules/path/filenamify'; +import { CE_AUTO_RENDER_CASE } from '#/templates/const-enum/CE_AUTO_RENDER_CASE'; import type { IIndexRenderData } from '#/templates/interfaces/IIndexRenderData'; import { getSelectStyle } from '#/templates/modules/getSelectStyle'; import { describe, expect, it } from '@jest/globals'; @@ -8,7 +9,6 @@ import copy from 'fast-copy'; import { randomUUID } from 'node:crypto'; import path from 'node:path'; import * as tsm from 'ts-morph'; -import { CE_AUTO_RENDER_CASE } from '../const-enum/CE_AUTO_RENDER_CASE'; const uuid = randomUUID(); const filename = `${uuid}.ts`;