-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add run command * feat: add generate config command * chore: add changeset and update Readme * chore: update yarn lock * chore: add run command changeset * chore: update changeset
- Loading branch information
Showing
12 changed files
with
225 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
"@heymp/scratchpad": major | ||
--- | ||
|
||
Add `generate config` command [#38](https://github.com/heyMP/scratchpad/issues/38) | ||
|
||
```bash | ||
npx @heymp/scratchpad generate --help | ||
|
||
Usage: scratchpad generate [options] [command] | ||
|
||
Generate files from templates. | ||
|
||
Options: | ||
-h, --help display help for command | ||
|
||
Commands: | ||
config Generates an example config file. | ||
help [command] display help for command | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
"@heymp/scratchpad": major | ||
--- | ||
|
||
Add `run` command | ||
|
||
```bash | ||
npx @heymp/scratchpad run --help | ||
|
||
Usage: cli run [options] <file> | ||
|
||
Execute a file in a browser. | ||
|
||
Arguments: | ||
file file to execute in the browser. | ||
|
||
Options: | ||
--headless [boolean] specify running the browser in headless | ||
mode. | ||
--devtools [boolean] open browser devtools automatically. | ||
--ts-write [boolean] write the js output of the target ts file. | ||
--url [string] specify a specific url to execute the code | ||
in. | ||
-h, --help display help for command | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import fs from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import * as esbuild from 'esbuild'; | ||
|
||
export class ProcessorChangeEvent extends Event { | ||
constructor() { | ||
super('change'); | ||
} | ||
} | ||
|
||
export type ProcessorOpts = { | ||
headless?: boolean; | ||
devtools?: boolean; | ||
tsWrite?: boolean; | ||
url?: string; | ||
playwright?: any; | ||
file: string; | ||
} | ||
|
||
/** | ||
* Event Bus that monitors the target file for | ||
* changes and emits stringified javascript to | ||
* throught the 'change' event. | ||
* */ | ||
export class Processor extends EventTarget { | ||
_func = ''; | ||
|
||
constructor(public opts: ProcessorOpts) { | ||
super(); | ||
this.watcher(); | ||
} | ||
|
||
get func() { | ||
return this._func; | ||
} | ||
|
||
set func(func) { | ||
this._func = func; | ||
this.dispatchEvent(new ProcessorChangeEvent()); | ||
} | ||
|
||
watcher() { | ||
const file = this.opts.file; | ||
if (!fs.existsSync(file)) { | ||
throw new Error(`${file} file not found.`); | ||
} | ||
// execute it immediately then start watcher | ||
this.build(); | ||
fs.watchFile(join(file), { interval: 100 }, () => { | ||
this.build(); | ||
}); | ||
} | ||
|
||
async build() { | ||
const file = this.opts.file; | ||
try { | ||
if (!file) { | ||
throw new Error(`${file} file not found.`); | ||
} | ||
if (file.endsWith('.ts')) { | ||
const { outputFiles: [stdout]} = await esbuild.build({ | ||
entryPoints: [file], | ||
format: 'esm', | ||
bundle: true, | ||
write: false, | ||
}); | ||
this.func = new TextDecoder().decode(stdout.contents); | ||
if (this.opts.tsWrite) { | ||
fs.writeFile(join(process.cwd(), file.replace(/\.ts$/g, '.js')), this.func, 'utf8', () => {}); | ||
} | ||
} | ||
else { | ||
this.func = fs.readFileSync(file, 'utf8'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
start() { | ||
this.dispatchEvent(new ProcessorChangeEvent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Command } from '@commander-js/extra-typings'; | ||
import * as readline from 'node:readline/promises'; | ||
import { stdin, stdout } from 'node:process'; | ||
import { writeFile, readFile, stat } from 'node:fs/promises'; | ||
import { dirname, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
|
||
const configCommand = new Command('config') | ||
.description('Generates an example config file.') | ||
.action(async () => { | ||
const rl = readline.createInterface({ input: stdin, output: stdout }); | ||
const template = await readFile(join(__dirname, '../templates/scratchpad.config.js'), 'utf8'); | ||
const outpath = join(process.cwd(), 'scratchpad.config.js'); | ||
const confirmation = await rl.question(`Are you sure you want to create the following | ||
${outpath} [Y/n]: `); | ||
|
||
// if the user answers no then abort | ||
if (['n', 'N'].includes(confirmation)) { | ||
rl.close(); | ||
return; | ||
} | ||
|
||
// write the template file to the users directory | ||
writeFile(outpath, template, 'utf8'); | ||
rl.close(); | ||
}); | ||
|
||
export const generateCommand = new Command('generate') | ||
.description('Generate files from templates.') | ||
.addCommand(configCommand); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Command } from '@commander-js/extra-typings'; | ||
import { getConfig } from './config.js'; | ||
import { Processor } from './Processor.js'; | ||
import { browser } from './browser.js'; | ||
|
||
export const runCommand = new Command('run') | ||
.description('Execute a file in a browser.') | ||
.argument('<file>', 'file to execute in the browser.') | ||
.option('--headless [boolean]', 'specify running the browser in headless mode.') | ||
.option('--devtools [boolean]', 'open browser devtools automatically.') | ||
.option('--ts-write [boolean]', 'write the js output of the target ts file.') | ||
.option('--url [string]', 'specify a specific url to execute the code in.') | ||
.action(async (file, options, command) => { | ||
const config = await getConfig(); | ||
const opts = { ...config, ...options}; | ||
const processor = new Processor({ | ||
// type narrow the options | ||
headless: !!opts['headless'], | ||
devtools: !!opts['devtools'], | ||
tsWrite: !!opts['tsWrite'], | ||
url: typeof opts['url'] === 'string' ? opts['url'] : undefined, | ||
playwright: opts['playwright'], | ||
file, | ||
}); | ||
browser(processor); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export default /** @type {import('@heymp/scratchpad/src/config').Config} */ ({ | ||
}); |
Oops, something went wrong.