Skip to content

Commit

Permalink
fix: support ts, esm config file
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoer committed Aug 31, 2021
1 parent 9d13a52 commit a04ebc4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

## 前言

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

当项目越大,脚本数量也会越来越多,如何管理是难题,如何复用也是难题。

脚本复用,可以将一些定制化的数据通过函数传参传入,这也意味着,脚本需要导出一个函数。

如何管理?可以使用一个入口文件,在入口文件中引入并配置和执行所有的脚本,这样,启动项目时,先运行脚本入口文件即可。

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

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

### 配置文件

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

```js
module.exports = {
Expand Down
23 changes: 6 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Command, flags } from '@oclif/command'
import { resolve, isAbsolute } from 'path'
import { promises } from 'fs'
import * as fs from 'fs'
import { promises, constants } from 'fs'
import * as tsNode from 'ts-node'
import { Config, Script } from './types'

Expand All @@ -23,7 +22,7 @@ class ScriptRunner extends Command {
async parseConfigPath(paths: string[]) {
for (const path of paths) {
try {
await promises.access(path, fs.constants.F_OK)
await promises.access(path, constants.F_OK)
return path
} catch { }
}
Expand All @@ -34,7 +33,7 @@ class ScriptRunner extends Command {
let configPaths: string[] = []

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

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

if (fileExt !== 'js') {
tsNode.register({ dir: configPath, skipProject: true, transpileOnly: true, compilerOptions: { allowJs: true } })
}
tsNode.register({ dir: configPath, skipProject: true, transpileOnly: true, compilerOptions: { allowJs: true } })

return require(configPath)
const configModule = require(configPath)
return configModule.default ?? configModule
}

// runTs(script: Script) {
// const { module, args } = script
// tsNode.register({ dir: require.resolve(module), skipProject: true, transpileOnly: true })
// require(module).default(...args)
// }

runTs(script: Script) {
const { module, args } = script
tsNode.register({ dir: require.resolve(module), skipProject: true, transpileOnly: true, compilerOptions: { allowJs: true } })
Expand All @@ -74,8 +65,6 @@ class ScriptRunner extends Command {
runScript(script: Script) {
switch (script.type) {
case 'ts':
// this.runTs(script)
// break
case 'esm':
this.runTs(script)
break
Expand Down

0 comments on commit a04ebc4

Please sign in to comment.