diff --git a/package-lock.json b/package-lock.json index 67b00f1..8a84452 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@wshcmx/cli", - "version": "3.5.0", + "version": "3.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@wshcmx/cli", - "version": "3.5.0", + "version": "3.6.0", "license": "MIT", "workspaces": [ "./test/project" diff --git a/package.json b/package.json index b861be5..c442c04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wshcmx/cli", - "version": "3.5.0", + "version": "3.6.0", "main": "dist/index.js", "type": "module", "repository": "github:wshcmx/cli", diff --git a/src/commands/build.ts b/src/commands/build.ts deleted file mode 100644 index bf50590..0000000 --- a/src/commands/build.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { args } from '../core/args.js'; -import { buildTypescriptFiles } from '../core/build.js'; -import { getTSConfig } from '../core/config.js'; -import { logger } from '../core/logger.js'; - -export function build(cwd: string) { - logger.success(`🔨 ${new Date().toLocaleTimeString()} Project building started`); - const configuration = getTSConfig(cwd, args['project']); - - const result = buildTypescriptFiles(configuration); - - if (result?.emitSkipped) { - logger.error(`❌ ${new Date().toLocaleTimeString()} Project building failed`); - process.exit(1); - } - - logger.success(`✅ ${new Date().toLocaleTimeString()} Project building finished`); - process.exit(0); -} diff --git a/src/commands/help.ts b/src/commands/help.ts deleted file mode 100644 index 0e3db5f..0000000 --- a/src/commands/help.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { logger } from '../core/logger.js'; -import { commands } from './index.js'; - -export function help() { - logger.warning('\n@wshcmx/cli plugin - CLI для работы с WebSoft HCM'); - console.log(` -Команды для вызова: -\t${Array.from(commands).map(x => `"${logger.success(x[0])}" - ${x[1].description}`).join('\n\t')} -`); -} \ No newline at end of file diff --git a/src/commands/index.ts b/src/commands/index.ts deleted file mode 100644 index 1bc7fc9..0000000 --- a/src/commands/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { build } from './build.js'; -import { help } from './help.js'; -import { watch } from './watch.js'; - -export type Command = { - callback: Function, - description: string -}; - -export const commands = new Map([ - ['build', { callback: build, description: 'Building the project'}], - ['help', { callback: help, description: 'Plugin help description' }], - ['watch', { callback: watch, description: 'Watching for changes'}], -]); \ No newline at end of file diff --git a/src/commands/watch.ts b/src/commands/watch.ts deleted file mode 100644 index 75d8f3b..0000000 --- a/src/commands/watch.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { args } from '../core/args.js'; -import { watchTypescriptFiles } from '../core/build.js'; -import { getTSConfig } from '../core/config.js'; - -export function watch(cwd: string) { - const configuration = getTSConfig(cwd, args['project']); - watchTypescriptFiles(configuration); -} \ No newline at end of file diff --git a/src/core/args.ts b/src/core/args.ts deleted file mode 100644 index 984dba3..0000000 --- a/src/core/args.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { parseArgs } from "node:util"; - -class ArgsParser { - command: string = ''; - args: Record = {} ; - - constructor() { - const { values: args, positionals: [command] } = parseArgs({ - options: { - help: { type: 'boolean', short: 'h', default: false }, - version: { type: 'boolean', short: 'v', default: false }, - project: { type: 'string', short: 'p', default: 'tsconfig.json' }, - }, - allowPositionals: true - }); - this.command = command || ''; - this.args = args; - } -} - -export const { args, command } = new ArgsParser(); diff --git a/src/core/build.ts b/src/core/build.ts index 3007070..cc51761 100644 --- a/src/core/build.ts +++ b/src/core/build.ts @@ -5,7 +5,6 @@ import { removeExports } from '../transformers/remove_exports.js'; import { convertTemplateStrings } from '../transformers/template_strings.js'; import { transformNamespaces } from '../transformers/transform_namespaces.js'; import { noDeclarationInLoop } from '../checks/no-declaration-in-loop.js'; -import { logger } from './logger.js'; export function buildTypescriptFiles(configuration: ts.ParsedCommandLine) { const program = ts.createProgram(configuration.fileNames, configuration.options); @@ -23,9 +22,9 @@ export function buildTypescriptFiles(configuration: ts.ParsedCommandLine) { if (diagnostic.file) { const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!); const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); - logger.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); + console.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); } else { - logger.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); + console.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); } }); @@ -36,9 +35,9 @@ function reportDiagnostic(diagnostic: ts.Diagnostic) { if (diagnostic.file) { const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!); const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); - logger.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); + console.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); } else { - logger.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); + console.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); } } diff --git a/src/core/config.ts b/src/core/config.ts index 449cb61..fdb0c82 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -2,20 +2,18 @@ import { dirname } from 'node:path'; import ts from 'typescript'; -import { logger } from './logger.js'; - export function getTSConfig(cwd: string, project: string = 'tsconfig.json'): ts.ParsedCommandLine { const tsconfigPath = ts.findConfigFile(cwd, ts.sys.fileExists, project); if (!tsconfigPath) { - logger.error(`There is no any configuration files at "${cwd}". Execute npx tsc -init to create a new one.`); + console.error(`There is no any configuration files at "${cwd}". Execute npx tsc -init to create a new one.`); process.exit(1); } const { config, error } = ts.readConfigFile(tsconfigPath, ts.sys.readFile); if (error) { - logger.error(error.messageText.toString()); + console.error(error.messageText.toString()); process.exit(1); } @@ -23,7 +21,7 @@ export function getTSConfig(cwd: string, project: string = 'tsconfig.json'): ts. if (configFileContent.errors.length > 0) { configFileContent.errors.forEach(x => { - logger.error(x.messageText.toString()); + console.error(x.messageText.toString()); }); process.exit(1); diff --git a/src/core/logger.ts b/src/core/logger.ts deleted file mode 100644 index 86ecc3f..0000000 --- a/src/core/logger.ts +++ /dev/null @@ -1,34 +0,0 @@ -import util from 'node:util'; - -class Logger { - #styleText: Function = (_color: string, message: string) => message; - - constructor() { - this.#reimplementStyleText(); - } - - error(message: string) { - console.error(this.#styleText('red', message)); - } - - success(message: string) { - console.log(this.#styleText('greenBright', message)); - } - - warning(message: string) { - console.warn(this.#styleText('yellow', message)); - } - - #reimplementStyleText() { - const nodeVersionArray = process.versions.node.split('.'); - - if ( - Number(nodeVersionArray[0]) > 21 || - (Number(nodeVersionArray[0]) === 21 && Number(nodeVersionArray[1]) > 7) - ) { - this.#styleText = util.styleText; - } - } -} - -export const logger = new Logger(); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a026860..0774854 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,41 @@ #!/usr/bin/env node -import * as args from './core/args.js'; -import { commands } from './commands/index.js'; -import { help } from './commands/help.js'; +import { parseArgs } from "node:util"; -const cwd = process.cwd(); +import { buildTypescriptFiles, watchTypescriptFiles } from './core/build.js'; +import { getTSConfig } from './core/config.js'; -const command = commands.get(args.command); +export const { values: args, positionals: [command] } = parseArgs({ + options: { + help: { type: 'boolean', short: 'h', default: false }, + version: { type: 'boolean', short: 'v', default: false }, + project: { type: 'string', short: 'p', default: 'tsconfig.json' }, + }, + allowPositionals: true +}); -if (command) { - command.callback(cwd); +const CWD = process.cwd(); + +if (command === 'build') { + console.log(`🔨 ${new Date().toLocaleTimeString()} Project building started`); + const configuration = getTSConfig(CWD, args['project']); + + const result = buildTypescriptFiles(configuration); + + if (result?.emitSkipped) { + console.error(`❌ ${new Date().toLocaleTimeString()} Project building failed`); + process.exit(1); + } + + console.log(`✅ ${new Date().toLocaleTimeString()} Project building finished`); +} else if (command === 'watch') { + const configuration = getTSConfig(CWD, args['project']); + watchTypescriptFiles(configuration); } else { - help(); -} + console.warn('\n@wshcmx/cli plugin - CLI для работы с WebSoft HCM'); + console.log(` + Команды для вызова: + build - сборка проекта + + `); +} \ No newline at end of file diff --git a/test/core/args/args.test.js b/test/core/args/args.test.js deleted file mode 100644 index 09231ef..0000000 --- a/test/core/args/args.test.js +++ /dev/null @@ -1,99 +0,0 @@ -import { strictEqual } from 'node:assert'; -import { suite, test } from 'node:test'; - -const argsModuleUrl = new URL('../../../dist/core/args.js', import.meta.url); -let importCounter = 0; - -async function loadArgs(argv) { - const originalArgv = process.argv; - - process.argv = argv; - - try { - return await import(`${argsModuleUrl.href}?test=${importCounter += 1}`); - } finally { - process.argv = originalArgv; - } -} - -suite('args parser', () => { - test('uses defaults when no command or flags are provided', async () => { - const { args, command } = await loadArgs([ - 'node', - 'wshcmx', - ]); - - strictEqual(command, ''); - strictEqual(args.help, false); - strictEqual(args.version, false); - strictEqual(args.project, 'tsconfig.json'); - }); - - test('parses the positional command', async () => { - const { command } = await loadArgs([ - 'node', - 'wshcmx', - 'build', - ]); - - strictEqual(command, 'build'); - }); - - test('parses help from long and short flags', async () => { - const fromLong = await loadArgs([ - 'node', - 'wshcmx', - '--help', - ]); - const fromShort = await loadArgs([ - 'node', - 'wshcmx', - '-h', - ]); - - strictEqual(fromLong.args.help, true); - strictEqual(fromShort.args.help, true); - }); - - test('parses version from long and short flags', async () => { - const fromLong = await loadArgs([ - 'node', - 'wshcmx', - '--version', - ]); - const fromShort = await loadArgs([ - 'node', - 'wshcmx', - '-v', - ]); - - strictEqual(fromLong.args.version, true); - strictEqual(fromShort.args.version, true); - }); - - test('parses project from long and short options', async () => { - const fromLong = await loadArgs([ - 'node', - 'wshcmx', - '--project', - 'configs\\custom.tsconfig.json', - ]); - const fromShort = await loadArgs([ - 'node', - 'wshcmx', - '-p', - 'configs\\short.tsconfig.json', - ]); - - strictEqual(fromLong.args.project, 'configs\\custom.tsconfig.json'); - strictEqual(fromShort.args.project, 'configs\\short.tsconfig.json'); - }); - - test('parses all supported boolean build flags', async () => { - const { args } = await loadArgs([ - 'node', - 'wshcmx', - 'build', - ]); - }); -});