diff --git a/scripts/build/library.ts b/scripts/build/library.ts index e3a5f815c..78f7fee33 100644 --- a/scripts/build/library.ts +++ b/scripts/build/library.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-loop-func */ /* eslint-disable max-depth */ /* eslint-disable complexity */ import { URL } from 'url'; @@ -15,7 +16,6 @@ import chalk from 'chalk'; import type { ITiddlerFields } from 'tiddlywiki'; import { - shell, tiddlywiki, mkdirsForFileSync, findFirstOne, @@ -46,10 +46,10 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => { mkdirsForFileSync(resolve(cachePluginsDir, 'foo')); } - // 启动TW + // 启动 TW const $tw = tiddlywiki(); - // 拷贝本地插件(未在网络上发布的) cp plugin_files/* ${distDir}/tmp/ + // 拷贝本地插件 (未在网络上发布的) cp plugin_files/* ${distDir}/tmp/ const pluginFilesDir = resolve('plugin_files'); for (const file of readdirSync(pluginFilesDir)) { const p = resolve(pluginFilesDir, file); @@ -68,7 +68,7 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => { for (const title of pluginInfoTiddlerTitles) { try { const tiddler = $tw.wiki.getTiddler(title)!.fields; - // 应当有title + // 应当有 title if ( !tiddler['cpl.title'] || (tiddler['cpl.title'] as string).trim() === '' @@ -79,7 +79,7 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => { continue; } const title_ = tiddler['cpl.title'] as string; - // 带有uri,需要下载下来,但是需要是tw支持的格式 + // 带有 uri,需要下载下来,但是需要是 tw 支持的格式 if ( !tiddler['cpl.uri'] || (tiddler['cpl.uri'] as string).trim() === '' @@ -111,9 +111,26 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => { copyFileSync(downloadFileMap[url.href], filePath); } else { try { - shell( - `wget "${url.href}" --no-verbose --force-directories --no-check-certificate -O "${filePath}"`, - ); + fetch(url.href) + .then(response => { + if (!response.ok) { + throw new Error( + `Network response was not ok: ${response.statusText}`, + ); + } + return response.arrayBuffer(); + }) + .then(data => { + // 处理获取的数据,这里的例子是保存到文件 + const blob = new Blob([data]); + const a = document.createElement('a'); + a.href = window.URL.createObjectURL(blob); + a.download = filePath; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + }); + downloadFileMap[url.href] = filePath; } catch (e) { failedPlugins[title_] = `404 not found: ${url.href}`; @@ -193,7 +210,7 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => { ); }; - // 接下来从tmpDir处理所有的插件 + // 接下来从 tmpDir 处理所有的插件 const pluginTitlePathMap: Record = {}; const pluginInfos: ReturnType['newInfoTiddler'][] = []; @@ -318,7 +335,7 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => { ); } - // 生成插件源HTML文件 + // 生成插件源 HTML 文件 console.log(chalk.bgCyan.black.bold('\nGenerating plugin library file...')); writeFileSync(