0.0.12 • Published 4 months ago

@yinpo/node-scripts v0.0.12

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

node script

installation

npm i -D @yinpo/node-scripts

脚本目录:

version check

检查指定目录下package.json中的version字段是否有更新,如果当前version小于等于线上的version,则会返回错误码1。

防止在npm publish进行自动发布时,被npm拦截。

默认是拉取npm上对应name的包,也可以通过传递target参数指定github url进行对比。

  1. 参数
function versionCheck(projectPathArr: string[], target?: string): void

projectPathArr 表示项目根路径

  1. 引用

在项目指定文件下引入该脚本,例如:

// /script/versionCheck.js
#!/usr/bin/env node

require('@yinpo/node-scripts').versionCheck([__dirname, '..'])

因为脚本路径为 /script/versionCheck.js, 所以传入路劲数组 [__dirname, '..'] 表示的是根路径 /

最终脚本寻找的是/package.json中的version字段

  1. 使用

当定义好了脚本的路径之后,可以在一些自动化工具中使用。

例如在huskypre-push脚本中检查version字段,防止version字段没有更新有推到remote上。

这样能够让github action很安全的执行 npm publish 指令。

#!/bin/sh
while read local_ref local_sha remote_ref remote_sha
do
    branch_name="${remote_ref##refs/heads/}"

    # 如果分支名称为 'main' ,则执行特定逻辑
    if [[ "$branch_name" == "main" ]]; then

        node "scripts/versionCheck.js"
        checkResult=$?

        # 在此处添加你想要运行的其他命令
        if [ $checkResult -eq 1 ]; then
          echo "脚本中止!"
          exit 1
        fi
    fi

done

version check如果识别到remote与local的version字段相同,则会终止脚本并返回状态码1

export all

自动引用指定路径下的所有index.jsindex.ts 文件中导出的内容,整合到根路径下 /index.ts/index.js

  1. 参数
function exportAll(projectPathArr: string[], targetDir: string,  mode?: 'esm' | 'cjs' = "esm"): void

projectPathArr 表示项目根路径

targetDir 在项目根路径的基础下,指定的目标文件夹,会从该路径下扫描所有的导出

mode 表示采用哪种模块化规范输出

  1. 引用

在项目指定文件下引入该脚本,例如:

// /script/exportAll.js
#!/usr/bin/env node

require('@yinpo/node-scripts').exportAll([__dirname, '..'], 'src')

因为脚本路径为 /script/exportAll.js, 所以传入路劲数组 [__dirname, '..'] 表示的是根路径 /

最终脚本扫描的目标路径是/src下的所有文件夹中index.js或index.ts

/src 下的所有文件夹会被取 index.jsindex.ts 的所有导出。只有第一层下才有效,多层文件夹嵌套不会扫描。

/src 下的所有第一层文件会被扫描,然后输出所有导出。

  1. 使用

可以在package.json中定义脚本,然后在自动化处理流程中执行改脚本,例如:

{
  "scripts": {
    "build": "node ./scripts/exportAll.js && rollup -c"
  }
}

然后在一些自动化工具中直接执行 npm run build 即可。

0.0.11

4 months ago

0.0.12

4 months ago

0.0.9

4 months ago

0.0.8

4 months ago

0.0.7

4 months ago

0.0.6

4 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.1

10 months ago