Skip to content

Commit 2b88c36

Browse files
authored
feat: make file optional (#75)
* feat: make file optional * chore: update changeset * chore: cleanup
1 parent 80c8090 commit 2b88c36

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

.changeset/thin-sloths-perform.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@heymp/scratchpad": patch
3+
---
4+
5+
File argument for run command optional
6+
7+
```bash
8+
npx @heymp/scratchpad@next run
9+
```

src/Processor.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class ProcessorChangeEvent extends Event {
1010
}
1111

1212
export type ProcessorOpts = Config & {
13-
file: string;
13+
file?: string;
1414
}
1515

1616
/**
@@ -36,14 +36,17 @@ export class Processor extends EventTarget {
3636
}
3737

3838
watcher() {
39-
const file = this.opts.file;
40-
4139
if (this.opts.login) {
4240
if (!fs.existsSync(join(process.cwd(), '.scratchpad', 'login.json'))) {
4341
throw new Error(`Session file not found.`);
4442
}
4543
}
4644

45+
const file = this.opts.file;
46+
if (!file) {
47+
return;
48+
}
49+
4750
if (!fs.existsSync(file)) {
4851
throw new Error(`${file} file not found.`);
4952
}
@@ -56,10 +59,10 @@ export class Processor extends EventTarget {
5659

5760
async build() {
5861
const file = this.opts.file;
62+
if (!file) {
63+
return;
64+
}
5965
try {
60-
if (!file) {
61-
throw new Error(`${file} file not found.`);
62-
}
6366
if (file.endsWith('.ts')) {
6467
const { outputFiles: [stdout]} = await build({
6568
entryPoints: [file],

src/runCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { browser } from './browser.js';
55

66
export const runCommand = new Command('run')
77
.description('Execute a file in a browser.')
8-
.argument('<file>', 'file to execute in the browser.')
8+
.argument('[file]', 'file to execute in the browser.')
99
.option('--headless [boolean]', 'specify running the browser in headless mode.')
1010
.option('--devtools [boolean]', 'open browser devtools automatically.')
1111
.option('--ts-write [boolean]', 'write the js output of the target ts file.')
1212
.option('--url [string]', 'specify a specific url to execute the code in.')
1313
.option('--login [boolean]', `use previously saved session from 'generate login' command`)
14-
.action(async (file, options, command) => {
14+
.action(async (file, options) => {
1515
const config = await getConfig();
1616
const opts = { ...config, ...options};
1717
const processor = new Processor({
@@ -23,7 +23,7 @@ export const runCommand = new Command('run')
2323
playwright: opts['playwright'],
2424
login: !!opts['login'],
2525
rerouteDir: opts['rerouteDir'],
26-
file,
26+
file
2727
});
2828
browser(processor);
2929
});

0 commit comments

Comments
 (0)