zl程序教程

您现在的位置是:首页 >  Java

当前栏目

npm自动改版本号+博客静态源代码自动上传

2023-02-18 15:37:23 时间

npm自动改版本号+博客静态源代码自动上传

碎碎念

在此之前 我食用了下 abeginner.cn 的Action配置 但是行不通

教程开始

1. 在source目录下创建index.js

const fs = require('fs/promises')

const index = async () => {
  const obj = {
    date: Date.now() + ''
  }
  const pageageFile = {
    name: process.env.NPM_NAME || '',
    version: `${obj.date[0]}.${obj.date[1]}.${obj.date.slice(2, 13)}`,
    main: 'index.js',
    description: ''
  }
  const pageageFileStr = JSON.stringify(pageageFile)
  await fs.writeFile('./package.json', pageageFileStr, 'utf8', (err) => {
    if (err) throw err
  })
  console.log('写入完成')
}
index()

记得在config中skip掉

2. 想办法在public有一个package.json

我们可以直接npm init在source目录 也可以使用其他方法

记得在config中skip掉

3. 在.github/workflows创建一个

如果你使用了和店长一样的action方案 在下方直接添加

- name: 发布npm包
  if: env.NPM_TOKEN && env.NPM_NAME
  run: |
    cd public
    node index.js
    npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
    npm publish
  env:
    NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
    NPM_NAME: ${{ secrets.NPM_NAME }}

如果没有 可以先去和店长创建一个 akilar.top (超级好用的

4. 在github仓库设置一个secret

点击仓库界面的setting

点击secret -> action 并且点击new

创建两个参数

名称

内容

NPM_NAME

NPM仓库名

NPM_TOKEN

NPM密钥

完成了 Enjoy it!