diff --git a/package-lock.json b/package-lock.json index 912e72d..6c5eb18 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,25 +1,25 @@ { "name": "@wshcmx/cli", - "version": "2.1.0", + "version": "3.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@wshcmx/cli", - "version": "2.1.0", + "version": "3.0.0", "license": "MIT", "workspaces": [ "./test/project" ], "dependencies": { - "@wshcmx/types": "^11.0.9", - "typescript": "^5.7.3" + "typescript": "^6.0.3" }, "bin": { "wshcmx": "dist/index.js" }, "devDependencies": { - "@types/node": "^22.0.0" + "@types/node": "^22.0.0", + "@wshcmx/types": "^12.1.0" }, "engines": { "node": ">= 14.17" @@ -40,9 +40,10 @@ "link": true }, "node_modules/@wshcmx/types": { - "version": "11.0.9", - "resolved": "https://registry.npmjs.org/@wshcmx/types/-/types-11.0.9.tgz", - "integrity": "sha512-cAIEpSZvXujdIV3kXV6xWrray5CSziR+Y6W5fh8+KN8oIBvx1ZeCEdkzcamcq91jVdcFR3SYl73BFxIsApC9mA==", + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/@wshcmx/types/-/types-12.1.0.tgz", + "integrity": "sha512-n1yGb/hyP742/JTV2KOjuqiNXvbetztmY31+jA6378x/uMRJfZGi0uehjVuSamM5sWFbgggfMoP4Bz4MIVXfeg==", + "dev": true, "license": "MIT" }, "node_modules/project": { @@ -50,9 +51,9 @@ "link": true }, "node_modules/typescript": { - "version": "5.8.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.3.tgz", - "integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", @@ -74,6 +75,12 @@ "@wshcmx/cli": "file:../..", "@wshcmx/types": "^11.0.0-rc.1" } + }, + "test/project/node_modules/@wshcmx/types": { + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/@wshcmx/types/-/types-11.1.1.tgz", + "integrity": "sha512-q50MTCBed/w4/R5pQ0BomH82uQ7xAII6UmvxwpDqqlPJ7k+Ej018qgdEExl5cAAyyQBdvQf3avX0TGNFVeu0sg==", + "license": "MIT" } } } diff --git a/package.json b/package.json index 2cf0aa9..a823c18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wshcmx/cli", - "version": "2.1.0", + "version": "3.0.0", "main": "dist/index.js", "type": "module", "repository": "github:wshcmx/cli", @@ -11,9 +11,9 @@ "build": "tsc", "start": "dist/index.js", "test": "npm run test:project && npm run test:snapshots", - "test:generate": "npx tsc && node --test --test-update-snapshots '**/*.test.js'", - "test:project": "npx tsc && npm test --script-shell=bash -w ./test/project", - "test:snapshots": "npx tsc && node --test --experimental-test-snapshots '**/*.test.js'" + "test:generate": "npx tsc && node --test --test-update-snapshots \"**/*.test.js\"", + "test:project": "npx tsc && npm test -w ./test/project", + "test:snapshots": "npx tsc && node --test --experimental-test-snapshots \"**/*.test.js\"" }, "workspaces": [ "./test/project" @@ -35,10 +35,10 @@ "license": "MIT", "description": "Набор CLI-инструментов предназначен для автоматизации сборки и отслеживания изменений в исходном коде с последующей транспиляцией в синтаксис WebSoftHCM.", "dependencies": { - "@wshcmx/types": "^11.0.9", - "typescript": "^5.7.3" + "typescript": "^6.0.3" }, "devDependencies": { - "@types/node": "^22.0.0" + "@types/node": "^22.0.0", + "@wshcmx/types": "^12.1.0" } } diff --git a/src/core/args.ts b/src/core/args.ts index 582853c..8311c58 100644 --- a/src/core/args.ts +++ b/src/core/args.ts @@ -13,7 +13,7 @@ class ArgsParser { } getArg(argName: string) { - return process.argv.slice(2).find(x => x.startsWith('--') && x.slice(2) === argName); + return process.argv.slice(2).find((x: string) => x.startsWith('--') && x.slice(2) === argName); } getCommand() { @@ -26,8 +26,8 @@ class ArgsParser { #parse() { this.#command = process.argv.slice(2)[0]; - this.#argv = process.argv.slice(3).filter(x => x.startsWith('--')).map(x => x.slice(2)); + this.#argv = process.argv.slice(3).filter((x: string) => x.startsWith('--')).map((x: string) => x.slice(2)); } } -export const args = new ArgsParser(); \ No newline at end of file +export const args = new ArgsParser(); diff --git a/src/core/build.ts b/src/core/build.ts index 3dbbf74..368de39 100644 --- a/src/core/build.ts +++ b/src/core/build.ts @@ -1,5 +1,5 @@ import fs from 'node:fs'; -import { dirname, normalize, relative, resolve } from 'node:path'; +import { dirname, extname, normalize, relative, resolve } from 'node:path'; import ts from 'typescript'; @@ -48,13 +48,23 @@ export function collectNonTypescriptFiles(configuration: ts.ParsedCommandLine) { } const { exclude, files, include } = configuration.raw; - const fileNames = configuration.fileNames.map(normalize); - const normalizedExclude = (exclude ?? []).map(normalize); - - return fs.globSync([...(include ?? []), ...(files ?? [])]) - .filter(x => !fileNames.includes(x)) - .filter(x => !normalizedExclude?.includes(x)) - .filter(x => fs.statSync(x).isFile()); + const configFilePath = typeof configuration.options.configFilePath === 'string' + ? configuration.options.configFilePath + : resolve('tsconfig.json'); + const configDirectoryPath = dirname(configFilePath); + const includePatterns = Array.isArray(include) ? include.filter((pattern): pattern is string => typeof pattern === 'string') : []; + const filePatterns = Array.isArray(files) ? files.filter((pattern): pattern is string => typeof pattern === 'string') : []; + const excludePatterns = Array.isArray(exclude) ? exclude.filter((pattern): pattern is string => typeof pattern === 'string') : []; + const fileNames = new Set(configuration.fileNames.map((fileName) => normalize(fileName))); + const normalizedExclude = new Set(excludePatterns.map((filePath) => normalize(resolve(configDirectoryPath, filePath)))); + const compilableExtensions = new Set(['.js', '.jsx', '.ts', '.tsx']); + + return fs.globSync([...includePatterns, ...filePatterns], { cwd: configDirectoryPath }) + .map((filePath: string) => normalize(resolve(configDirectoryPath, filePath))) + .filter((filePath: string) => !fileNames.has(filePath)) + .filter((filePath: string) => !normalizedExclude.has(filePath)) + .filter((filePath: string) => !compilableExtensions.has(extname(filePath))) + .filter((filePath: string) => fs.statSync(filePath).isFile()); } function reportDiagnostic(diagnostic: ts.Diagnostic) { @@ -145,7 +155,7 @@ export function watchNonTypescriptFiles(configuration: ts.ParsedCommandLine) { const { rootDir, outDir } = configuration.options; const entries = collectNonTypescriptFiles(configuration); - entries.forEach(x => { + entries.forEach((x: string) => { const filePath = rootDir ? relative(rootDir, x) : x; const outputFilePath = resolve(outDir!, filePath); @@ -167,10 +177,10 @@ export function buildNonTypescriptFiles(configuration: ts.ParsedCommandLine) { const { rootDir, outDir } = configuration.options; const entries = collectNonTypescriptFiles(configuration); - entries.forEach(x => { + entries.forEach((x: string) => { const filePath = rootDir ? relative(rootDir, x) : x; const outputFilePath = resolve(outDir!, filePath); fs.mkdirSync(dirname(outputFilePath), { recursive: true }); fs.writeFileSync(outputFilePath, fs.readFileSync(resolve(x), 'utf-8')); }); -} \ No newline at end of file +} diff --git a/src/core/config.ts b/src/core/config.ts index dba1f01..449cb61 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -1,3 +1,5 @@ +import { dirname } from 'node:path'; + import ts from 'typescript'; import { logger } from './logger.js'; @@ -17,7 +19,7 @@ export function getTSConfig(cwd: string, project: string = 'tsconfig.json'): ts. process.exit(1); } - const configFileContent = ts.parseJsonConfigFileContent(config, ts.sys, './'); + const configFileContent = ts.parseJsonConfigFileContent(config, ts.sys, dirname(tsconfigPath), undefined, tsconfigPath); if (configFileContent.errors.length > 0) { configFileContent.errors.forEach(x => { diff --git a/test/core/build/build.test.js b/test/core/build/build.test.js new file mode 100644 index 0000000..48da59a --- /dev/null +++ b/test/core/build/build.test.js @@ -0,0 +1,18 @@ +import { deepEqual } from 'node:assert'; +import { join, normalize } from 'node:path'; +import { test, suite } from 'node:test'; + +import { collectNonTypescriptFiles } from '#dist/core/build.js'; +import { getTSConfig } from '#dist/core/config.js'; + +suite('collectNonTypescriptFiles', () => { + test('collects only non-js/ts files relative to the tsconfig directory', () => { + const fixturePath = join(import.meta.dirname, 'fixture'); + const configuration = getTSConfig(fixturePath); + const nonTypescriptFiles = collectNonTypescriptFiles(configuration).map(normalize); + + deepEqual(nonTypescriptFiles, [ + join(fixturePath, 'src', 'template.xml'), + ].map(normalize)); + }); +}); diff --git a/test/core/build/fixture/lib/lib.ts b/test/core/build/fixture/lib/lib.ts new file mode 100644 index 0000000..da7d8c9 --- /dev/null +++ b/test/core/build/fixture/lib/lib.ts @@ -0,0 +1 @@ +export const lib = 'lib'; diff --git a/test/core/build/fixture/src/exclude_submodule.ts b/test/core/build/fixture/src/exclude_submodule.ts new file mode 100644 index 0000000..3373511 --- /dev/null +++ b/test/core/build/fixture/src/exclude_submodule.ts @@ -0,0 +1 @@ +export const ignored = 'ignored'; diff --git a/test/core/build/fixture/src/main.ts b/test/core/build/fixture/src/main.ts new file mode 100644 index 0000000..0d7b33e --- /dev/null +++ b/test/core/build/fixture/src/main.ts @@ -0,0 +1 @@ +export const main = 'main'; diff --git a/test/core/build/fixture/src/template.xml b/test/core/build/fixture/src/template.xml new file mode 100644 index 0000000..6e310dc --- /dev/null +++ b/test/core/build/fixture/src/template.xml @@ -0,0 +1 @@ + diff --git a/test/core/build/fixture/tsconfig.json b/test/core/build/fixture/tsconfig.json new file mode 100644 index 0000000..e7294a6 --- /dev/null +++ b/test/core/build/fixture/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "outDir": "dist" + }, + "include": [ + "src/**/*" + ], + "files": [ + "lib/lib.ts" + ], + "exclude": [ + "src/exclude_submodule.ts" + ] +} diff --git a/test/core/config/config.test.js b/test/core/config/config.test.js new file mode 100644 index 0000000..2932f67 --- /dev/null +++ b/test/core/config/config.test.js @@ -0,0 +1,21 @@ +import { join, normalize } from 'node:path'; +import { test, suite } from 'node:test'; + +import { getTSConfig } from '#dist/core/config.js'; + +suite('getTSConfig', () => { + test('resolves include and files relative to the tsconfig directory', (t) => { + const fixturePath = join(import.meta.dirname, 'fixture'); + const configuration = getTSConfig(fixturePath); + const normalizedFileNames = configuration.fileNames.map(normalize); + + const expectedFileNames = [ + join(fixturePath, 'src', 'main.ts'), + join(fixturePath, 'types', 'global.d.ts'), + ].map(normalize); + + expectedFileNames.forEach((fileName) => { + t.assert.ok(normalizedFileNames.includes(fileName)); + }); + }); +}); diff --git a/test/core/config/fixture/src/main.ts b/test/core/config/fixture/src/main.ts new file mode 100644 index 0000000..21b4de7 --- /dev/null +++ b/test/core/config/fixture/src/main.ts @@ -0,0 +1 @@ +export const main = true; diff --git a/test/core/config/fixture/tsconfig.json b/test/core/config/fixture/tsconfig.json new file mode 100644 index 0000000..85c7475 --- /dev/null +++ b/test/core/config/fixture/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "noEmit": true + }, + "include": [ + "src/**/*.ts" + ], + "files": [ + "types/global.d.ts" + ] +} diff --git a/test/core/config/fixture/types/global.d.ts b/test/core/config/fixture/types/global.d.ts new file mode 100644 index 0000000..f7eb531 --- /dev/null +++ b/test/core/config/fixture/types/global.d.ts @@ -0,0 +1,7 @@ +declare global { + interface FixtureGlobal { + ok: true; + } +} + +export {}; diff --git a/test/project/package.json b/test/project/package.json index 0eefe17..5edbe92 100644 --- a/test/project/package.json +++ b/test/project/package.json @@ -1,10 +1,10 @@ { "scripts": { - "build": "wshcmx build --include-non-ts-files --retain-imports-in-comments", - "start": "wshcmx watch", - "pretest": "rm -rf dist && npm install", + "build": "node ../../dist/index.js build --include-non-ts-files --retain-imports-as-comments", + "start": "node ../../dist/index.js watch", + "pretest": "node ./scripts/test-project.mjs clean && npm install", "test": "npm run build", - "posttest": "[[ $(find dist/* -name \"*.ts\" | wc -l) -gt 0 ]] && exit 1 || exit 0" + "posttest": "node ./scripts/test-project.mjs verify" }, "dependencies": { "@wshcmx/cli": "file:../..", diff --git a/test/project/scripts/test-project.mjs b/test/project/scripts/test-project.mjs new file mode 100644 index 0000000..39b1673 --- /dev/null +++ b/test/project/scripts/test-project.mjs @@ -0,0 +1,53 @@ +import { readdirSync, rmSync } from 'node:fs'; +import { join, resolve } from 'node:path'; + +const command = process.argv[2]; +const distPath = resolve('dist'); + +if (command === 'clean') { + rmSync(distPath, { recursive: true, force: true }); + process.exit(0); +} + +if (command === 'verify') { + const typeScriptFiles = []; + + const collectTypeScriptFiles = (directoryPath) => { + for (const entry of readdirSync(directoryPath, { withFileTypes: true })) { + const entryPath = join(directoryPath, entry.name); + + if (entry.isDirectory()) { + collectTypeScriptFiles(entryPath); + continue; + } + + if (entry.isFile() && entry.name.endsWith('.ts')) { + typeScriptFiles.push(entryPath); + } + } + }; + + try { + collectTypeScriptFiles(distPath); + } catch (error) { + if (error instanceof Error && 'code' in error && error.code === 'ENOENT') { + process.exit(0); + } + + throw error; + } + + if (typeScriptFiles.length > 0) { + console.error('TypeScript files found in dist:'); + + for (const filePath of typeScriptFiles) { + console.error(filePath); + } + + process.exit(1); + } + + process.exit(0); +} + +throw new Error(`Unknown command: ${command}`); diff --git a/test/project/tsconfig.json b/test/project/tsconfig.json index 124e754..a9082a6 100644 --- a/test/project/tsconfig.json +++ b/test/project/tsconfig.json @@ -1,10 +1,7 @@ { "compilerOptions": { - "target": "ES5", + "target": "esnext", "module": "Preserve", - "noLib": true, - "esModuleInterop": false, - "allowSyntheticDefaultImports": false, "outDir": "dist", "typeRoots": [ "./../../node_modules/@wshcmx/types/lib", diff --git a/tsconfig.json b/tsconfig.json index a4787d4..6ca5ac1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,9 @@ "strict": true, "allowJs": true, "skipLibCheck": true, - "outDir": "dist" + "types": ["node"], + "rootDir": "src", + "outDir": "./dist" }, "include": [ "src/**/*"