Skip to content

Commit

Permalink
Support config files, first stab at #4
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed Mar 16, 2024
1 parent bc877e6 commit 2b5273e
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
#!/usr/bin/env node

import path from "path";
import { globSync } from "glob";
import env from "./env/node.js";
import run from "./run.js";

const CONFIG_GLOB = "{,_,.}htest.{json,config.json,config.js}";
let config;


export async function getConfig (glob = CONFIG_GLOB) {
if (config) {
return config;
}

let paths = globSync(glob);

if (paths.length > 0) {
let configPath = "./" + paths[0];
let importParams;
configPath = path.join(process.cwd(), configPath);
// return import(p).then(m => m.default ?? m);
if (configPath.endsWith(".json")) {
importParams = {assert: { type: "json" }, with: { type: "json" }};
}

config = await import(configPath, importParams).then(m => config = m.default);

return config;
}
}

/**
* Run tests via a CLI command
* First argument is the location to look for tests (defaults to the current directory)
* Second argument is the test path (optional)
* @param {object} [options] Same as `run()` options, but command line arguments take precedence
*/
export default async function cli (options = {}) {
let config = await getConfig();
if (config) {
console.log("config", config);
options = {...config, ...options};
}

let argv = process.argv.slice(2);

let location = argv[0];
Expand Down

0 comments on commit 2b5273e

Please sign in to comment.