From 8e641d8aab5a0b923a40497987a4534e3f1c7698 Mon Sep 17 00:00:00 2001 From: biaov Date: Tue, 28 May 2024 21:06:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/publish.yml | 15 ++++++++---- .gitignore | 3 ++- package.json | 10 ++++---- scripts/build.js | 10 -------- scripts/external.ts | 6 +++++ scripts/index.ts | 2 ++ scripts/path.js | 15 ------------ scripts/{hooks.js => rollup-plugin-copy.ts} | 24 ++++++++++++------ scripts/tag.js | 5 ++++ src/index.ts | 27 +-------------------- src/update.ts | 26 ++++++++++++++++++++ src/utils/functions.ts | 10 ++++---- vite.config.ts | 22 ++++++++--------- 13 files changed, 89 insertions(+), 86 deletions(-) delete mode 100644 scripts/build.js create mode 100644 scripts/external.ts create mode 100644 scripts/index.ts delete mode 100644 scripts/path.js rename scripts/{hooks.js => rollup-plugin-copy.ts} (64%) create mode 100644 scripts/tag.js create mode 100644 src/update.ts diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 73351e9..9e1bff2 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -15,12 +15,17 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: '20.x' + node-version: '20.12.x' registry-url: 'https://registry.npmjs.org' - - run: npm install -g npm - - run: npm ci + - run: npm i - run: npm run build - - run: cd dist - - run: npm publish + - run: | + VERSION=$(node -p "require('./package.json').version") + cd dist + if [[ "$VERSION" == *"beta"* ]]; then + npm publish --tag=beta + else + npm publish + fi env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore index 6aff181..a112368 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist node_modules -.log \ No newline at end of file +.log +.git \ No newline at end of file diff --git a/package.json b/package.json index 4f38bc1..3878808 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "create-mine", "private": false, - "version": "1.5.1", + "version": "1.5.2-beta.0", "exports": "./dist/index.js", "main": "./dist/index.js", "type": "module", @@ -18,17 +18,17 @@ "scripts": { "start": "npm run dev", "dev": "npm run build -- --watch", - "build": "node scripts/build && vite build", + "build": "vite build", "prettier": "prettier --write '**/*.{js,ts,md,json}'", "ncu": "ncu --configFileName .ncurc.json && npm i", - "pre-publish": "start cmd /k cd ./dist" + "tag": "node scripts/tag" }, "publishConfig": { "registry": "https://registry.npmjs.org/", "provenance": true }, "engines": { - "node": ">=20" + "node": ">=20.12" }, "keywords": [ "cli", @@ -68,4 +68,4 @@ "typescript": "^5.4.5", "vite": "^5.2.11" } -} \ No newline at end of file +} diff --git a/scripts/build.js b/scripts/build.js deleted file mode 100644 index 74fc008..0000000 --- a/scripts/build.js +++ /dev/null @@ -1,10 +0,0 @@ -import { rewritePackage, copyAssets } from './hooks.js' - -!(async () => { - try { - await Promise.all([rewritePackage(), copyAssets()]) - } catch (e) { - console.log(e) - process.exit(1) - } -})() diff --git a/scripts/external.ts b/scripts/external.ts new file mode 100644 index 0000000..2672b4b --- /dev/null +++ b/scripts/external.ts @@ -0,0 +1,6 @@ +import pkg from '../package.json' + +/** + * 扩展 + */ +export const external = Object.keys(pkg.dependencies) diff --git a/scripts/index.ts b/scripts/index.ts new file mode 100644 index 0000000..d13a20d --- /dev/null +++ b/scripts/index.ts @@ -0,0 +1,2 @@ +export { external } from './external' +export { default as rollupPluginCopy } from './rollup-plugin-copy' diff --git a/scripts/path.js b/scripts/path.js deleted file mode 100644 index 378ade3..0000000 --- a/scripts/path.js +++ /dev/null @@ -1,15 +0,0 @@ -import { resolve, dirname } from 'path' -import { fileURLToPath } from 'url' - -/** - * 当前文件所在目录 - */ -export const __dirname = dirname(fileURLToPath(import.meta.url)) - -/** - * 重置路径, @ 表示根目录 - */ -export const resetPath = (filePath, defaultPrefix = '../') => { - const prefix = filePath.slice(0, 1) === '@' ? defaultPrefix : '' - return resolve(__dirname, prefix + filePath.slice(1)) -} diff --git a/scripts/hooks.js b/scripts/rollup-plugin-copy.ts similarity index 64% rename from scripts/hooks.js rename to scripts/rollup-plugin-copy.ts index 6f17fbe..2225a6e 100644 --- a/scripts/hooks.js +++ b/scripts/rollup-plugin-copy.ts @@ -1,7 +1,6 @@ import { writeFileSync, copyFileSync, existsSync, readdirSync, mkdirSync, statSync, unlinkSync } from 'fs' import { join } from 'path' -import { resetPath } from './path.js' -import packageJson from '../package.json' assert { type: 'json' } +import pkg from '../package.json' assert { type: 'json' } /** * 重写 package.json @@ -10,20 +9,20 @@ export const rewritePackage = () => { /** * 重置输出目录 */ - const output = resetPath('@/dist') - !existsSync(output) && mkdirSync(output) + const output = 'dist' + + pkg.devDependencies = pkg.scripts = {} as any - packageJson.devDependencies = packageJson.scripts = {} /** * 写入最新的 */ - writeFileSync(resetPath('@/dist/package.json'), JSON.stringify(packageJson, null, 2)) + writeFileSync(`${output}/package.json`, JSON.stringify(pkg, null, 2)) } /** * 拷贝目录 */ -const copyDirectory = (source, destination) => { +const copyDirectory = (source: string, destination: string) => { const stat = statSync(source) if (stat.isFile()) { /** @@ -56,6 +55,15 @@ export const copyAssets = () => { */ const filePaths = ['bin', 'README.md', 'LICENSE'] filePaths.forEach(path => { - copyDirectory(resetPath(`@/${path}`), resetPath(`@/dist/${path}`)) + const destName = path.split('/').at(-1) + copyDirectory(path, `dist/${destName}`) }) } + +export default () => ({ + name: 'rollup-plugin-copy', + closeBundle() { + rewritePackage() + copyAssets() + } +}) diff --git a/scripts/tag.js b/scripts/tag.js new file mode 100644 index 0000000..f90f651 --- /dev/null +++ b/scripts/tag.js @@ -0,0 +1,5 @@ +import { execSync } from 'child_process' +import pkg from '../package.json' assert { type: 'json' } + +execSync(`git tag v${pkg.version}`) +execSync(`git push origin v${pkg.version}`) diff --git a/src/index.ts b/src/index.ts index f841ffd..1452ecf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,29 +1,4 @@ /// -import updateNotifier from 'update-notifier' -import chalk from 'chalk' -import pkg from '../package.json' import './commander' - -const notifier = updateNotifier({ pkg }) - -if (notifier.update && notifier.update.latest !== pkg.version) { - let msg = '' - switch (notifier.update.type) { - case 'major': - msg = chalk.red('{latestVersion}') - break - case 'minor': - msg = chalk.yellow('{latestVersion}') - break - default: - msg = chalk.green('{latestVersion}') - break - } - const compareUrl = `https://github.com/biaov/${pkg.name}/compare/v${pkg.version}...v{latestVersion}` - notifier.notify({ - defer: false, - isGlobal: true, - message: `有更新 ${chalk.dim('{currentVersion}')}${chalk.reset(' → ')}${msg}\n运行 ${chalk.cyan('{updateCommand}')} 命令更新\n${chalk.dim.underline(compareUrl)}` - }) -} +import './update' diff --git a/src/update.ts b/src/update.ts new file mode 100644 index 0000000..f97bde8 --- /dev/null +++ b/src/update.ts @@ -0,0 +1,26 @@ +import updateNotifier from 'update-notifier' +import chalk from 'chalk' +import pkg from '../package.json' + +const notifier = updateNotifier({ pkg }) + +if (notifier.update && notifier.update.latest !== pkg.version) { + let msg = '' + switch (notifier.update.type) { + case 'major': + msg = chalk.red('{latestVersion}') + break + case 'minor': + msg = chalk.yellow('{latestVersion}') + break + default: + msg = chalk.green('{latestVersion}') + break + } + const compareUrl = `https://github.com/biaov/${pkg.name}/compare/v${pkg.version}...v{latestVersion}` + notifier.notify({ + defer: false, + isGlobal: true, + message: `有更新 ${chalk.dim('{currentVersion}')}${chalk.reset(' → ')}${msg}\n运行 ${chalk.cyan('{updateCommand}')} 命令更新\n${chalk.dim.underline(compareUrl)}` + }) +} diff --git a/src/utils/functions.ts b/src/utils/functions.ts index 9156277..af2577d 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -1,14 +1,14 @@ import { writeFile, readFileSync } from 'fs' -import { resolve, dirname } from 'path' +import { resolve } from 'path' import chalk from 'chalk' -import { fileURLToPath } from 'url' -import { PresetInfo } from './types' +import type { PresetInfo } from './types' + +export const { dirname } = import.meta -export const __dirname = dirname(fileURLToPath(import.meta.url)) /** * 预设数据路径 */ -const presetPath = resolve(__dirname, './presetData.json') +const presetPath = resolve(dirname, './presetData.json') /** * 保存本地预设信息 diff --git a/vite.config.ts b/vite.config.ts index 2af53ef..bea4d75 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,33 +1,33 @@ -import { UserConfig } from 'vite' import { resolve } from 'path' +import { external, rollupPluginCopy } from './scripts' + +const { dirname } = import.meta /** * 配置文件 */ -const config: UserConfig = { - root: __dirname, +export default { resolve: { alias: { - '@': resolve(__dirname, './src') + '@': resolve(dirname, './src') } }, build: { - target: 'node16', - outDir: resolve(__dirname, './dist/dist'), + target: 'node20', + outDir: resolve(dirname, './dist/dist'), lib: { - entry: resolve(__dirname, './src/index.ts'), + entry: resolve(dirname, './src/index.ts'), formats: ['es'] }, rollupOptions: { - external: ['update-notifier', 'url', 'path', 'child_process', 'fs', 'chalk', 'commander', 'download-git-repo', 'inquirer', 'log-symbols', 'ora'], + external: [...external, 'path', 'child_process', 'fs'], output: { entryFileNames: '[name].js' - } + }, + plugins: [rollupPluginCopy()] }, ssr: false, ssrManifest: false, emptyOutDir: true } } - -export default config