Skip to content

Commit

Permalink
✨ feat: Support customize configuration file path
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflyt committed Jul 2, 2024
1 parent dd97a7a commit 2f70045
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
11 changes: 9 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 }),
});

Expand Down
43 changes: 25 additions & 18 deletions src/config-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config, 'plugins'>
> => {
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<Omit<Config, 'plugins'>> => {
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) {
Expand All @@ -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<Config> => {
const result = await esbuild.build({
entryPoints: [filePath],
entryPoints: [configPath],
bundle: true,
platform: 'node',
format: 'esm',
Expand All @@ -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;
Expand Down

0 comments on commit 2f70045

Please sign in to comment.