Skip to content
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
29 changes: 18 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand All @@ -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"
}
}
6 changes: 3 additions & 3 deletions src/core/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
export const args = new ArgsParser();
32 changes: 21 additions & 11 deletions src/core/build.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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'));
});
}
}
4 changes: 3 additions & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dirname } from 'node:path';

import ts from 'typescript';

import { logger } from './logger.js';
Expand All @@ -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 => {
Expand Down
18 changes: 18 additions & 0 deletions test/core/build/build.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
1 change: 1 addition & 0 deletions test/core/build/fixture/lib/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const lib = 'lib';
1 change: 1 addition & 0 deletions test/core/build/fixture/src/exclude_submodule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ignored = 'ignored';
1 change: 1 addition & 0 deletions test/core/build/fixture/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const main = 'main';
1 change: 1 addition & 0 deletions test/core/build/fixture/src/template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root />
17 changes: 17 additions & 0 deletions test/core/build/fixture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist"
},
"include": [
"src/**/*"
],
"files": [
"lib/lib.ts"
],
"exclude": [
"src/exclude_submodule.ts"
]
}
21 changes: 21 additions & 0 deletions test/core/config/config.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
});
1 change: 1 addition & 0 deletions test/core/config/fixture/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const main = true;
14 changes: 14 additions & 0 deletions test/core/config/fixture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmit": true
},
"include": [
"src/**/*.ts"
],
"files": [
"types/global.d.ts"
]
}
7 changes: 7 additions & 0 deletions test/core/config/fixture/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare global {
interface FixtureGlobal {
ok: true;
}
}

export {};
8 changes: 4 additions & 4 deletions test/project/package.json
Original file line number Diff line number Diff line change
@@ -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:../..",
Expand Down
53 changes: 53 additions & 0 deletions test/project/scripts/test-project.mjs
Original file line number Diff line number Diff line change
@@ -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}`);
5 changes: 1 addition & 4 deletions test/project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"strict": true,
"allowJs": true,
"skipLibCheck": true,
"outDir": "dist"
"types": ["node"],
"rootDir": "src",
"outDir": "./dist"
},
"include": [
"src/**/*"
Expand Down
Loading