Skip to content

Commit

Permalink
feat: add jiti runtime support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 9, 2020
1 parent dc6a046 commit dc245ba
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
41 changes: 31 additions & 10 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
export const run = `
#!/bin/bash
set -e
import { resolve } from 'path'

for dir in packages/* distributions/* ; do
# echo "$dir"
pushd $dir > /dev/null
$@
popd > /dev/null
done
`
import consola from 'consola'
import { bold, gray } from 'chalk'
import { existsSync } from 'fs-extra'

interface RunCommandOptions {
file: string
args: string[]
}

export async function run({ file, args }: RunCommandOptions) {
const filepath = resolve(process.cwd(), file)
if (!(file.endsWith('.js') || file.endsWith('.ts'))) {
return consola.error(`${bold('siroc run')} should take a .js or .ts file.`)
}
if (!existsSync(filepath)) {
return consola.error(`${bold(filepath)} could not be found.`)
}
process.argv = [filepath, ...args]
try {
// eslint-disable-next-line
const jiti = require('jiti')(__filename)
jiti(filepath)
consola.success(`Ran ${bold(file)}\n`)
} catch (e) {
consola.error(
`Error running ${bold(file)}\n`,
gray(process.stderr.toString().trim())
)
}
}
11 changes: 11 additions & 0 deletions packages/cli/src/commands/workspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const run = `
#!/bin/bash
set -e
for dir in packages/* distributions/* ; do
# echo "$dir"
pushd $dir > /dev/null
$@
popd > /dev/null
done
`
7 changes: 6 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { version } from '../package.json'

import { build, BuildCommandOptions } from './commands/build'
import { changelog } from './commands/changelog'
import { run as runFile } from './commands/run'

const cli = cac('siroc')

Expand All @@ -20,7 +21,7 @@ const run = async <A extends (...args: any[]) => Promise<void>>(

cli
.command('build [...packages]', 'Bundle input files')
.option('--watch', 'Watch files in bundle and rebuild on changes', {
.option('-w, --watch', 'Watch files in bundle and rebuild on changes', {
default: false,
})
.option('--dev', 'Build development bundle (only CJS)', {
Expand All @@ -32,6 +33,10 @@ cli

cli.command('changelog', 'Generate changelog').action(() => run(changelog))

cli
.command('run <file> [...args]', 'Run Node script')
.action((file, args) => run(runFile, { file, args }))

cli.version(version)
cli.help()

Expand Down

0 comments on commit dc245ba

Please sign in to comment.