Skip to content

Commit 2bcb56e

Browse files
authored
Feat/config (#35)
* add scratchpad.config.js config file * chore: add changeset * chore: typo
1 parent 0e6cb47 commit 2bcb56e

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

.changeset/cool-dingos-provide.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@heymp/scratchpad": minor
3+
---
4+
5+
Add `scratchpad.config.js` file as an alternative to specifying scratchpad options
6+
using the CLI flags.
7+
8+
```js
9+
export default ({
10+
devtools: true,
11+
headless: false,
12+
url: 'https://www.google.com'
13+
});
14+
```
15+
16+
NOTE: CLI flags will take precidence over config file options.

bin/cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { join, dirname } from 'node:path';
55
import { fileURLToPath } from 'node:url';
66
import { Command } from 'commander';
77
import { browser } from '../src/browser.js';
8+
import { getConfig } from '../src/config.js';
89
import * as esbuild from 'esbuild';
910

1011
// Get pkg info
@@ -22,7 +23,8 @@ program
2223
program.parse(process.argv);
2324

2425
const file = program.args.at(0);
25-
const opts = program.opts();
26+
const config = await getConfig();
27+
const opts = { ...config, ...program.opts()};
2628

2729
class Processor extends EventTarget {
2830
constructor() {

examples/scratchpad.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default ({
2+
devtools: true,
3+
headless: false,
4+
url: 'https://www.google.com'
5+
});

src/config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { join } from 'node:path';
2+
import { stat } from 'node:fs/promises';
3+
4+
const exists = (path) => stat(path).then(() => true, () => false);
5+
6+
async function importConfig(rootDir) {
7+
const path = join(rootDir, './scratchpad.config.js');
8+
if (await exists(path)) {
9+
return import(path)
10+
.then(x => x.default)
11+
.catch(e => {
12+
console.error(e);
13+
return {};
14+
});
15+
} else {
16+
return {};
17+
}
18+
}
19+
20+
export async function getConfig() {
21+
const rootDir = process.cwd();
22+
return {
23+
...await importConfig(rootDir),
24+
}
25+
}
26+

0 commit comments

Comments
 (0)