Skip to content

Commit

Permalink
feat: add inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaack committed Sep 1, 2024
1 parent 8ba3104 commit 8cee42f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ A simple, light-weight and modern task runner for general purpose.
## Features

- Promise-based tasks and built-in utilities.
- `<a href="https://github.com/shelljs/shelljs" target="_blank">`shelljs`</a>`-like commands
- `<a href="https://github.com/shelljs/shelljs" target="_blank">`shelljs `</a>`-like commands
- Easy to learn, stop spending hours for build tools.
- Small install size
- foy: [![install size](https://packagephobia.now.sh/badge?p=foy)](https://packagephobia.now.sh/result?p=foy)
Expand All @@ -54,7 +54,7 @@ yarn add -g foy # or npm i -g foy

## Write a Foyfile

You need to add a Foyfile.js(or Foyfile.ts with [ts-node](https://github.com/TypeStrong/ts-node) installed) to your project root.
You need to add a Foyfile.js(or Foyfile.ts with [tsx](https://github.com/privatenumber/tsx) or [@swc-node/register](https://github.com/swc-project/swc-node) or [ts-node](https://github.com/TypeStrong/ts-node) installed) to your project root.

Also, you can simply generate a Foyfile.js via:

Expand Down
56 changes: 38 additions & 18 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,58 @@ import chalk from 'chalk'
import { initDefaultCli } from './default-cli'
import { spawn } from 'child_process'

const { foyFiles, registers } = initDefaultCli()
const { foyFiles, registers, defaultCli } = initDefaultCli()
async function main() {
const pkg = await fs.readJson('./package.json')
const isESM = pkg.type === 'module'
const deps = { ...pkg.dependencies, ...pkg.devDependencies }
for (const foyFile of foyFiles) {
let env = 'node'
if (['.ts', '.cts', '.tsx', '.ctsx'].includes(extname(foyFile))) {
if ('ts-node' in deps) {
env = 'ts-node'
} else if ('tsx' in deps) {
env = 'tsx'
} else if ('@swc-node/register' in deps) {
if (isESM) {
registers.push('@swc-node/register/esm-register')
let executor = defaultCli.options.executor
if (!executor) {
executor = 'node'

if (['.ts', '.cts', '.tsx', '.ctsx'].includes(extname(foyFile))) {
if ('ts-node' in deps) {
executor = isESM ? 'ts-node-esm' : 'ts-node'
} else if ('tsx' in deps) {
executor = 'tsx'
} else if ('@swc-node/register' in deps) {
if (isESM) {
registers.push('@swc-node/register/esm-register')
} else {
registers.push('@swc-node/register')
}
} else if ('swc-node' in deps) {
executor = 'swc-node'
} else {
registers.push('@swc-node/register')
logger.error('no tsx/ts-node/swc-node or @swc-node/register found')
process.exit(1)
}
} else if ('swc-node' in deps) {
env = 'swc-node'
} else {
logger.error('no ts-node/tsx/swc-node or @swc-node/register found')
process.exit(1)
}
}
const args = [
...registers.map((r) => `--${isESM ? 'import' : 'require'} ${r}`),
foyFile,
...process.argv.slice(2),
]
spawn(env, args, {
stdio: 'inherit'
let NODE_OPTIONS = process.env.NODE_OPTIONS ?? ''
;[
['--inspect',defaultCli.options.inspect],
['--inspectBrk',defaultCli.options.inspectBrk]
].map(([inspect, inspectVal]) => {
if (inspectVal) {
if (typeof inspectVal === 'string') {
inspect += '=' + inspectVal
}
NODE_OPTIONS += ' ' + inspect
}
})
spawn(executor, args, {
stdio: 'inherit',
env:{
...process.env,
NODE_OPTIONS,
}
})
}
}
Expand Down
44 changes: 8 additions & 36 deletions src/default-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,22 @@ import requireStr from 'require-from-string'
export function initDefaultCli() {
const defaultCli = cac()
// generate default argv because cac cannot handle `foy command` + `foy options`
function generateArgv() {
let defaultArgv: string[] = []
let taskArgv: string[] = []
const argv = process.argv.slice(2)
const defaultOptions = new Map<string, number>([
['--config', 1],
['-c', 1],
['--require', 1],
['-r', 1],
['--init', 1],
['-i', 1],
['--completion', 1],
['--completion-profile', 0],
])
let i = 0
for (i = 0; i < argv.length; i++) {
const arg = argv[i]
let valLen = defaultOptions.get(arg)
if (Is.defined(valLen)) {
let end = i + valLen
defaultArgv.push(...argv.slice(i, end + 1))
i = end
} else {
break
}
}
taskArgv = argv.slice(i)
defaultArgv = process.argv.slice(0, 2).concat(defaultArgv)
taskArgv = process.argv.slice(0, 2).concat(taskArgv)
return [defaultArgv, taskArgv]
}
const [defaultArgv, taskArgv] = generateArgv()

// parse default options
const defaultOptions = [
[`--config, -c <...files>`, 'The Foyfiles'],
[`--require, -r <...names>`, 'Require the given modules'],
[`--executor, -e [name]`, 'Custom executor to replace node, e.g. tsx/ts-node'],
[`--init, -i [ext]`, 'Generate the Foyfile, [ext] can be "ts" | "js", default is "js"'],
[`--completion [val]`, `Generate completion words`],
[`--completion [words]`, `Generate completion words`],
[`--completion-profile`, `Generate completion shell profile`],
]
[`--inspect`, `node inspect`],
[`--inspect-brk`, `node inspect`],
] as const
defaultOptions.forEach(([name, desc]) => {
defaultCli.option(name, desc)
})
defaultCli.parse(defaultArgv)
let parsedArgv =defaultCli.parse(process.argv)

if (defaultCli.options.init) {
let ext = defaultCli.options.init
Expand Down Expand Up @@ -258,5 +229,6 @@ _foy_complete_func()
}
process.exit()
}
return { taskArgv, defaultHelpMsg, defaultCli, outputCompletion, registers, foyFiles }
const taskArgs = process.argv.slice(0, 2).concat(process.argv.slice(process.argv.findIndex(a=> a===parsedArgv.args[0])))
return { defaultHelpMsg, defaultCli, outputCompletion, registers, foyFiles, taskArgs }
}
4 changes: 2 additions & 2 deletions src/run-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import chalk from 'chalk'
import { initDefaultCli } from './default-cli'

export function runCli() {
const { defaultCli, defaultHelpMsg, outputCompletion, taskArgv } = initDefaultCli()
const { defaultCli, defaultHelpMsg, outputCompletion, taskArgs } = initDefaultCli()
const taskCli = cac("foy")

let taskManager = getGlobalTaskManager()
Expand Down Expand Up @@ -62,7 +62,7 @@ export function runCli() {
process.exit(1)
})

taskCli.parse(taskArgv)
taskCli.parse([...taskArgs])

if (process.argv.length === 2) {
taskCli.outputHelp()
Expand Down

0 comments on commit 8cee42f

Please sign in to comment.