From 71f9b4af1b9a259a89ecd8f8d16f2e688e4e27d0 Mon Sep 17 00:00:00 2001 From: Neko Ayaka Date: Sat, 15 Mar 2025 23:24:41 +0800 Subject: [PATCH] refactor: migrate unplugin-live2d-sdk & unplugin-download to https://github.com/proj-airi --- apps/stage-tamagotchi/package.json | 4 +- apps/stage-web/package.json | 4 +- packages/unplugin-download/package.json | 46 ---------- packages/unplugin-download/src/utils/fs.ts | 25 ------ packages/unplugin-download/src/utils/index.ts | 1 - packages/unplugin-download/src/vite/index.ts | 52 ----------- packages/unplugin-download/tsconfig.json | 18 ---- packages/unplugin-live2d-sdk/package.json | 47 ---------- .../unplugin-live2d-sdk/src/utils/errors.ts | 38 -------- packages/unplugin-live2d-sdk/src/utils/fs.ts | 25 ------ .../unplugin-live2d-sdk/src/utils/index.ts | 3 - .../unplugin-live2d-sdk/src/utils/unzip.ts | 89 ------------------- .../unplugin-live2d-sdk/src/vite/index.ts | 44 --------- packages/unplugin-live2d-sdk/tsconfig.json | 18 ---- pnpm-lock.yaml | 82 ++++++++++------- 15 files changed, 55 insertions(+), 441 deletions(-) delete mode 100644 packages/unplugin-download/package.json delete mode 100644 packages/unplugin-download/src/utils/fs.ts delete mode 100644 packages/unplugin-download/src/utils/index.ts delete mode 100644 packages/unplugin-download/src/vite/index.ts delete mode 100644 packages/unplugin-download/tsconfig.json delete mode 100644 packages/unplugin-live2d-sdk/package.json delete mode 100644 packages/unplugin-live2d-sdk/src/utils/errors.ts delete mode 100644 packages/unplugin-live2d-sdk/src/utils/fs.ts delete mode 100644 packages/unplugin-live2d-sdk/src/utils/index.ts delete mode 100644 packages/unplugin-live2d-sdk/src/utils/unzip.ts delete mode 100644 packages/unplugin-live2d-sdk/src/vite/index.ts delete mode 100644 packages/unplugin-live2d-sdk/tsconfig.json diff --git a/apps/stage-tamagotchi/package.json b/apps/stage-tamagotchi/package.json index 635dd3f1e..1da8c91b3 100644 --- a/apps/stage-tamagotchi/package.json +++ b/apps/stage-tamagotchi/package.json @@ -105,8 +105,8 @@ "@proj-airi/lobe-icons": "^0.3.6", "@proj-airi/provider-transformers": "workspace:^", "@proj-airi/ui-transitions": "workspace:^", - "@proj-airi/unplugin-download": "workspace:^", - "@proj-airi/unplugin-live2d-sdk": "workspace:^", + "@proj-airi/unplugin-fetch": "^0.1.4", + "@proj-airi/unplugin-live2d-sdk": "^0.1.2", "@shikijs/markdown-it": "^3.2.1", "@types/culori": "^2.1.1", "@types/markdown-it-link-attributes": "^3.0.5", diff --git a/apps/stage-web/package.json b/apps/stage-web/package.json index 8aeff138a..ab4cc4d66 100644 --- a/apps/stage-web/package.json +++ b/apps/stage-web/package.json @@ -101,8 +101,8 @@ "@proj-airi/lobe-icons": "^0.3.6", "@proj-airi/provider-transformers": "workspace:^", "@proj-airi/ui-transitions": "workspace:^", - "@proj-airi/unplugin-download": "workspace:^", - "@proj-airi/unplugin-live2d-sdk": "workspace:^", + "@proj-airi/unplugin-fetch": "^0.1.4", + "@proj-airi/unplugin-live2d-sdk": "^0.1.2", "@shikijs/markdown-it": "^3.2.1", "@types/culori": "^2.1.1", "@types/markdown-it-link-attributes": "^3.0.5", diff --git a/packages/unplugin-download/package.json b/packages/unplugin-download/package.json deleted file mode 100644 index d78351260..000000000 --- a/packages/unplugin-download/package.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "name": "@proj-airi/unplugin-download", - "type": "module", - "private": true, - "description": "Vite plugin to download files", - "author": { - "name": "Neko Ayaka", - "email": "neko@ayaka.moe", - "url": "https://github.com/nekomeowww" - }, - "license": "MIT", - "repository": { - "type": "git", - "url": "https://github.com/moeru-ai/airi.git", - "directory": "packages/unplugin-download" - }, - "exports": { - ".": { - "types": "./dist/vite/index.d.ts", - "import": "./dist/vite/index.mjs", - "require": "./dist/vite/index.cjs" - }, - "./vite": { - "types": "./dist/vite/index.d.ts", - "import": "./dist/vite/index.mjs", - "node": "./dist/vite/index.cjs" - } - }, - "main": "./dist/vite/index.cjs", - "module": "./dist/vite/index.mjs", - "types": "./dist/vite/index.d.ts", - "files": [ - "README.md", - "dist", - "package.json" - ], - "scripts": { - "dev": "pnpm run stub", - "stub": "unbuild --stub", - "build": "unbuild" - }, - "dependencies": { - "ofetch": "^1.4.1", - "vite": "^6.2.2" - } -} diff --git a/packages/unplugin-download/src/utils/fs.ts b/packages/unplugin-download/src/utils/fs.ts deleted file mode 100644 index 808bf6190..000000000 --- a/packages/unplugin-download/src/utils/fs.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { stat } from 'node:fs/promises' - -export async function exists(path: string) { - try { - await stat(path) - return true - } - catch (error) { - if (isENOENTError(error)) - return false - - throw error - } -} - -export function isENOENTError(error: unknown): boolean { - if (!(error instanceof Error)) - return false - if (!('code' in error)) - return false - if (error.code !== 'ENOENT') - return false - - return true -} diff --git a/packages/unplugin-download/src/utils/index.ts b/packages/unplugin-download/src/utils/index.ts deleted file mode 100644 index 668241973..000000000 --- a/packages/unplugin-download/src/utils/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './fs' diff --git a/packages/unplugin-download/src/vite/index.ts b/packages/unplugin-download/src/vite/index.ts deleted file mode 100644 index db206ff4d..000000000 --- a/packages/unplugin-download/src/vite/index.ts +++ /dev/null @@ -1,52 +0,0 @@ -import type { Plugin } from 'vite' - -import { Buffer } from 'node:buffer' -import { copyFile, mkdir, writeFile } from 'node:fs/promises' -import { join, resolve } from 'node:path' -import { ofetch } from 'ofetch' -import { createLogger } from 'vite' - -import { exists } from '../utils' - -export function Download( - url: string, - filename: string, - destination: string, -): Plugin { - return { - name: `download-${filename}`, - async configResolved(config) { - const logger = createLogger() - - const cacheDir = resolve(join(config.root, '.cache')) - const publicDir = resolve(join(config.root, 'public')) - - try { - // cache - if (await exists(resolve(join(cacheDir, destination, filename)))) { - return - } - - logger.info(`Downloading ${filename}...`) - const stream = await ofetch(url, { responseType: 'arrayBuffer' }) - await mkdir(join(cacheDir, destination), { recursive: true }) - await writeFile(join(cacheDir, destination, filename), Buffer.from(stream)) - - logger.info(`${filename} downloaded.`) - - if (await exists(resolve(join(publicDir, destination, filename)))) { - return - } - - await mkdir(join(publicDir, destination), { recursive: true }).catch(() => { }) - await copyFile(join(cacheDir, destination, filename), join(publicDir, destination, filename)) - - // TODO: use motion editor to remap emotions - } - catch (err) { - console.error(err) - throw err - } - }, - } -} diff --git a/packages/unplugin-download/tsconfig.json b/packages/unplugin-download/tsconfig.json deleted file mode 100644 index 00dcfd807..000000000 --- a/packages/unplugin-download/tsconfig.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "lib": [ - "ESNext" - ], - "module": "ESNext", - "moduleResolution": "bundler", - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "isolatedModules": true, - "verbatimModuleSyntax": true, - "skipLibCheck": true - }, - "include": [ - "src/**/*.ts" - ] -} diff --git a/packages/unplugin-live2d-sdk/package.json b/packages/unplugin-live2d-sdk/package.json deleted file mode 100644 index 6e9394edf..000000000 --- a/packages/unplugin-live2d-sdk/package.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "name": "@proj-airi/unplugin-live2d-sdk", - "type": "module", - "private": true, - "description": "Vite plugin to download Live2D SDK", - "author": { - "name": "Neko Ayaka", - "email": "neko@ayaka.moe", - "url": "https://github.com/nekomeowww" - }, - "license": "MIT", - "repository": { - "type": "git", - "url": "https://github.com/moeru-ai/airi.git", - "directory": "packages/unplugin-live2d-sdk" - }, - "exports": { - ".": { - "types": "./dist/vite/index.d.ts", - "import": "./dist/vite/index.mjs", - "require": "./dist/vite/index.cjs" - }, - "./vite": { - "types": "./dist/vite/index.d.ts", - "import": "./dist/vite/index.mjs", - "node": "./dist/vite/index.cjs" - } - }, - "main": "./dist/vite/index.cjs", - "module": "./dist/vite/index.mjs", - "types": "./dist/vite/index.d.ts", - "files": [ - "README.md", - "dist", - "package.json" - ], - "scripts": { - "dev": "pnpm run stub", - "stub": "unbuild --stub", - "build": "unbuild" - }, - "dependencies": { - "ofetch": "^1.4.1", - "vite": "^6.2.2", - "yauzl": "^3.2.0" - } -} diff --git a/packages/unplugin-live2d-sdk/src/utils/errors.ts b/packages/unplugin-live2d-sdk/src/utils/errors.ts deleted file mode 100644 index 633a90604..000000000 --- a/packages/unplugin-live2d-sdk/src/utils/errors.ts +++ /dev/null @@ -1,38 +0,0 @@ -export function rejectIfError(error: E | undefined, reject: (error?: E) => void, handler?: (error?: E) => void) { - if (error) { - reject(error) - !!handler && handler(error) - } -} - -export function resolveWhenNoError(reject: (error?: E) => void, resolve: (result?: R) => void) { - return (err?: E) => { - if (err) { - reject(err) - } - else { - resolve() - } - } -} - -export function onError(reject: (error?: E) => void, handler?: (error?: E) => void) { - return (error?: E) => rejectIfError(error, reject, handler) -} - -export function noError< - T, - U extends unknown[], - E = unknown, ->( - reject: (err?: E) => void, - fn: (...args: U) => T, -): (err: E | undefined, ...args: U) => T | undefined { - return (err, ...args) => { - if (err) { - rejectIfError(err, reject) - return - } - return fn(...args) - } -} diff --git a/packages/unplugin-live2d-sdk/src/utils/fs.ts b/packages/unplugin-live2d-sdk/src/utils/fs.ts deleted file mode 100644 index 808bf6190..000000000 --- a/packages/unplugin-live2d-sdk/src/utils/fs.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { stat } from 'node:fs/promises' - -export async function exists(path: string) { - try { - await stat(path) - return true - } - catch (error) { - if (isENOENTError(error)) - return false - - throw error - } -} - -export function isENOENTError(error: unknown): boolean { - if (!(error instanceof Error)) - return false - if (!('code' in error)) - return false - if (error.code !== 'ENOENT') - return false - - return true -} diff --git a/packages/unplugin-live2d-sdk/src/utils/index.ts b/packages/unplugin-live2d-sdk/src/utils/index.ts deleted file mode 100644 index 72cfd94dc..000000000 --- a/packages/unplugin-live2d-sdk/src/utils/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './errors' -export * from './fs' -export * from './unzip' diff --git a/packages/unplugin-live2d-sdk/src/utils/unzip.ts b/packages/unplugin-live2d-sdk/src/utils/unzip.ts deleted file mode 100644 index 45fb7f470..000000000 --- a/packages/unplugin-live2d-sdk/src/utils/unzip.ts +++ /dev/null @@ -1,89 +0,0 @@ -import type { Buffer } from 'node:buffer' - -import { createWriteStream, existsSync, mkdirSync } from 'node:fs' -import { dirname, join } from 'node:path' -import { fromBuffer } from 'yauzl' - -import { noError, onError } from './errors' - -/** - * Example: - * - * await unzip("./tim.zip", "./"); - * - * Will create directories: - * - * ./tim.zip - * ./tim - * - * Originally by [How to unzip to a folder using yauzl? - Stack Overflow](https://stackoverflow.com/questions/63932027/how-to-unzip-to-a-folder-using-yauzl) - * - * @param buffer Buffer of the zip file. - * @param target Path to the folder where the zip folder will be put. - */ -export async function unzip(buffer: Buffer, target: string) { - return new Promise((resolve, reject) => { - let pendingWrites = 0 - let isReadingComplete = false - - function tryResolve() { - if (isReadingComplete && pendingWrites === 0) { - resolve() - } - } - - fromBuffer(buffer, { lazyEntries: true }, noError(reject, (zipFile) => { - // This is the key. We start by reading the first entry. - zipFile.readEntry() - - // Now for every entry, we will write a file or dir - // to disk. Then call zipFile.readEntry() again to - // trigger the next cycle. - zipFile.on('entry', (entry) => { - // Directories - if (/\/$/.test(entry.fileName)) { - // Create the directory then read the next entry. - mkdirSync(join(target, entry.fileName), { recursive: true }) - zipFile.readEntry() - - return - } - - // Files - const dir = dirname(join(target, entry.fileName)) - if (!existsSync(dir)) { - mkdirSync(dir, { recursive: true }) - } - - // Write the file to disk. - pendingWrites++ - zipFile.openReadStream(entry, noError(reject, (readStream) => { - const file = createWriteStream(join(target, entry.fileName)) - readStream.pipe(file) - - // Handle errors - file.on('error', (err) => { - pendingWrites-- - zipFile.close() - reject(err) - }) - - // Wait until the file is finished writing, then read the next entry. - file.on('finish', () => { - file.close(() => { - pendingWrites-- - tryResolve() - zipFile.readEntry() - }) - }) - })) - }) - - zipFile.on('error', onError(reject, zipFile.close)) - zipFile.on('end', noError(reject, () => { - isReadingComplete = true - tryResolve() - })) - })) - }) -} diff --git a/packages/unplugin-live2d-sdk/src/vite/index.ts b/packages/unplugin-live2d-sdk/src/vite/index.ts deleted file mode 100644 index 84d131d41..000000000 --- a/packages/unplugin-live2d-sdk/src/vite/index.ts +++ /dev/null @@ -1,44 +0,0 @@ -import type { Plugin } from 'vite' - -import { Buffer } from 'node:buffer' -import { copyFile, mkdir } from 'node:fs/promises' -import { join, resolve } from 'node:path' -import { ofetch } from 'ofetch' -import { createLogger } from 'vite' - -import { exists, unzip } from '../utils' - -export function DownloadLive2DSDK(): Plugin { - return { - name: `download-live2d-cubism-sdk`, - async configResolved(config) { - const logger = createLogger() - - const cacheDir = resolve(join(config.root, '.cache')) - const publicDir = resolve(join(config.root, 'public')) - - try { - // cache - if (!(await exists(resolve(join(cacheDir, 'assets/js/CubismSdkForWeb-5-r.1'))))) { - logger.info('Downloading Cubism SDK...') - const stream = await ofetch('https://dist.ayaka.moe/npm/live2d-cubism/CubismSdkForWeb-5-r.1.zip', { responseType: 'arrayBuffer' }) - - logger.info('Unzipping Cubism SDK...') - await mkdir(join(cacheDir, 'assets/js'), { recursive: true }) - await unzip(Buffer.from(stream), join(cacheDir, 'assets/js')) - - logger.info('Cubism SDK downloaded and unzipped.') - } - - if (!(await exists(resolve(join(publicDir, 'assets/js/CubismSdkForWeb-5-r.1'))))) { - await mkdir(join(publicDir, 'assets/js/CubismSdkForWeb-5-r.1/Core'), { recursive: true }).catch(() => {}) - await copyFile(join(cacheDir, 'assets/js/CubismSdkForWeb-5-r.1/Core/live2dcubismcore.min.js'), join(publicDir, 'assets/js/CubismSdkForWeb-5-r.1/Core/live2dcubismcore.min.js')) - } - } - catch (err) { - console.error(err) - throw err - } - }, - } -} diff --git a/packages/unplugin-live2d-sdk/tsconfig.json b/packages/unplugin-live2d-sdk/tsconfig.json deleted file mode 100644 index 00dcfd807..000000000 --- a/packages/unplugin-live2d-sdk/tsconfig.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "lib": [ - "ESNext" - ], - "module": "ESNext", - "moduleResolution": "bundler", - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "isolatedModules": true, - "verbatimModuleSyntax": true, - "skipLibCheck": true - }, - "include": [ - "src/**/*.ts" - ] -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 005c4be87..48976c30c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -385,12 +385,12 @@ importers: '@proj-airi/ui-transitions': specifier: workspace:^ version: link:../../packages/ui-transitions - '@proj-airi/unplugin-download': - specifier: workspace:^ - version: link:../../packages/unplugin-download + '@proj-airi/unplugin-fetch': + specifier: ^0.1.4 + version: 0.1.4(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) '@proj-airi/unplugin-live2d-sdk': - specifier: workspace:^ - version: link:../../packages/unplugin-live2d-sdk + specifier: ^0.1.2 + version: 0.1.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) '@shikijs/markdown-it': specifier: ^3.2.1 version: 3.2.1 @@ -719,12 +719,12 @@ importers: '@proj-airi/ui-transitions': specifier: workspace:^ version: link:../../packages/ui-transitions - '@proj-airi/unplugin-download': - specifier: workspace:^ - version: link:../../packages/unplugin-download + '@proj-airi/unplugin-fetch': + specifier: ^0.1.4 + version: 0.1.4(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) '@proj-airi/unplugin-live2d-sdk': - specifier: workspace:^ - version: link:../../packages/unplugin-live2d-sdk + specifier: ^0.1.2 + version: 0.1.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) '@shikijs/markdown-it': specifier: ^3.2.1 version: 3.2.1 @@ -1112,27 +1112,6 @@ importers: specifier: ^2.2.8 version: 2.2.8(typescript@5.8.2) - packages/unplugin-download: - dependencies: - ofetch: - specifier: ^1.4.1 - version: 1.4.1 - vite: - specifier: ^6.2.2 - version: 6.2.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) - - packages/unplugin-live2d-sdk: - dependencies: - ofetch: - specifier: ^1.4.1 - version: 1.4.1 - vite: - specifier: ^6.2.2 - version: 6.2.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) - yauzl: - specifier: ^3.2.0 - version: 3.2.0 - packages/utils-transformers: dependencies: '@huggingface/transformers': @@ -4069,6 +4048,12 @@ packages: '@proj-airi/server-shared@0.3.6': resolution: {integrity: sha512-VVGhVHDrZojH2RQIC6w4znmA/QlWrxFL3UI1N/pQU20vpRN23r4NaG/XvxdlzxQOYuXkyHvdc6/gn1b+qJnBpA==} + '@proj-airi/unplugin-fetch@0.1.4': + resolution: {integrity: sha512-COL9bzbT9wNv+yyueIvC6tcd3EuiAHMuMGpZNJlCn1qsThpi5mIvak2KZH5HYiDo7wusDVLBnye1fG92r1hYTA==} + + '@proj-airi/unplugin-live2d-sdk@0.1.2': + resolution: {integrity: sha512-zmpjfQ64le+NTzWrM7eXIVUtSicsyYDuJeYBXPK8Z0eW3S7oMoGHrlcm0oPqPROpRoem4PHgHLhB6T1LLQqitw==} + '@protobufjs/aspromise@1.1.2': resolution: {integrity: sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==} @@ -14591,6 +14576,41 @@ snapshots: dependencies: crossws: 0.3.4 + '@proj-airi/unplugin-fetch@0.1.4(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0)': + dependencies: + ofetch: 1.4.1 + vite: 6.2.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) + transitivePeerDependencies: + - '@types/node' + - jiti + - less + - lightningcss + - sass + - sass-embedded + - stylus + - sugarss + - terser + - tsx + - yaml + + '@proj-airi/unplugin-live2d-sdk@0.1.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0)': + dependencies: + ofetch: 1.4.1 + vite: 6.2.2(@types/node@22.13.10)(jiti@2.4.2)(less@4.2.2)(terser@5.17.6)(tsx@4.19.3)(yaml@2.7.0) + yauzl: 3.2.0 + transitivePeerDependencies: + - '@types/node' + - jiti + - less + - lightningcss + - sass + - sass-embedded + - stylus + - sugarss + - terser + - tsx + - yaml + '@protobufjs/aspromise@1.1.2': {} '@protobufjs/base64@1.1.2': {}