diff --git a/src/core/args.ts b/src/core/args.ts index 8311c58..9dbda2a 100644 --- a/src/core/args.ts +++ b/src/core/args.ts @@ -4,7 +4,7 @@ export enum ArgsFlags { RETAIN_NON_ASCII_CHARACTERS = 'retain-non-ascii-characters', } -class ArgsParser { +export class ArgsParser { #command: string = ''; #argv: string[] = []; @@ -13,7 +13,26 @@ class ArgsParser { } getArg(argName: string) { - return process.argv.slice(2).find((x: string) => x.startsWith('--') && x.slice(2) === argName); + const argumentsList = process.argv.slice(2); + + for (let i = 0; i < argumentsList.length; i++) { + const argument = argumentsList[i]; + + if (!argument.startsWith(`--${argName}`)) { + continue; + } + + if (argument === `--${argName}`) { + const nextArgument = argumentsList[i + 1]; + return nextArgument?.startsWith('--') ? undefined : nextArgument; + } + + if (argument.startsWith(`--${argName}=`)) { + return argument.slice(argName.length + 3); + } + } + + return undefined; } getCommand() { diff --git a/test/core/args/args.test.js b/test/core/args/args.test.js new file mode 100644 index 0000000..16822b6 --- /dev/null +++ b/test/core/args/args.test.js @@ -0,0 +1,64 @@ +import { strictEqual } from 'node:assert'; +import { resolve } from 'node:path'; +import { test, suite } from 'node:test'; + +import { ArgsParser } from '#dist/core/args.js'; + +suite('ArgsParser.getArg', () => { + test('returns undefined when project argument is omitted', () => { + const originalArgv = process.argv; + process.argv = ['node', 'cli', 'build']; + + try { + strictEqual(new ArgsParser().getArg('project'), undefined); + } finally { + process.argv = originalArgv; + } + }); + + test('reads a relative project argument from a separate value', () => { + const originalArgv = process.argv; + process.argv = ['node', 'cli', 'build', '--project', 'configs/custom.tsconfig.json']; + + try { + strictEqual(new ArgsParser().getArg('project'), 'configs/custom.tsconfig.json'); + } finally { + process.argv = originalArgv; + } + }); + + test('reads an absolute project argument from a separate value', () => { + const originalArgv = process.argv; + const absolutePath = resolve('test', 'core', 'config', 'fixture', 'configs', 'custom.tsconfig.json'); + process.argv = ['node', 'cli', 'build', '--project', absolutePath]; + + try { + strictEqual(new ArgsParser().getArg('project'), absolutePath); + } finally { + process.argv = originalArgv; + } + }); + + test('reads a relative project argument from equals syntax', () => { + const originalArgv = process.argv; + process.argv = ['node', 'cli', 'build', '--project=configs/custom.tsconfig.json']; + + try { + strictEqual(new ArgsParser().getArg('project'), 'configs/custom.tsconfig.json'); + } finally { + process.argv = originalArgv; + } + }); + + test('reads an absolute project argument from equals syntax', () => { + const originalArgv = process.argv; + const absolutePath = resolve('test', 'core', 'config', 'fixture', 'configs', 'custom.tsconfig.json'); + process.argv = ['node', 'cli', 'build', `--project=${absolutePath}`]; + + try { + strictEqual(new ArgsParser().getArg('project'), absolutePath); + } finally { + process.argv = originalArgv; + } + }); +}); diff --git a/test/core/config/config.test.js b/test/core/config/config.test.js index 2932f67..c78a8cc 100644 --- a/test/core/config/config.test.js +++ b/test/core/config/config.test.js @@ -1,4 +1,4 @@ -import { join, normalize } from 'node:path'; +import { join, normalize, resolve } from 'node:path'; import { test, suite } from 'node:test'; import { getTSConfig } from '#dist/core/config.js'; @@ -18,4 +18,24 @@ suite('getTSConfig', () => { t.assert.ok(normalizedFileNames.includes(fileName)); }); }); + + test('resolves a relative custom project path passed with --project semantics', (t) => { + const fixturePath = join(import.meta.dirname, 'fixture'); + const configuration = getTSConfig(fixturePath, 'configs/custom.tsconfig.json'); + const normalizedFileNames = configuration.fileNames.map(normalize); + + t.assert.deepEqual(normalizedFileNames, [ + join(fixturePath, 'types', 'global.d.ts'), + ].map(normalize)); + }); + + test('resolves an absolute custom project path passed with --project semantics', (t) => { + const fixturePath = join(import.meta.dirname, 'fixture'); + const configuration = getTSConfig(fixturePath, resolve(fixturePath, 'configs', 'custom.tsconfig.json')); + const normalizedFileNames = configuration.fileNames.map(normalize); + + t.assert.deepEqual(normalizedFileNames, [ + join(fixturePath, 'types', 'global.d.ts'), + ].map(normalize)); + }); }); diff --git a/test/core/config/fixture/configs/custom.tsconfig.json b/test/core/config/fixture/configs/custom.tsconfig.json new file mode 100644 index 0000000..1a346b3 --- /dev/null +++ b/test/core/config/fixture/configs/custom.tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "Preserve" + }, + "include": [ + "../types/global.d.ts" + ] +}