Skip to content

Commit

Permalink
feat: add update cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiden-FE committed Mar 18, 2024
1 parent 9d60f98 commit e236cb8
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
parserOptions: {
project: ['./tsconfig.json'],
},
ignorePatterns: ['.eslintrc.cjs', 'dist', 'types'],
ignorePatterns: ['.eslintrc.cjs', 'dist', 'types', 'node_modules'],
extends: [
// typescript使用此配置
'@compass-aiden/eslint-config/ts',
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Publish and deploy

on:
push:
branches:
- master
tags:
- v*

permissions:
contents: write
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/tag-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Create tag and release

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# 设置 git
- name: Setup git
run: |
git config --local user.email aiden_fe@users.noreply.github.com
git config --local user.name AidenFEBot
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Create a GitHub release
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}

- name: Updated version
run: |
npm pkg set version=${{ steps.tag_version.outputs.new_tag }}
git add -A
git commit -m "chore: created tag ${{ steps.tag_version.outputs.new_tag }}"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmmirror.com/
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@types/inquirer": "^9.0.7",
"@types/node": "^20.11.27",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
Expand All @@ -68,6 +69,7 @@
"typescript": "^5.4.2"
},
"dependencies": {
"@compass-aiden/helpers": "^0.1.1",
"@compass-aiden/telegram": "^2.1.0",
"axios": "^1.6.7",
"chalk": "^5.3.0",
Expand Down
24 changes: 23 additions & 1 deletion pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import terser from '@rollup/plugin-terser';
import cleanup from 'rollup-plugin-cleanup';
import summary from 'rollup-plugin-summary';
import { visualizer } from 'rollup-plugin-visualizer';
import pkg from './package.json' assert { type: 'json' };

const IS_PROD = !process.env.ROLLUP_WATCH;

Expand Down Expand Up @@ -71,7 +72,7 @@ export default [
{
input: 'src/main.ts',
output: getOutput(),
external: getExternal(['axios', 'chalk', 'commander', 'inquirer', 'ora']),
external: getExternal(Object.keys(pkg.dependencies).filter((dep) => !['@compass-aiden/telegram'].includes(dep))),
plugins: getPlugins({
nodeResolve: { browser: false, exportConditions: ['node'] },
}),
Expand Down
63 changes: 58 additions & 5 deletions src/commands/update.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { execSync } from 'child_process';
import { Command } from 'commander';
import ora from 'ora';
import chalk from 'chalk';
import { getRepoReleasesFromNpm } from '@/http';
import { version } from '../../package.json';
import inquirer from 'inquirer';
import { compareVersion } from '@compass-aiden/helpers/cjs';
import { getRepoInfoFromNpm } from '@/http';
import { version, name } from '../../package.json';

/**
* @description 检查Compass CLI是否存在新版本内容
Expand All @@ -18,9 +21,59 @@ export default (program: Command) => {
.action(async () => {
const loading = ora();
loading.start(chalk.cyan('正在检查版本信息'));
const releases = await getRepoReleasesFromNpm();
const releases = await getRepoInfoFromNpm(name);
if (!releases?.['dist-tags']?.latest) {
loading.fail(chalk.red('获取更新信息失败'));
return;
}
const latestVersion = releases['dist-tags'].latest;
loading.stop();
console.log(`Current version: ${version}, latest is ${latestVersion}`);
if (compareVersion(version, latestVersion) >= 0) {
loading.succeed(chalk.green('当前已是最新版本'));
return;
}
loading.warn(chalk.yellow(`发现新版本: ${latestVersion}`));
inquirer
.prompt([
{
type: 'confirm',
name: 'isUpdate',
message: '是否立即更新',
default: true,
},
])
.then((options) => {
if (!options.isUpdate) return;
inquirer
.prompt([
{
type: 'list',
name: 'commandType',
message: '请选择对应工具更新',
choices: [
{ name: 'npm', value: 'npm' },
{ name: 'yarn', value: 'yarn' },
{ name: 'pnpm', value: 'pnpm' },
],
},
])
.then((opts) => {
const updateLoading = ora();
updateLoading.start(chalk.cyan('开始更新Compass CLI'));
switch (opts.commandType) {
case 'yarn':
execSync(`yarn global add ${name}`, { stdio: 'inherit' });
break;
case 'pnpm':
execSync(`pnpm add ${name} --global`, { stdio: 'inherit' });
break;
case 'npm':
default:
execSync(`npm install -g ${name}`, { stdio: 'inherit' });
break;
}
updateLoading.succeed(chalk.green('更新成功,当前已是最新版本.'));
execSync(`compass -v`, { stdio: 'inherit' });
});
});
});
};
15 changes: 9 additions & 6 deletions src/http/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ const defaultErrorInterceptor: HttpTelegramErrorInterceptor = (error) => {
throw new Error(`[${error.response?.status || 'Api Error'}]: ${error.message || error}`);
};

const Api = new Telegram({
interceptors: {
response: defaultResInterceptor,
responseError: defaultErrorInterceptor,
},
})
const Api = new Telegram()
.register('github', {
baseURL: 'https://api.github.com',
interceptors: {
response: defaultResInterceptor,
responseError: defaultErrorInterceptor,
},
})
.register('npm', {
baseURL: 'https://registry.npmjs.org',
interceptors: {
response: defaultResInterceptor,
responseError: defaultErrorInterceptor,
},
});

export default Api;
4 changes: 2 additions & 2 deletions src/http/npm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Api from './api';

// eslint-disable-next-line import/prefer-default-export
export function getRepoReleasesFromNpm() {
return Api.chain().domain('npm').get('/@compass-aiden/commander').request();
export function getRepoInfoFromNpm(repoName: string) {
return Api.chain().domain('npm').get(`/${repoName}`).request();
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "bundler",
"lib": ["ESNext"],
"strict": true,
"sourceMap": true,
Expand Down

0 comments on commit e236cb8

Please sign in to comment.