From 2f7004568da6bc4dd3c4a715c7b08569ded42f67 Mon Sep 17 00:00:00 2001 From: Snowflyt Date: Tue, 2 Jul 2024 18:39:23 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Support=20customize=20confi?= =?UTF-8?q?guration=20file=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/cli.ts | 11 +++++++++-- src/config-helpers.ts | 43 +++++++++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index c32173d..c405eab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typroof", - "version": "0.2.9-dev", + "version": "0.2.9", "private": true, "description": "🚀 Revolutionize your TS type testing with a fast, smooth, and flexible WYSIWYG experience!", "keywords": [ diff --git a/src/cli.ts b/src/cli.ts index 33045e7..e54ffad 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,12 +20,15 @@ const cli = meow( Options --files -f Glob of files to test [Default: '/path/proof/**/*.{ts,tsx}' or '/**/*.proof.{ts,tsx}'] + --config -c Path to a typroof config file [Default: '/path/typroof.config.{ts,mts,cts,js,mjs,cjs}'] Examples $ typroof /path/to/project $ typroof --files /test/some/folder/*.ts --files /test/other/folder/*.tsx + $ typroof --config ../typroof.config.ts + $ typroof ❯ proof/string-utils.ts (2) @@ -46,19 +49,23 @@ const cli = meow( shortFlag: 'f', isMultiple: true, }, + config: { + type: 'string', + shortFlag: 'c', + }, }, importMeta: import.meta, }, ); const cwd = cli.input.length > 0 ? cli.input[0]! : process.cwd(); -const { files: testFiles } = cli.flags; +const { config: configPath, files: testFiles } = cli.flags; registerBuiltinAnalyzers(); const project = createTyproofProject({ tsConfigFilePath: path.join(cwd, 'tsconfig.json'), - ...(await loadConfig({ cwd })), + ...(await loadConfig({ cwd, configPath })), ...(testFiles && testFiles.length > 0 && { testFiles }), }); diff --git a/src/config-helpers.ts b/src/config-helpers.ts index a7a163b..4065556 100644 --- a/src/config-helpers.ts +++ b/src/config-helpers.ts @@ -10,28 +10,34 @@ import { omit } from './utils/object'; import type { Validator } from './assertions'; import type { Config } from './config'; -export const loadConfig = async ({ cwd = process.cwd() }: { cwd?: string } = {}): Promise< - Omit -> => { - const extensions = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs']; - const configBaseName = 'typroof.config'; +const CONFIG_BASE_NAME = 'typroof.config'; +const SUPPORTED_EXTENSIONS = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs']; + +export const loadConfig = async ({ + configPath, + cwd = process.cwd(), +}: { cwd?: string; configPath?: string } = {}): Promise> => { + const candidatePaths = + configPath ? + [path.join(cwd, configPath)] + : SUPPORTED_EXTENSIONS.map((ext) => path.join(cwd, `${CONFIG_BASE_NAME}${ext}`)); const tsConfig = getTsconfig(path.join(cwd, 'tsconfig.json')) ?? {}; - for (const ext of extensions) { - const filePath = path.join(cwd, `${configBaseName}${ext}`); - if (fs.existsSync(filePath)) { + for (const configPath of candidatePaths) { + const ext = path.extname(configPath); + if (fs.existsSync(configPath)) { let config: Config; if (ext === '.ts' || ext === '.mts' || ext === '.cts') - config = await compileConfig({ filePath, tsConfig }); + config = await compileConfig({ configPath, tsConfig }); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - else if (ext === '.mjs') config = (await import(filePath)).default; - else if (ext === '.cjs') config = require(filePath); + else if (ext === '.mjs') config = (await import(configPath)).default; + else if (ext === '.cjs') config = require(configPath); else try { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - config = (await import(filePath)).default; + config = (await import(configPath)).default; } catch (e) { - config = require(filePath); + config = require(configPath); } if (config.plugins) { @@ -52,14 +58,14 @@ export const loadConfig = async ({ cwd = process.cwd() }: { cwd?: string } = {}) }; export const compileConfig = async ({ - filePath, + configPath, tsConfig, }: { - filePath: string; + configPath: string; tsConfig: object; }): Promise => { const result = await esbuild.build({ - entryPoints: [filePath], + entryPoints: [configPath], bundle: true, platform: 'node', format: 'esm', @@ -70,8 +76,9 @@ export const compileConfig = async ({ const { outputFiles } = result; if (outputFiles && outputFiles.length > 0) { const code = outputFiles[0]!.text; - const moduleUrl = `data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`; - const ext = path.extname(filePath); + const encodedCode = encodeURIComponent(code); + const moduleUrl = `data:text/javascript;charset=utf-8,${encodedCode}`; + const ext = path.extname(configPath); if (ext === '.mts') // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return (await import(moduleUrl)).default;