From 46e05aeff376dd5d1ac2413d835885b87cdbd893 Mon Sep 17 00:00:00 2001 From: biaov Date: Thu, 12 Oct 2023 10:47:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A1=B9=E7=9B=AE=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E7=9A=84=E4=B8=80=E4=BA=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/mine-auto-cli.js | 2 +- package-lock.json | 4 +-- package.json | 13 +++++---- scripts/hooks.js | 64 +++++++++++++++++++++++++------------------- vite.config.ts | 2 +- 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/bin/mine-auto-cli.js b/bin/mine-auto-cli.js index b8e2964..2c79ffa 100644 --- a/bin/mine-auto-cli.js +++ b/bin/mine-auto-cli.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -import '../index.js' +import '../dist/index.js' diff --git a/package-lock.json b/package-lock.json index 1e02993..e037e8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mine-auto-cli", - "version": "2.0.2", + "version": "2.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mine-auto-cli", - "version": "2.0.2", + "version": "2.0.3", "license": "ISC", "dependencies": { "chalk": "^5.3.0", diff --git a/package.json b/package.json index 96ac821..6d628ce 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "mine-auto-cli", "private": false, - "version": "2.0.2", - "exports": "./index.js", - "main": "./index.js", + "version": "2.0.3", + "exports": "./dist/index.js", + "main": "./dist/index.js", "type": "module", "bin": { "auto": "bin/mine-auto-cli.js" @@ -16,11 +16,10 @@ "description": "一个将多个命令简化成一个命令的项目 😆", "scripts": { "start": "npm run dev", - "dev": "vite build --watch && node scripts/build", - "build": "vite build && node scripts/build", + "dev": "npm run build -- --watch", + "build": "node scripts/build && vite build", "prettier": "prettier --write '**/*.{js,ts,md,json}'", - "ncu": "ncu --configFileName .ncurc.json && npm i", - "pre-publish": "start cmd /k cd ./dist" + "ncu": "ncu --configFileName .ncurc.json && npm i" }, "publishConfig": { "registry": "https://registry.npmjs.org/" diff --git a/scripts/hooks.js b/scripts/hooks.js index d38bdea..2c60fc3 100644 --- a/scripts/hooks.js +++ b/scripts/hooks.js @@ -1,4 +1,4 @@ -import { writeFileSync, copyFileSync, existsSync, readdirSync, mkdirSync, readFileSync } from 'fs' +import { writeFileSync, copyFileSync, existsSync, readdirSync, mkdirSync, readFileSync, statSync, unlinkSync } from 'fs' import { join } from 'path' import { resetPath } from './path.js' @@ -9,6 +9,12 @@ const packageJson = pkg * 重写 package.json */ export const rewritePackage = () => { + /** + * 重置输出目录 + */ + const output = resetPath('@/dist') + !existsSync(output) && mkdirSync(output) + packageJson.devDependencies = packageJson.scripts = {} /** * 写入最新的 @@ -17,39 +23,41 @@ export const rewritePackage = () => { } /** - * 复制资源 + * 拷贝目录 */ -export const copyAssets = () => { - /** - * 根目录需要复制文件夹 - */ - const needDirs = ['bin'] - /** - * 根目录需要复制的文件 - */ - const filePaths = ['README.md', 'LICENSE'] - /** - * 根目录 - */ - const inputDir = resetPath('@') - /** - * 输出目录 - */ - const outputDir = resetPath('@/dist') - - !existsSync(outputDir) && mkdirSync(outputDir) - needDirs.forEach(dir => { - const dirPath = resetPath(`@/${dir}`) +const copyDirectory = (source, destination) => { + const stat = statSync(source) + if (stat.isFile()) { + /** + * 复制文件 + */ + existsSync(destination) && unlinkSync(destination) + copyFileSync(source, destination) + } else if (stat.isDirectory()) { /** * 创建目录 */ - mkdirSync(join(outputDir, dir)) - readdirSync(dirPath).forEach(file => { - filePaths.push(`${dir}/${file}`) + !existsSync(destination) && mkdirSync(destination) + + /** + * 原有目录 + */ + const directory = readdirSync(source) + directory.forEach(file => { + copyDirectory(join(source, file), join(destination, file)) }) - }) + } +} +/** + * 复制资源 + */ +export const copyAssets = () => { + /** + * 根目录需要复制的文件和目录 + */ + const filePaths = ['bin', 'README.md', 'LICENSE'] filePaths.forEach(path => { - copyFileSync(join(inputDir, path), join(outputDir, path)) + copyDirectory(resetPath(`@/${path}`), resetPath(`@/dist/${path}`)) }) } diff --git a/vite.config.ts b/vite.config.ts index a4bf208..30cce9c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -10,7 +10,7 @@ const config: UserConfig = { }, build: { target: 'node16', - outDir: resolve(__dirname, './dist'), + outDir: resolve(__dirname, './dist/dist'), lib: { entry: resolve(__dirname, './src/index.ts'), formats: ['es']