-
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.
* chore: update git ignore * tools: initial convert to ts * tools: migrate ts files * chore: add ts build to release pipeline * chore: remove old config file * chore: remove reference to old config types * chore: add changeset * chore: update changeset
- Loading branch information
Showing
13 changed files
with
305 additions
and
141 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,10 @@ | ||
--- | ||
"@heymp/scratchpad": minor | ||
--- | ||
|
||
⚠️ Change config types directory. | ||
|
||
```js | ||
export default /** @type {import('@heymp/scratchpad/src/config').Config} */ ({ | ||
}); | ||
``` |
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
node_modules | ||
|
||
src/**.js | ||
src/**.d.ts | ||
src/**.d.ts.map |
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 |
---|---|---|
@@ -1,90 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'node:fs'; | ||
import { join, dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { Command } from 'commander'; | ||
import { browser } from '../src/browser.js'; | ||
import { getConfig } from '../src/config.js'; | ||
import * as esbuild from 'esbuild'; | ||
|
||
// Get pkg info | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const pkg = JSON.parse(fs.readFileSync(join(__dirname, '../package.json'), 'utf8')); | ||
|
||
const program = new Command(); | ||
program | ||
.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.') | ||
.version(pkg.version) | ||
program.parse(process.argv); | ||
|
||
const file = program.args.at(0); | ||
const config = await getConfig(); | ||
const opts = { ...config, ...program.opts()}; | ||
|
||
class Processor extends EventTarget { | ||
constructor() { | ||
super(); | ||
this.url = opts['url']; | ||
this.headless = opts['headless']; | ||
this.devtools = opts['devtools']; | ||
this.tsWrite = opts['tsWrite']; | ||
this.playwrightConfig = opts['playwright']; | ||
this._func = ''; | ||
this.watcher(); | ||
browser(this); | ||
} | ||
|
||
get func() { | ||
return this._func; | ||
} | ||
|
||
set func(func) { | ||
this._func = func; | ||
this.dispatchEvent(new Event('change')); | ||
} | ||
|
||
watcher() { | ||
if (!fs.existsSync(file)) { | ||
throw new Error(`${file} file not found.`); | ||
} | ||
// execute it immediately then start watcher | ||
this.build(); | ||
fs.watchFile(file, { interval: 100 }, () => { | ||
this.build(); | ||
}); | ||
} | ||
|
||
async build() { | ||
try { | ||
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.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 Event('change')); | ||
} | ||
} | ||
|
||
new Processor(); | ||
import '../src/cli.js'; |
This file was deleted.
Oops, something went wrong.
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,97 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'node:fs'; | ||
import { join, dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { Command } from 'commander'; | ||
import { browser } from './browser.js'; | ||
import { getConfig } from './config.js'; | ||
import * as esbuild from 'esbuild'; | ||
|
||
// Get pkg info | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const pkg = JSON.parse(fs.readFileSync(join(__dirname, '../package.json'), 'utf8')); | ||
|
||
const program = new Command(); | ||
program | ||
.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.') | ||
.version(pkg.version) | ||
program.parse(process.argv); | ||
|
||
const file = program.args.at(0); | ||
const config = await getConfig(); | ||
const opts = { ...config, ...program.opts()}; | ||
|
||
export class Processor extends EventTarget { | ||
url = opts['url']; | ||
headless = opts['headless']; | ||
devtools = opts['devtools']; | ||
tsWrite = opts['tsWrite']; | ||
playwrightConfig = opts['playwright']; | ||
_func = ''; | ||
|
||
constructor() { | ||
super(); | ||
this.watcher(); | ||
browser(this); | ||
} | ||
|
||
get func() { | ||
return this._func; | ||
} | ||
|
||
set func(func) { | ||
this._func = func; | ||
this.dispatchEvent(new Event('change')); | ||
} | ||
|
||
watcher() { | ||
if (!file) { | ||
throw new Error(`${file} file not found.`); | ||
} | ||
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() { | ||
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.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 Event('change')); | ||
} | ||
} | ||
|
||
new Processor(); |
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
import { join } from 'node:path'; | ||
import { stat } from 'node:fs/promises'; | ||
import type { Page, BrowserContext, Browser } from 'playwright'; | ||
|
||
export type PlaywrightConfig = { | ||
page: Page, | ||
context: BrowserContext, | ||
browser: Browser, | ||
} | ||
|
||
export type Config = { | ||
headless?: boolean; | ||
devtools?: boolean; | ||
tsWrite?: boolean; | ||
url?: string; | ||
playwright?: (page: PlaywrightConfig) => Promise<void> | ||
} | ||
|
||
const exists = (path:string) => stat(path).then(() => true, () => false); | ||
|
||
async function importConfig(rootDir:string) { | ||
const path = join(rootDir, './scratchpad.config.js'); | ||
if (await exists(path)) { | ||
return import(path) | ||
.then(x => x.default) | ||
.catch(e => { | ||
console.error(e); | ||
return {}; | ||
}); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
export async function getConfig() { | ||
const rootDir = process.cwd(); | ||
return { | ||
...await importConfig(rootDir), | ||
} | ||
} | ||
|
Oops, something went wrong.