diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ecb1a7f..0ae09e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,6 @@ name: CI on: push: branches: - - main - dev jobs: @@ -35,5 +34,4 @@ jobs: run: pnpm build - name: Package - # 上面的 Build 已经构建过了,这里可以忽略 Build ,避免重复运行 run: pnpm package diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index 9680f90..a4410c9 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -26,4 +26,78 @@ jobs: ## header used within the release PR body, defaults to using :robot: I have created a release *beep* *boop* pull-request-header: ':robot: A new release will be created' ## A JSON formatted String containing to override the outputted changelog sections - changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"refactor","section":"Code Refactoring","hidden":false},{"type":"chore","section":"Miscellaneous","hidden":false},{"type":"perf","section":"Performance Improvements","hidden":false}]' \ No newline at end of file + changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"refactor","section":"Code Refactoring","hidden":false},{"type":"chore","section":"Miscellaneous","hidden":false},{"type":"perf","section":"Performance Improvements","hidden":false}]' + + # Checkout + - name: Checkout + if: ${{ steps.release.outputs.release_created }} + uses: actions/checkout@v3 + + # Install Node.js + - name: Install Node.js + if: ${{ steps.release.outputs.release_created }} + uses: actions/setup-node@v3 + with: + node-version: 18 + registry-url: 'https://registry.npmjs.org' + + # Install pnpm + - name: Install pnpm + if: ${{ steps.release.outputs.release_created }} + uses: pnpm/action-setup@v2 + id: pnpm-install + with: + version: 8 + run_install: false + + # Get pnpm store directory + - name: Get pnpm store directory + if: ${{ steps.release.outputs.release_created }} + id: pnpm-cache + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT + + # Setup pnpm cache + - name: Setup pnpm cache + if: ${{ steps.release.outputs.release_created }} + uses: actions/cache@v3 + with: + path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + # Install dependencies + - name: Install dependencies + if: ${{ steps.release.outputs.release_created }} + run: pnpm install + + # Prepare new version + # https://github.com/google-github-actions/release-please-action#outputs + - name: Prepare new version + if: ${{ steps.release.outputs.release_created }} + run: | + pnpm prepareRelease + + # Build for production + - name: Build for production + if: ${{ steps.release.outputs.release_created }} + run: pnpm build + + # Archive package + - name: Build for production + if: ${{ steps.release.outputs.release_created }} + run: pnpm package + + # Upload package to release + # https://github.com/philips-labs/terraform-aws-github-runner/blob/main/.github/workflows/release.yml#L46 + - name: Upload package.zip to the release + if: ${{ steps.release.outputs.releases_created }} + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + run: | + ls -l ./build + for f in $(find ./build -name '*.zip'); do + gh release upload ${{ steps.release.outputs.tag_name }} $f + done \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 800b0db..ae2ada2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,11 @@ ## [1.1.0](https://github.com/terwer/siyuan-plugin-importer/compare/v1.0.0...v1.1.0) (2023-05-17) ### Features * 初始化插件项目 ([e41c745](https://github.com/terwer/siyuan-plugin-importer/commit/e41c7458cf8f3882b072e214a78fb858d33a29f6)) -* 完善国际化以及项目说明 ([4ad0fef](https://github.com/terwer/siyuan-plugin-importer/commit/4ad0fefeef1b04257515cc7afc41fc03685bd3a0)) +* 完善国际化以及项目说明 ([07a2099](https://github.com/terwer/siyuan-plugin-importer/commit/07a2099912318ea8f73e666b70f1d0d438f78ee7)) * 完成导入epub基础功能 ([a0580fa](https://github.com/terwer/siyuan-plugin-importer/commit/a0580fabfba2834cdd73bfa5d4ce2da79ba52a5a)) * 完成导出epub、docx、opml、html测试 ([1d671b6](https://github.com/terwer/siyuan-plugin-importer/commit/1d671b697b006d881315a90dfd45310d415272a1)) -* 导入工具第一个版本 ([1fc739b](https://github.com/terwer/siyuan-plugin-importer/commit/1fc739b676ba51460c7f57e4e22c00869517b74f)) +* 导入工具第一个版本 ([1fc739b2](https://github.com/terwer/siyuan-plugin-importer/commit/1fc739b676ba51460c7f57e4e22c00869517b74f)) * 插件最小化运行单位 ([155a825](https://github.com/terwer/siyuan-plugin-importer/commit/155a825461bf447d45083329ccce9eb93b3857d6)) -* 新增kernelApi ([80ca482](https://github.com/terwer/siyuan-plugin-importer/commit/80ca4829e5949c871ddaacad2e6fced1771bd336)) -* 完善国际化以及项目说明 ([07a2099](https://github.com/terwer/siyuan-plugin-importer/commit/07a2099912318ea8f73e666b70f1d0d438f78ee7)) +* 新增kernelapi ([80ca482](https://github.com/terwer/siyuan-plugin-importer/commit/80ca4829e5949c871ddaacad2e6fced1771bd336)) ### Miscellaneous * **main:** release 1.0.0 ([74258ce](https://github.com/terwer/siyuan-plugin-importer/commit/74258ce418a45bd64c7a4c2b947508a842691605)) \ No newline at end of file diff --git a/public/icon.png b/icon.png similarity index 100% rename from public/icon.png rename to icon.png diff --git a/package.json b/package.json index 1c1b8d1..84af635 100644 --- a/package.json +++ b/package.json @@ -13,18 +13,16 @@ "build": "vite build", "start": "vite preview", "test": "vitest --watch", - "syncVersion": "ts-node-esm --experimental-specifier-resolution=node tools/syncVersion.ts", - "parseChangelog": "ts-node-esm --experimental-specifier-resolution=node tools/changelogParser.ts", + "syncVersion": "python scripts/version.py", + "parseChangelog": "python scripts/parse_changelog.py", "prepareRelease": "pnpm syncVersion && pnpm parseChangelog", - "package": "ts-node-esm --experimental-specifier-resolution=node tools/packageApp.ts" + "package": "python scripts/package.py" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^2.0.3", - "@terwer/changelog-parser": "^1.1.0", "@terwer/eslint-config-custom": "^1.2.0", "@terwer/vite-config-custom": "^0.6.0", "@tsconfig/svelte": "^4.0.1", - "archiver": "^5.3.1", "glob": "^7.2.3", "minimist": "^1.2.8", "rollup-plugin-livereload": "^2.0.5", diff --git a/public/plugin.json b/plugin.json similarity index 100% rename from public/plugin.json rename to plugin.json diff --git a/public/preview.png b/preview.png similarity index 100% rename from public/preview.png rename to preview.png diff --git a/scripts/package.py b/scripts/package.py new file mode 100644 index 0000000..8ca59f4 --- /dev/null +++ b/scripts/package.py @@ -0,0 +1,27 @@ +import os + +import scriptutils + +if __name__ == "__main__": + # 切换工作空间 + scriptutils.switch_workdir() + + # 获取当前工作空间 + cwd = scriptutils.get_workdir() + + dist_folder = "./dist" + data = scriptutils.read_json_file(cwd + "package.json") + v = data["version"] + + src_folder = dist_folder + tmp_folder_name = "./siyuan-importer" + build_zip_path = "./build" + build_zip_name = "siyuan-plugin-importer-" + v + ".zip" + + try: + # 压缩dist为zip + scriptutils.zip_folder(src_folder, tmp_folder_name, build_zip_path, build_zip_name) + scriptutils.cp_file(os.path.join(build_zip_path, build_zip_name), os.path.join(build_zip_path, "package.zip")) + except Exception as e: + print(f"打包错误,{str(e)}") + print("插件打包完毕.") diff --git a/scripts/parse_changelog.py b/scripts/parse_changelog.py new file mode 100644 index 0000000..01c4cda --- /dev/null +++ b/scripts/parse_changelog.py @@ -0,0 +1,87 @@ +import re +import shutil +from collections import defaultdict + + +def parse_changelog(): + """ + :robot: A new release will be created + --- + + + ## 1.0.0 (2023-04-24) + + + ### ⚠ BREAKING CHANGES + + * **esbuild-config-custom:** previews esbuild config need to be a attr value for esbuild property, add a new customConfig property + + ### Features + + * [#163](https://github.com/terwer/zhi/issues/163) ([d9fae8a](https://github.com/terwer/zhi/commit/d9fae8af6f96052a82e62f9aea79505a52fdbb23)) + * Add a vuepress 2 blog ([9862b18](https://github.com/terwer/zhi/commit/9862b1811663a2b34b05d140d3197075d943696b)) + * add api docs ([97751dc](https://github.com/terwer/zhi/commit/97751dc63b6ed9143a9d6eb0d673a22d4f9c9f6a)) + * add api docs ([3b5ff6f](https://github.com/terwer/zhi/commit/3b5ff6fbb1ab3ea161e41d0436037e85e4a6cee3)) + * add api docs ([b338a67](https://github.com/terwer/zhi/commit/b338a67268e767b9289a8d768377c5cd8ce29fbe)) + * add api docs ([6f9ebd9](https://github.com/terwer/zhi/commit/6f9ebd939c8bdabc5e2e377feb13363dc0dbe769)) + * Add api docs ([9c1a5e5](https://github.com/terwer/zhi/commit/9c1a5e5036c3d95d050e4672df5f505386d29962)) + """ + + # will print + + """ + :robot: a new release will be created + --- + + ## 1.0.0 (2023-04-24) + ### ⚠ BREAKING CHANGES + * **esbuild-config-custom:** previews esbuild config need to be a attr value for esbuild property, add a new customconfig property + ### Features + * [#163](https://github.com/terwer/zhi/issues/163) ([d9fae8a](https://github.com/terwer/zhi/commit/d9fae8af6f96052a82e62f9aea79505a52fdbb23)) + * add a vuepress 2 blog ([9862b18](https://github.com/terwer/zhi/commit/9862b1811663a2b34b05d140d3197075d943696b)) + * add api docs ([9c1a5e5](https://github.com/terwer/zhi/commit/9c1a5e5036c3d95d050e4672df5f505386d29962)) + + """ + + # make a backup copy of the original file + original_file = 'CHANGELOG.md' + # backup_file = original_file.replace(".md", "_backup.md") + # shutil.copyfile(original_file, backup_file) + + # handle repeat lines + with open(original_file, 'r', encoding='utf-8') as f: + lines = [line.strip() for line in f.readlines()] + unique_commits = remove_same_commit(lines) + + # save new file + save_file = original_file + with open(save_file, 'w', encoding='utf-8') as f: + f.write('\n'.join(unique_commits)) + print(f"comment parsed.saved to => {save_file}") + + +def remove_same_commit(commit_list): + commit_map = defaultdict() + for line in commit_list: + if '#' not in line: + line = line.lower() + # 先匹配常规的 + match = re.search(r'(?<=\*\s).*?(?=\()', line) + if match: + title = match.group(0).strip() + commit_map[title] = line + else: + # 接下来匹配有模块的 + match2 = re.search(r'[*] [**](.*)[**] ([^:]+): (.*) \((.*)\)', line) + if match2: + message_title = match.group(3).strip() + commit_map[message_title] = line + else: + # 最后处理剩下的 + commit_map[line] = line + + return commit_map.values() + + +if __name__ == "__main__": + parse_changelog() \ No newline at end of file diff --git a/scripts/scriptutils.py b/scripts/scriptutils.py new file mode 100644 index 0000000..3ca1a55 --- /dev/null +++ b/scripts/scriptutils.py @@ -0,0 +1,235 @@ +# Copyright (c) 2023, Terwer . All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. Terwer designates this +# particular file as subject to the "Classpath" exception as provided +# by Terwer in the LICENSE file that accompanied this code. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com +# or visit www.terwer.space if you need additional information or have any +# questions. + +import distutils +import glob +import json +import os +import pathlib +import shutil +import sys +import time +import zipfile +from distutils import dir_util +from distutils import file_util + + +def get_workdir(): + """ + 获取工作空间 + """ + cwd = "./" + if os.getcwd().endswith("scripts"): + cwd = "../" + + # 打印当前python版本 + print("当前python版本:" + sys.version) + # 打印当前路径 + print("当前路径:" + os.path.abspath(cwd)) + + return cwd + + +def switch_workdir(): + """ + 切换工作空间 + """ + # 获取当前工作空间 + cwd = get_workdir() + + print("切换路径") + os.chdir(cwd) + print("当前路径:" + os.getcwd()) + + +def cp_file(f, t): + """ + 拷贝文件 + :param f: 源路径 + :param t: 目的地 + """ + distutils.file_util.copy_file(f, t) + + +def rm_file(filename): + """ + 删除文件 + :param filename:文件名 + """ + if os.path.exists(filename): + os.remove(filename) + + +def mv_file(src, dst): + """ + 移动文件 + :param src: 源文件 + :param dst: 目标文件 + """ + if os.path.exists(dst): + rm_file(dst) + if os.path.exists(src): + file_util.move_file(src, dst) + + +def rm_files(regex): + """ + 正则删除文件 + :param regex: 正则 + """ + file_list = glob.glob(regex) + for file in file_list: + rm_file(file) + + +def cp_folder(src, dst, remove_folder=False): + """ + 拷贝文件夹 + :param src: 源文件夹,例如:"/path/to/source/folder" + :param dst: 目的地,例如:"/path/to/destination/folder" + :param remove_folder: 是否删除文件夹 + """ + if os.path.exists(dst) and remove_folder: + rm_folder(dst) + + if not os.path.exists(dst): + mkdir(dst) + + try: + shutil.copytree(src, dst) + except FileExistsError: + # 如果目标文件夹已经存在,则删除它并重试 + shutil.rmtree(dst) + shutil.copytree(src, dst) + except Exception as e: + print(f"无法拷贝文件夹,{e}") + raise e + + +def mkdir(dirname): + """ + 创建目录 + :param dirname: 目录 + """ + if not os.path.exists(dirname): + distutils.dir_util.mkpath(dirname) + + +def rm_folder(folder): + """ + 删除文件夹,它会递归的删除文件夹中的所有文件和子文件夹 + :param folder: 文件夹 + """ + if os.path.exists(folder): + shutil.rmtree(folder) + + +def read_json_file(filename): + """ + 读取 JSON 文件 + :param filename: 文件名 + """ + # 读取 JSON 文件 + print("读取文件:" + os.path.abspath(filename)) + with open(filename, "r", encoding="utf-8") as f: + data = json.load(f) + return data + + +def write_json_file(filename, data): + """ + 写入 JSON 文件 + :param filename: 文件名 + :param data: JSON 数据 + """ + # 写入 JSON 文件 + with open(filename, "w", encoding="utf-8") as f: + json.dump(data, f, indent=2, ensure_ascii=False) + + +def zip_folder(src_folder, tmp_folder_name, build_zip_path, build_zip_name): + """ + 压缩文件夹为zip + :param src_folder: 需要压缩的文件所在的目录 + :param tmp_folder_name: 临时目录,也是解压后的默认目录 + :param build_zip_path: zip保存目录 + :param build_zip_name: zip文件名称 + """ + mkdir(tmp_folder_name) + cp_folder(src_folder, tmp_folder_name) + + mkdir(build_zip_path) + print("tmp_folder_name:" + tmp_folder_name) + print("build_zip_path:" + build_zip_path) + print("build_zip_name:" + build_zip_name) + + rm_file(build_zip_name) + create_zip(tmp_folder_name, build_zip_name, [], build_zip_path) + rm_folder(tmp_folder_name) + + +def create_zip(root_path, file_name, ignored=[], storage_path=None): + """Create a ZIP + + This function creates a ZIP file of the provided root path. + + Args: + root_path (str): Root path to start from when picking files and directories. + file_name (str): File name to save the created ZIP file as. + ignored (list): A list of files and/or directories that you want to ignore. This + selection is applied in root directory only. + storage_path: If provided, ZIP file will be placed in this location. If None, the + ZIP will be created in root_path + """ + if storage_path is not None: + zip_root = os.path.join(storage_path, file_name) + else: + zip_root = os.path.join(root_path, file_name) + + zipf = zipfile.ZipFile(zip_root, 'w', zipfile.ZIP_STORED) + + def iter_subtree(path, layer=0): + # iter the directory + path = pathlib.Path(path) + for p in path.iterdir(): + if layer == 0 and p.name in ignored: + continue + zipf.write(p, str(p).replace(root_path, '').lstrip('/')) + + if p.is_dir(): + iter_subtree(p, layer=layer + 1) + + iter_subtree(root_path) + zipf.close() + + +def get_filename_from_time(): + """ + 根据时间命名文件 + :return: 根据时间生成的名称 + """ + # 获取当前的时间 + now_time = time.localtime() + # 使用strftime函数把时间转换成想要的格式 + filename = time.strftime("%Y%m%d%H%M%S", now_time) # 输出结果为:20210126095555 + return filename diff --git a/scripts/version.py b/scripts/version.py new file mode 100644 index 0000000..8096589 --- /dev/null +++ b/scripts/version.py @@ -0,0 +1,71 @@ +# Copyright (c) 2023, Terwer . All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. Terwer designates this +# particular file as subject to the "Classpath" exception as provided +# by Terwer in the LICENSE file that accompanied this code. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com +# or visit www.terwer.space if you need additional information or have any +# questions. + +import argparse + +import scriptutils + + +def parse_json(filename, version_field, new_version): + """ + 解析json文件,并修改版本号未指定的值 + :param filename: 文件路径 + :param version_field: 版本号字段 + :param new_version: 版本号 + """ + + # 读取 JSON 文件 + data = scriptutils.read_json_file(filename) + + pkg = scriptutils.read_json_file(cwd + "package.json") + print(f'new_version=>{new_version}') + print(f'pkgv=>{pkg["version"]}') + if new_version is None: + new_version = pkg["version"] + + # 修改 JSON 文件中的属性 + if data[version_field] == new_version: + print("版本号已经是最新,无需修改") + return + data[version_field] = new_version + + # 将修改后的 JSON 写回到文件中 + scriptutils.write_json_file(filename, data) + print(f"修改 {filename} 完毕,新版本为:" + new_version) + + +if __name__ == "__main__": + # 获取当前工作空间 + cwd = scriptutils.get_workdir() + + # 参数解析 + parser = argparse.ArgumentParser() + parser.add_argument("--version", help="the file to be processed") + parser.add_argument("-v", "--verbose", action="store_true", help="enable verbose output") + args = parser.parse_args() + + if args.verbose: + print("Verbose mode enabled") + + # plugin.json + parse_json(cwd + "plugin.json", "version", args.version) \ No newline at end of file diff --git a/tools/.gitkeep b/tools/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tools/changelogParser.ts b/tools/changelogParser.ts deleted file mode 100644 index 8d19820..0000000 --- a/tools/changelogParser.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { ChangelogParser } from "@terwer/changelog-parser" - -const changelogParser = new ChangelogParser() -changelogParser.parseChangelog(process.cwd(), false) diff --git a/tools/packageApp.ts b/tools/packageApp.ts deleted file mode 100644 index 353f53a..0000000 --- a/tools/packageApp.ts +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2023, Terwer . All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Terwer designates this - * particular file as subject to the "Classpath" exception as provided - * by Terwer in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com - * or visit www.terwer.space if you need additional information or have any - * questions. - */ - -import minimist from "minimist" -import FileUtils from "./utils/fileUtils" - -/** - * 插件打包 - * - * @author terwer - * @version 1.0.0 - * @since 1.0.0 - */ -class PackageApp { - public async packagePlugin(isTest: boolean) { - // zip to build/package.zip etc. - const zipTo = "./build/package.zip" - await FileUtils.makeZip("./dist", zipTo) - console.log(`plugin packaged to ${zipTo}`) - - // 开发测试阶段,拷贝到插件目录 - if (isTest) { - // copy to local plugin folder - await FileUtils.cp( - "./dist", - "/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/plugins/siyuan-importer", - true, - true - ) - } - } -} - -// 本地生产测试 -// pnpm package -t -// -// 生产环境打包 -// pnpm package -;(async () => { - const args = minimist(process.argv.slice(2)) - const isTest = args.test || args.t || false - - const packageApp = new PackageApp() - // plugin - await packageApp.packagePlugin(isTest) - console.log("app packaged.") -})() diff --git a/tools/syncVersion.ts b/tools/syncVersion.ts deleted file mode 100644 index 88c7d38..0000000 --- a/tools/syncVersion.ts +++ /dev/null @@ -1,47 +0,0 @@ -import fs from "fs" -import minimist from "minimist" - -class SyncVersion { - /** - * 同步根目录的版本号到指定指定文件 - * - * @param filename - 问号路径,相对于根目录 - * @param versionField - 版本字段名 - * @param newVersion - 可选,传递之后将使用传递的版本号,不使用默认的 package.json 版本号 - */ - async parseJSON(filename: string, versionField: string, newVersion: string | undefined): Promise { - try { - // Read JSON from file - const rawData = await fs.promises.readFile(filename) - const data = JSON.parse(rawData.toString()) - - // Modify version field in JSON data - const pkgData = await fs.promises.readFile("package.json") - const pkg = JSON.parse(pkgData.toString()) - if (!newVersion) { - newVersion = pkg["version"] - } - - if (data[versionField] === newVersion) { - console.log("版本号已经是最新,无需修改") - return - } - - data[versionField] = newVersion - - // Write modified JSON back to file - await fs.promises.writeFile(filename, JSON.stringify(data, null, 2)) - console.log(`${filename} 修改完毕,新版本为:${newVersion}`) - } catch (err) { - console.error(`Error parsing JSON file: ${err}`) - } - } -} - -;(async () => { - const args = minimist(process.argv.slice(2)) - const newVersion = args.version || args.v || undefined - - const syncVersion = new SyncVersion() - await syncVersion.parseJSON("public/plugin.json", "version", newVersion) -})() diff --git a/tools/utils/fileUtils.ts b/tools/utils/fileUtils.ts deleted file mode 100644 index e6bd61b..0000000 --- a/tools/utils/fileUtils.ts +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2023, Terwer . All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Terwer designates this - * particular file as subject to the "Classpath" exception as provided - * by Terwer in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com - * or visit www.terwer.space if you need additional information or have any - * questions. - */ - -import fs from "fs" -import path from "path" -import glob from "glob" -import archiver from "archiver" - -class FileUtils { - static async cp(sourcePath: string, destinationPath: string, r = false, f = false) { - if (!fs.existsSync(sourcePath)) { - throw `文件或目录不存在:${sourcePath}` - } - - if (fs.statSync(sourcePath).isFile()) { - throw `${sourcePath} 不是一个目录` - } - - if (!fs.existsSync(destinationPath)) { - fs.mkdirSync(destinationPath, { recursive: true }) - } - - let pattern = `${sourcePath}/*` - if (r) { - pattern = `${sourcePath}/**/*` - } - - let count = 0 - const files = glob.sync(pattern, { nodir: true }) - for (const file of files) { - const dest = path.join(destinationPath, file.replace(sourcePath, "")) - if (!fs.existsSync(path.dirname(dest))) { - fs.mkdirSync(path.dirname(dest), { recursive: true }) - } - - if (!f && fs.existsSync(dest)) { - throw `目标文件或目录已经存在: ${dest}` - } - - try { - fs.copyFileSync(file, dest) - ++count - } catch (err) { - throw `复制文件时出错:${file} => ${dest}` - } - } - - console.log(`从 ${sourcePath} 复制 ${count} 个文件到 ${destinationPath}`) - } - - public static async makeZip(source: string, destination: string): Promise { - return new Promise((resolve, reject) => { - const archive = archiver("zip", { zlib: { level: 9 } }) - const output = fs.createWriteStream(destination) - - output.on("close", () => { - resolve(destination) - }) - - archive.on("warning", (err: archiver.ArchiverError) => { - if (err.code === "ENOENT") { - console.warn(err) - } else { - reject(err) - } - }) - - archive.on("error", (err: archiver.ArchiverError) => { - reject(err) - }) - - archive.pipe(output) - archive.directory(source, "/") - archive.finalize() - }).then((destination: string) => { - return new Promise((resolve, reject) => { - const targetDirectory = path.dirname(destination) - - fs.promises - .mkdir(targetDirectory, { recursive: true }) - .then(() => { - resolve(destination) - }) - .catch((err) => { - reject(err) - }) - }) - }) - } -} - -// 示例用法 -// ;(async function () { -// try { -// await FileUtils.cp("./test", "./test-copy", true, true) -// } catch (error) { -// console.error(`执行命令出错: ${error}`) -// } -// })() - -export default FileUtils diff --git a/vite.config.ts b/vite.config.ts index 8da1101..4c2bc02 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -25,6 +25,18 @@ export default defineConfig({ src: "./README*.md", dest: "./", }, + { + src: "./icon.png", + dest: "./", + }, + { + src: "./preview.png", + dest: "./", + }, + { + src: "./plugin.json", + dest: "./", + }, ], }), ],