Skip to content

Commit

Permalink
feat: use fetch instead of wget #175
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Jan 17, 2024
1 parent ff2cfa6 commit 5bae81d
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions scripts/build/library.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-loop-func */
/* eslint-disable max-depth */
/* eslint-disable complexity */
import { URL } from 'url';
Expand All @@ -15,7 +16,6 @@ import chalk from 'chalk';
import type { ITiddlerFields } from 'tiddlywiki';

import {
shell,
tiddlywiki,
mkdirsForFileSync,
findFirstOne,
Expand Down Expand Up @@ -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);
Expand All @@ -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() === ''
Expand All @@ -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() === ''
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -193,7 +210,7 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => {
);
};

// 接下来从tmpDir处理所有的插件
// 接下来从 tmpDir 处理所有的插件
const pluginTitlePathMap: Record<string, string> = {};
const pluginInfos: ReturnType<typeof mergePluginInfo>['newInfoTiddler'][] =
[];
Expand Down Expand Up @@ -318,7 +335,7 @@ export const buildLibrary = (distDir = defaultDistDir, cache = false) => {
);
}

// 生成插件源HTML文件
// 生成插件源 HTML 文件
console.log(chalk.bgCyan.black.bold('\nGenerating plugin library file...'));

writeFileSync(
Expand Down

0 comments on commit 5bae81d

Please sign in to comment.