Skip to content

Commit 363bddb

Browse files
authored
refactor(thumbnail): add one more thumbnail file (#103)
1 parent 3a16ffb commit 363bddb

File tree

11 files changed

+68
-21
lines changed

11 files changed

+68
-21
lines changed

src/bin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ program.command("thumbnail")
101101
.argument("[dir]", "The workdir directory", ".")
102102
.option("-s --search-dir <searchDir>", STORE_DIR_DESC)
103103
.action(async (dir, options) => {
104-
const thumbnailPath = await thumbnail(path.resolve(dir), getStoreDir(options.searchDir));
105-
if (thumbnailPath) {
104+
const thumbnailPaths = await thumbnail(path.resolve(dir), getStoreDir(options.searchDir));
105+
if (thumbnailPaths.length) {
106106
// eslint-disable-next-line no-console
107107
console.log("Thumbnail generated successfully.");
108108
}

src/cmd/install.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Dep, DepRaw, Deps, IDepMap, InstallAllResult, InstallFileResult, I
22
import path from "node:path";
33
import { execa } from "execa";
44
import pLimit from "p-limit";
5-
import { ooPackageName, ooThumbnailName } from "../const";
5+
import { ooPackageName, ooThumbnailNames } from "../const";
66
import { copyDir, exists, mkdir, move, remove, tempDir, walk, xTar } from "../utils/fs";
77
import { env, normalizePackageName } from "../utils/misc";
88
import {
@@ -82,7 +82,9 @@ export async function installFile(options: InstallFileOptions): Promise<InstallF
8282
}
8383
await mkdir(path.dirname(targetDir));
8484
await move(tempDir, targetDir);
85-
await remove(path.join(targetDir, ooThumbnailName));
85+
for (const ooThumbnailName of ooThumbnailNames) {
86+
await remove(path.join(targetDir, ooThumbnailName));
87+
}
8688
return {
8789
target: targetDir,
8890
isOverwrite: isExists,
@@ -356,7 +358,9 @@ async function _install(options: _InstallOptions): Promise<InstallPackageResult[
356358
}
357359

358360
function installFilter(src: string, _dest: string, source: string, _destination: string): boolean {
359-
return path.join(src, ooThumbnailName) !== source;
361+
return ooThumbnailNames.every((ooThumbnailName) => {
362+
return path.join(src, ooThumbnailName) !== source;
363+
});
360364
}
361365

362366
async function integrityCheck(options: _InstallOptions, tempDistDir: string): Promise<SearchDep[]> {

src/cmd/pack.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import { execa } from "execa";
33
import { globby } from "globby";
4+
import { ooThumbnailNames } from "../const";
45
import { copyDir, remove, tempDir, writeFile } from "../utils/fs";
56
import { env } from "../utils/misc";
67
import { generatePackageJson } from "../utils/npm";
@@ -26,7 +27,7 @@ export async function prePack(p: string, ignore: string[]) {
2627
absolute: true,
2728
});
2829

29-
const alwaysAllow = [path.join(p, ".oo-thumbnail.json")];
30+
const alwaysAllow = ooThumbnailNames.map(name => path.join(p, name));
3031

3132
await Promise.all([
3233
copyDir(p, path.join(workdir, "package"), (src, _dest, source, _destination) => {

src/cmd/thumbnail.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import path from "node:path";
2-
import { ooThumbnailName } from "../const";
2+
import { ooLanguages, ooThumbnailNames } from "../const";
33
import { writeFile } from "../utils/fs";
44
import { Thumbnail } from "../utils/thumbnail";
55

6-
export async function thumbnail(workdir: string, searchDir: string): Promise<string | undefined> {
7-
const thumbnail = await Thumbnail.create(workdir, searchDir);
6+
export async function thumbnail(workdir: string, searchDir: string): Promise<string[]> {
7+
const result: string[] = [];
8+
for (let i = 0; i < ooLanguages.length; i++) {
9+
const lang = ooLanguages[i];
10+
const ooThumbnailName = ooThumbnailNames[i];
11+
const thumbnailPath = await thumbnailInternal(workdir, searchDir, lang, ooThumbnailName);
12+
if (thumbnailPath) {
13+
result.push(thumbnailPath);
14+
}
15+
}
16+
17+
Thumbnail.clearCache();
18+
return result;
19+
}
20+
21+
async function thumbnailInternal(workdir: string, searchDir: string, lang: string, ooThumbnailName: string): Promise<string | undefined> {
22+
const thumbnail = await Thumbnail.create(workdir, searchDir, lang);
823
if (!thumbnail) {
924
return;
1025
}

src/const.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export const ooPackageName = "package.oo.yaml";
22
export const ooThumbnailName = ".oo-thumbnail.json";
3+
export const ooLanguages = ["en", "zh-CN"];
4+
export const ooThumbnailNames = /* #__PURE__ */ ooLanguages.map((lang) => {
5+
return lang === "en" ? ooThumbnailName : ooThumbnailName.replace(".json", `.${lang}.json`);
6+
});
37

48
export const ERR_OOPM_DEPEND_ITSELF = new Error("ERR_OOPM_DEPEND_ITSELF: Not allowed to depend on itself");

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ export {
2525
export {
2626
thumbnail,
2727
} from "./cmd/thumbnail";
28-
export { ERR_OOPM_DEPEND_ITSELF, ooPackageName, ooThumbnailName } from "./const";
28+
export { ERR_OOPM_DEPEND_ITSELF, ooPackageName, ooThumbnailName, ooThumbnailNames } from "./const";
2929
export type { Dep, DepRaw, Deps, IDepMap, InstallAllResult, InstallFileResult, InstallPackageResult, OOPackageSchema, SearchDep } from "./types";
3030
export { transformNodeModules } from "./utils/npm";

src/utils/npm.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe.concurrent("generatePackageJson", () => {
5656
"package",
5757
"package/.gitignore",
5858
"package/.oo-thumbnail.json",
59+
"package/.oo-thumbnail.zh-CN.json",
5960
],
6061
});
6162
});

src/utils/npm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export async function generatePackageJson(dir: string): Promise<string> {
4646
"package",
4747
"package/.gitignore",
4848
"package/.oo-thumbnail.json",
49+
"package/.oo-thumbnail.zh-CN.json",
4950
];
5051

5152
return JSON.stringify(content, null, 2);

src/utils/thumbnail.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,19 @@ const readFile = async (path: string): Promise<string | undefined> => {
227227
catch { }
228228
};
229229

230+
const manifestFileCache = new Map<string, Record<string, any> | null>();
231+
230232
export class Thumbnail implements ThumbnailProvider {
231-
public static async create(workspaceDir: string, registryStoreDir: string): Promise<Thumbnail | undefined> {
232-
const depsQuery = new DepsQuery(registryStoreDir);
233+
public static clearCache(): void {
234+
manifestFileCache.clear();
235+
}
236+
237+
public static async create(workspaceDir: string, registryStoreDir: string, lang: string): Promise<Thumbnail | undefined> {
238+
const depsQuery = new DepsQuery(registryStoreDir, lang);
233239
const wsPkgData = await PkgData.createWS(
234240
depsQuery,
235241
workspaceDir,
242+
lang,
236243
);
237244
if (wsPkgData) {
238245
return new Thumbnail(wsPkgData);
@@ -462,7 +469,7 @@ export class Thumbnail implements ThumbnailProvider {
462469
class DepsQuery {
463470
private cache: Map<string, PkgData | null | Promise<PkgData | undefined>> = new Map();
464471

465-
public constructor(public readonly searchPath: string) { }
472+
public constructor(public readonly searchPath: string, public readonly lang: string) { }
466473

467474
public async getPkgData(packageName: string, packageVersion: string): Promise<PkgData | undefined> {
468475
const packageId = `${packageName}-${packageVersion}`;
@@ -475,7 +482,7 @@ class DepsQuery {
475482
return pkgData;
476483
}
477484
}
478-
const p = PkgData.create(this, packageName, packageVersion, path.join(this.searchPath, packageId));
485+
const p = PkgData.create(this, packageName, packageVersion, path.join(this.searchPath, packageId), this.lang);
479486
this.cache.set(packageId, p);
480487
const pkgData = await p;
481488
this.cache.set(packageId, pkgData || null);
@@ -484,24 +491,30 @@ class DepsQuery {
484491
}
485492

486493
class PkgData {
487-
public static async createWS(depsQuery: DepsQuery, workspaceDir: string): Promise<PkgData | undefined> {
494+
public static async createWS(depsQuery: DepsQuery, workspaceDir: string, lang: string): Promise<PkgData | undefined> {
488495
const packagePath = await resolveManifest(workspaceDir, "package");
489496
if (packagePath) {
490497
const data = await readManifestFile(packagePath);
491-
const localePath = await resolveLocaleFile(workspaceDir);
498+
const localePath = await resolveLocaleFile(workspaceDir, lang);
492499
const userLocale = localePath ? await readJSONFile(localePath) : undefined;
500+
if (!userLocale && lang && lang !== "en") {
501+
return;
502+
}
493503
if (data && data.name && data.version) {
494504
return new PkgData(depsQuery, data.name, data.version, workspaceDir, packagePath, data, userLocale);
495505
}
496506
}
497507
}
498508

499-
public static async create(depsQuery: DepsQuery, packageName: string, packageVersion: string, packageDir: string): Promise<PkgData | undefined> {
509+
public static async create(depsQuery: DepsQuery, packageName: string, packageVersion: string, packageDir: string, lang: string): Promise<PkgData | undefined> {
500510
const packagePath = await resolveManifest(packageDir, "package");
501511
if (packagePath) {
502-
const data = readManifestFile(packagePath);
503-
const localePath = await resolveLocaleFile(packageDir);
512+
const data = await readManifestFile(packagePath);
513+
const localePath = await resolveLocaleFile(packageDir, lang);
504514
const userLocale = localePath ? await readJSONFile(localePath) : undefined;
515+
if (!userLocale && lang && lang !== "en") {
516+
return;
517+
}
505518
if (data) {
506519
return new PkgData(depsQuery, packageName, packageVersion, packageDir, packagePath, data, userLocale);
507520
}
@@ -712,8 +725,8 @@ class SubflowBlockData implements SharedBlockData, FlowLikeData {
712725
}
713726
}
714727

715-
async function resolveLocaleFile(dirPath: string): Promise<string | undefined> {
716-
const filePath = path.join(dirPath, "oo-locales/en.json");
728+
async function resolveLocaleFile(dirPath: string, lang?: string): Promise<string | undefined> {
729+
const filePath = path.join(dirPath, "oo-locales", `${lang || "en"}.json`);
717730
if (await isFile(filePath)) {
718731
return filePath;
719732
}
@@ -731,16 +744,22 @@ async function resolveManifest(dirPath: string, type: string): Promise<string |
731744
}
732745

733746
async function readManifestFile(manifestPath: string): Promise<Record<string, any> | undefined> {
747+
if (manifestFileCache.has(manifestPath)) {
748+
return manifestFileCache.get(manifestPath) || undefined;
749+
}
734750
try {
735751
const content = await readFile(manifestPath);
736752
if (content) {
737753
const data = YAML.parse(content);
738754
if (isPlainObject(data)) {
755+
manifestFileCache.set(manifestPath, data);
739756
return data;
740757
}
741758
}
759+
manifestFileCache.set(manifestPath, null);
742760
}
743761
catch {
762+
manifestFileCache.set(manifestPath, null);
744763
return undefined;
745764
}
746765
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
aaa
22

33
.oo-thumbnail.json
4+
.oo-thumbnail.zh-CN.json
45

56
foo.txt

0 commit comments

Comments
 (0)