Skip to content

Commit

Permalink
feat: 支持 Monorepo 发布 github package
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 23, 2024
1 parent e5311e0 commit 5cb5165
Show file tree
Hide file tree
Showing 23 changed files with 8,771 additions and 2,985 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
max_line_length = 160
tab_width = 2
trim_trailing_whitespace = true
41 changes: 41 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Macos
.DS_Store

# Windows
$RECYCLE.BIN/
Desktop.ini
ehthumbs.db
Thumbs.db

# Node
node_modules
package-lock.json
pnpm-lock.yaml
yarn.lock

# local env files
.env.local
.env.*.local

# Log files
/logs
*.log
*.log.*

# Editor directories and files
.idea
.vscode
!.vscode/extensions.json

# dist and cache
/dist
/dist-*
/dist_*
.cache

# testing
/coverage

# project
/test/example-dest

52 changes: 52 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { defineConfig } = require('eslint-define-config');

/**
* eslint config
* @ref https://eslint.org/
*/
module.exports = defineConfig({
root: true,

env: {
browser: true,
node: true,
es2022: true,
},

ignorePatterns: ['!.*rc.*'],

overrides: [
{
files: ['*.cjs'],
extends: [
//
'eslint:recommended',
'plugin:import/recommended',
],
rules: {
'prettier/prettier': 'error',
},
},
{
files: ['*.ts', '*.mts'],
parser: '@typescript-eslint/parser',
extends: [
//
'eslint:recommended',
'plugin:import/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
rules: {
'prettier/prettier': 'error',
'@typescript-eslint/no-unused-vars': 'off',
},
settings: {
'import/resolver': {
node: true,
typescript: true,
},
},
},
],
});
15 changes: 15 additions & 0 deletions .github/actions/setup-nvm/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: setup nvm
description: 依照 nvmrc 创建 node 环境

runs:
using: composite
steps:
- run: sudo timedatectl set-timezone Asia/Shanghai
shell: bash
- run: timedatectl
shell: bash
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
cache: npm
cache-dependency-path: '**/package-lock.json'
29 changes: 29 additions & 0 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 代码格式检查

name: code review

on:
push:
branches:
- v*.x
pull_request:
schedule:
# 19:00(UTC) 每天,相当于 03:00(GMT+8)
- cron: '0 19 * * *'

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-nvm
- run: npm ci
- run: npm run lint

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-nvm
- run: npm ci
- run: npm run test
73 changes: 46 additions & 27 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,52 @@
name: release please

on:
push:
branches:
- v*.x
push:
branches:
- v*.x

jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
outputs:
release_created: ${{ steps.release.outputs.release_created }}
major: ${{ steps.release.outputs.major }}
minor: ${{ steps.release.outputs.minor }}
steps:
- uses: googleapis/release-please-action@v4
id: release
with:
release-type: node
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-nvm
- run: npm ci
- run: npm run build
- uses: peter-evans/create-pull-request@v4
id: pr
with:
committer: github-actions[bot] <[email protected]>
author: github-actions[bot] <[email protected]>
commit-message: 'chore: build dist'
title: 机器人自动创建的拉取请求,将会自动合并,请勿手动处理!
body: 机器人自动创建的拉取请求,将会自动合并,请勿手动处理!
- run: gh pr merge ${{ steps.pr.outputs.pull-request-number }} --merge --auto --delete-branch
if: ${{ steps.pr.outputs.pull-request-number }}
env:
GH_TOKEN: ${{ github.token }}
release:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
outputs:
release_created: ${{ steps.release.outputs.release_created }}
major: ${{ steps.release.outputs.major }}
minor: ${{ steps.release.outputs.minor }}
steps:
- uses: googleapis/release-please-action@v4
id: release
with:
release-type: node

create-version:
runs-on: ubuntu-latest
needs: release
if: needs.release.outputs.release_created
steps:
- uses: actions/checkout@v2
- uses: jacobsvante/tag-major-minor-action@v0
with:
major: ${{ needs.release.outputs.major }}
minor: ${{ needs.release.outputs.minor }}
create-version:
runs-on: ubuntu-latest
needs: release
if: needs.release.outputs.release_created
steps:
- uses: actions/checkout@v2
- uses: jacobsvante/tag-major-minor-action@v0
with:
major: ${{ needs.release.outputs.major }}
minor: ${{ needs.release.outputs.minor }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pnpm-debug.log*
!.vscode/extensions.json

# dist and cache
/dist
# /dist
/dist-*
/dist_*
.cache
Expand Down
8 changes: 7 additions & 1 deletion .lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* lint-staged config
* @ref https://www.npmjs.com/package/lint-staged
*/
module.exports = {
'*': 'prettier --ignore-unknown --write',
'*.{cjs,mjs,ts,tsx}': 'eslint --fix',
'*.{cjs,mjs,ts,tsx,html,css,scss}': 'prettier --write',
'(package|tsconfig*).json': 'prettier --write',
};
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
20
23 changes: 10 additions & 13 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Macos
.DS_Store

# Windows
$RECYCLE.BIN/
Desktop.ini
ehthumbs.db
Thumbs.db

# Node
node_modules
package-lock.json
Expand All @@ -12,11 +18,9 @@ yarn.lock
.env.*.local

# Log files
/logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
*.log.*

# Editor directories and files
.idea
Expand All @@ -32,12 +36,5 @@ pnpm-debug.log*
# testing
/coverage

# next.js
.next
/out
/build

# nuxt.js
.nuxt
.nitro
.output
# project
/test/example-dest
8 changes: 5 additions & 3 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/**
* prettier.config
* @ref https://prettier.io/
* @type {import('prettier').Config}
*/
module.exports = {
tabWidth: 2,
singleQuote: true,
printWith: 120,
tabWidth: 4,
singleQuote: true,
printWidth: 160,
};
14 changes: 3 additions & 11 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,12 @@ runs:
echo "registry=https://npm.pkg.github.com/" >> .npmrc
- name: Modify the package.json name to meet GitHub's naming requirements
if: inputs.target == 'github'
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const pkg = require('./package.json');
// originName -> underlineName
// my-pkg -> my-pkg
// @my-scope/my-pkg -> my-scope__my-pkg
const underlineName = pkg.name.replace(/@(.*)\/(.*)/, '$1__$2');
// @ref https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/github/src/context.ts
const owner = context.payload.repository.owner.login;
pkg.name = '@' + owner + '/' + underlineName;
const fs = require('fs');
fs.writeFileSync('package.json', JSON.stringify(pkg), 'utf8');
const script = require('./dist/index.cjs');
return await script(__dirname, context);
- name: Publish
shell: bash
Expand Down
22 changes: 22 additions & 0 deletions dist/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";
const mapWorkspaces = require("@npmcli/map-workspaces");
const fs = require("fs");
const path = require("path");
async function rewritePkgJSON(cwd, context) {
var _a;
const owner = (_a = context.payload.repository) == null ? void 0 : _a.owner.login;
if (!owner) {
throw new Error("No owner found in context");
}
const pkg = JSON.parse(fs.readFileSync(path.join(cwd, "/package.json"), "utf-8"));
const workspaces = await mapWorkspaces({ pkg, cwd });
Object.values(workspaces).forEach((workspace) => {
const file = path.join(workspace, "/package.json");
const pkg2 = JSON.parse(fs.readFileSync(file, "utf-8"));
const underlineName = pkg2.name.replace(/@(.*)\/(.*)/, "$1__$2");
pkg2.name = "@" + owner + "/" + underlineName;
fs.writeFileSync(file, JSON.stringify(pkg2), "utf-8");
});
}
module.exports = rewritePkgJSON;
//# sourceMappingURL=index.cjs.map
1 change: 1 addition & 0 deletions dist/index.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Context } from '@actions/github/lib/context';

export default function rewritePkgJSON(cwd: string, context: Context): Promise<void>;
23 changes: 23 additions & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mapWorkspaces from "@npmcli/map-workspaces";
import fs from "fs";
import path from "path";
async function rewritePkgJSON(cwd, context) {
var _a;
const owner = (_a = context.payload.repository) == null ? void 0 : _a.owner.login;
if (!owner) {
throw new Error("No owner found in context");
}
const pkg = JSON.parse(fs.readFileSync(path.join(cwd, "/package.json"), "utf-8"));
const workspaces = await mapWorkspaces({ pkg, cwd });
Object.values(workspaces).forEach((workspace) => {
const file = path.join(workspace, "/package.json");
const pkg2 = JSON.parse(fs.readFileSync(file, "utf-8"));
const underlineName = pkg2.name.replace(/@(.*)\/(.*)/, "$1__$2");
pkg2.name = "@" + owner + "/" + underlineName;
fs.writeFileSync(file, JSON.stringify(pkg2), "utf-8");
});
}
export {
rewritePkgJSON as default
};
//# sourceMappingURL=index.mjs.map
1 change: 1 addition & 0 deletions dist/index.mjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5cb5165

Please sign in to comment.