signal-desktop/ts/scripts/compile-stories-icu-lookup.ts

62 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-03-13 12:52:08 -07:00
// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2025-03-26 13:23:53 -07:00
import { readFile, writeFile } from 'node:fs/promises';
import { join, basename } from 'node:path';
2025-03-13 12:52:08 -07:00
import pMap from 'p-map';
2025-03-26 13:23:53 -07:00
import fastGlob from 'fast-glob';
2025-03-13 12:52:08 -07:00
import { drop } from '../util/drop';
async function main(): Promise<void> {
const source = process.argv[2];
if (!source) {
throw new Error('Missing required source directory argument');
}
2025-03-26 13:23:53 -07:00
const dirEntries = await fastGlob('*/*.jpg', {
cwd: join(source, 'data'),
onlyFiles: true,
});
2025-03-13 12:52:08 -07:00
const enMessages = JSON.parse(
await readFile(
join(__dirname, '..', '..', '_locales', 'en', 'messages.json'),
'utf8'
)
);
const icuToStory: Record<string, Array<string>> = Object.create(null);
await pMap(
2025-03-26 13:23:53 -07:00
dirEntries,
async entry => {
const [storyId, imageFile] = entry.split('/', 2);
2025-03-13 12:52:08 -07:00
2025-03-26 13:23:53 -07:00
const icuId = `icu:${basename(imageFile, '.jpg')}`;
2025-03-13 12:52:08 -07:00
2025-03-26 13:23:53 -07:00
let list = icuToStory[icuId];
if (list == null) {
list = [];
icuToStory[icuId] = list;
2025-03-13 12:52:08 -07:00
}
2025-03-26 13:23:53 -07:00
list.push(storyId);
2025-03-13 12:52:08 -07:00
},
{ concurrency: 20 }
);
const index = Object.entries(icuToStory).map(([key, icuIds]) => {
return [key, enMessages[key]?.messageformat, icuIds];
});
const html = await readFile(
join(__dirname, '..', '..', '.storybook', 'icu-lookup.html'),
'utf8'
);
await writeFile(
join(source, 'index.html'),
html.replace('%INDEX%', JSON.stringify(index))
);
}
drop(main());