Skip to content

Commit a04ebc4

Browse files
committed
fix: support ts, esm config file
1 parent 9d13a52 commit a04ebc4

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
## 前言
66

7-
当项目遇到大量重复性代码,我们会想到编写脚本完成重复性工作。当项目越大,脚本数量也会越来越多,如何管理是难题,如何复用也是难题。脚本复用,可以将一些定制化的数据通过函数传参传入,这也意味着,脚本需要导出一个函数。如何管理?可以使用一个入口文件,在入口文件中引入并配置和执行所有的脚本,这样,启动项目时,先运行脚本入口文件即可。
7+
当项目遇到大量重复性代码,我们会想到编写脚本完成重复性工作。
8+
9+
当项目越大,脚本数量也会越来越多,如何管理是难题,如何复用也是难题。
10+
11+
脚本复用,可以将一些定制化的数据通过函数传参传入,这也意味着,脚本需要导出一个函数。
12+
13+
如何管理?可以使用一个入口文件,在入口文件中引入并配置和执行所有的脚本,这样,启动项目时,先运行脚本入口文件即可。
814

915
上面介绍的流程有几个缺陷。
1016

11-
> - 全量执行。脚本导出函数,意味着脚本不能再单独执行,如果项目只需要执行脚本 A,那么只能执行入口脚本文件,此时所有脚本都会执行。
17+
> - 全量执行。脚本导出函数,意味着脚本不能直接使用 node 执行,如果项目只需要执行脚本 A,那么只能执行入口脚本文件,此时所有脚本都会执行。
1218
> - 脚本模块规范。所有脚本都只能使用同一种模块规范,才能在入口文件中进行引用。否则还需要借助三方工具进行代码转换
1319
1420
ScriptRunner 可以很好的解决上述问题。它是一款 CLI 工具,可以管理和执行 cjs, esm 和 ts 的代码,你可以用自己喜欢的语法编写脚本。它会根据注册的模块,批量或者单独的执行脚本,一切随你的喜好而来。
@@ -41,7 +47,7 @@ script: {
4147

4248
### 配置文件
4349

44-
在项目根目录,书写名为 `scr.config.js` 的配置文件。ScriptRunner 支持直接执行 CommonJS、ESMTypeScript 的代码。当开发脚本时,可以直接指定模块位置为我们的脚本项目的入口文件,这将非常有用,可以及时运行脚本,查看我们的脚本有没有问题。当发布了 NPM 包,可以直接使用包名作为配置的 `module` 名称,此时类型为 cjs。
50+
ScriptRunner 支持直接解析和执行 cjs、esmts 的脚本代码,配置文件同样支持 cjs,esm 与 ts。[示例请查看:example](example/README.md)
4551

4652
```js
4753
module.exports = {

src/index.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Command, flags } from '@oclif/command'
22
import { resolve, isAbsolute } from 'path'
3-
import { promises } from 'fs'
4-
import * as fs from 'fs'
3+
import { promises, constants } from 'fs'
54
import * as tsNode from 'ts-node'
65
import { Config, Script } from './types'
76

@@ -23,7 +22,7 @@ class ScriptRunner extends Command {
2322
async parseConfigPath(paths: string[]) {
2423
for (const path of paths) {
2524
try {
26-
await promises.access(path, fs.constants.F_OK)
25+
await promises.access(path, constants.F_OK)
2726
return path
2827
} catch { }
2928
}
@@ -34,7 +33,7 @@ class ScriptRunner extends Command {
3433
let configPaths: string[] = []
3534

3635
if (!filePath) {
37-
configPaths = ['js', 'mjs', 'ts'].map(ext => resolve(process.cwd(), `scr.config.${ext}`))
36+
configPaths = ['js', 'ts'].map(ext => resolve(process.cwd(), `scr.config.${ext}`))
3837
} else {
3938
if (isAbsolute(filePath)) {
4039
configPaths.push(filePath)
@@ -44,21 +43,13 @@ class ScriptRunner extends Command {
4443
}
4544

4645
const configPath = await this.parseConfigPath(configPaths)
47-
const [, fileExt] = configPath.match(/.+\.(\w+)$/) || []
4846

49-
if (fileExt !== 'js') {
50-
tsNode.register({ dir: configPath, skipProject: true, transpileOnly: true, compilerOptions: { allowJs: true } })
51-
}
47+
tsNode.register({ dir: configPath, skipProject: true, transpileOnly: true, compilerOptions: { allowJs: true } })
5248

53-
return require(configPath)
49+
const configModule = require(configPath)
50+
return configModule.default ?? configModule
5451
}
5552

56-
// runTs(script: Script) {
57-
// const { module, args } = script
58-
// tsNode.register({ dir: require.resolve(module), skipProject: true, transpileOnly: true })
59-
// require(module).default(...args)
60-
// }
61-
6253
runTs(script: Script) {
6354
const { module, args } = script
6455
tsNode.register({ dir: require.resolve(module), skipProject: true, transpileOnly: true, compilerOptions: { allowJs: true } })
@@ -74,8 +65,6 @@ class ScriptRunner extends Command {
7465
runScript(script: Script) {
7566
switch (script.type) {
7667
case 'ts':
77-
// this.runTs(script)
78-
// break
7968
case 'esm':
8069
this.runTs(script)
8170
break

0 commit comments

Comments
 (0)