Skip to content

Commit

Permalink
fix: esm
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaack committed Aug 6, 2024
1 parent 90a479f commit 579f12d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 70 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"cli-spinners": "^2.4.0",
"execa": "^4.0.3",
"figures": "^3.2.0",
"require-from-string": "^2.0.2",
"shell-parser": "^1.0.0",
"strip-ansi": "^6.0.0",
"tslib": "^2.0.1",
Expand Down
105 changes: 48 additions & 57 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,59 @@ import { logger } from './logger'
import { Is } from './utils'
import { getGlobalTaskManager } from './task-manager'
import chalk from 'chalk'
import { defaultHelpMsg, taskArgv, defaultCli, outputCompletion, loadConfigPromises } from './default-cli'
import { defaultHelpMsg, taskArgv, defaultCli, outputCompletion } from './default-cli'

async function main() {
if (loadConfigPromises.length) {
await Promise.all(loadConfigPromises)
}
const taskCli = cac()
const taskCli = cac()

let taskManager = getGlobalTaskManager()
taskManager.getTasks().forEach((t) => {
let strict = taskManager.globalOptions.strict || t.strict
let cmd = taskCli.command(t.name, t.desc, { allowUnknownOptions: !strict })
if (t.optionDefs) {
t.optionDefs.forEach((def) => cmd.option(...def))
}
cmd.action(async (...args) => {
let options = args.pop()
let { globalOptions } = taskManager
globalOptions.rawArgs = taskCli.rawArgs
globalOptions.options = options
await taskManager
.run(t.name, {
options,
rawArgs: taskCli.rawArgs.slice(3),
})
.catch((err) => {
logger.error(err)
throw err
})
})
let taskManager = getGlobalTaskManager()
taskManager.getTasks().forEach((t) => {
let strict = taskManager.globalOptions.strict || t.strict
let cmd = taskCli.command(t.name, t.desc, { allowUnknownOptions: !strict })
if (t.optionDefs) {
t.optionDefs.forEach((def) => cmd.option(...def))
}
cmd.action(async (...args) => {
let options = args.pop()
let { globalOptions } = taskManager
globalOptions.rawArgs = taskCli.rawArgs
globalOptions.options = options
await taskManager
.run(t.name, {
options,
rawArgs: taskCli.rawArgs.slice(3),
})
.catch((err) => {
logger.error(err)
throw err
})
})
})

taskCli.help((sections) => {
if (!taskCli.matchedCommand) {
let last = sections[sections.length - 1]
let lines = last.body.split('\n')
lines.pop()
last.body = defaultHelpMsg
}
console.log(
sections
.map((section) => {
return section.title ? `${section.title}:\n${section.body}` : section.body
})
.join('\n\n'),
)
process.exit(0)
})
taskCli.parse(taskArgv)
taskCli.help((sections) => {
if (!taskCli.matchedCommand) {
let last = sections[sections.length - 1]
let lines = last.body.split('\n')
lines.pop()
last.body = defaultHelpMsg
}
console.log(
sections
.map((section) => {
return section.title ? `${section.title}:\n${section.body}` : section.body
})
.join('\n\n'),
)
process.exit(0)
})
taskCli.parse(taskArgv)

outputCompletion(taskCli)
outputCompletion(taskCli)

taskCli.on('command:*', () => {
console.error(`error: Unknown command \`${taskCli.args.join(' ')}\`\n\n`)
process.exit(1)
})
taskCli.on('command:*', () => {
console.error(`error: Unknown command \`${taskCli.args.join(' ')}\`\n\n`)
process.exit(1)
})

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

main().catch(e => {
throw e
})
36 changes: 23 additions & 13 deletions src/default-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from 'os'
import { logger } from './logger'
import { Is } from './utils'
import { getGlobalTaskManager } from './task-manager'
import requireStr from 'require-from-string'

export const defaultCli = cac()
// generate default argv because cac cannot handle `foy command` + `foy options`
Expand Down Expand Up @@ -171,15 +172,18 @@ function isESM() {
return false
}
}
const tsnodeOptions = {
transpileOnly: true,
compilerOptions: {
module: 'commonjs',
esModuleInterop: true,
moduleResolution: 'node',
},
}
try {
if (foyFiles.some((f) => f.endsWith('.ts')) && !require.extensions['.ts']) {
let options = {
transpileOnly: true,
compilerOptions: {
module: 'commonjs',
},
}
require('ts-node').register(options)

require('ts-node').register(tsnodeOptions)
}
} catch (error) {
// ignore
Expand All @@ -201,13 +205,19 @@ try {
}
}

export const loadConfigPromises:Promise<any>[]=[]
// load foyfiles
for (const file of foyFiles) {
if (isESM()) {
loadConfigPromises.push(import(file))
} else {
require(file)
{
let tsnode: any
for (const file of foyFiles) {
if (isESM()) {
// use tsnode to transpile ESM
if (!tsnode) {
tsnode = require('ts-node').create(tsnodeOptions)
}
requireStr(tsnode.compile(fs.readFileSync(file, 'utf-8'), file), file.replace('.ts', '.cjs'))
} else {
require(file)
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,11 @@ repeat-string@^1.6.1:
resolved "https://registry.npm.taobao.org/repeat-string/download/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637"
integrity sha1-jcrkcOHIirwtYA//Sndihtp15jc=

require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.npmmirror.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==

resolve-url@^0.2.1:
version "0.2.1"
resolved "https://registry.npm.taobao.org/resolve-url/download/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
Expand Down

0 comments on commit 579f12d

Please sign in to comment.