nomadro/apps/taro/scripts/export-seed.cjs

37 lines
2.1 KiB
JavaScript

// One-way snapshot of this project's own bundled catalog. Never reads a mini-program.
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const root = path.resolve(__dirname, '..');
const data = path.join(root, 'web/src/data');
const cache = new Map();
function load(name) {
const file = path.resolve(data, name.endsWith('.json') ? name : `${name}.ts`);
if (!file.startsWith(data + path.sep)) throw new Error('Seed import escapes data directory');
if (cache.has(file)) return cache.get(file);
if (file.endsWith('.json')) return JSON.parse(fs.readFileSync(file, 'utf8'));
const module = { exports: {} };
const code = ts.transpileModule(fs.readFileSync(file, 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020, esModuleInterop: true },
}).outputText;
vm.runInNewContext(code, { module, exports: module.exports, process: { env: { TARO_ENV: 'h5' } },
require: request => load(request.replace(/^\.\//, '')) }, { filename: file });
cache.set(file, module.exports);
return module.exports;
}
const config = load('remoteConfig.defaults').DEFAULT_REMOTE_CONFIG;
config.flags.payEnabled = false;
config.flags.vipWebBridgeEnabled = false;
const local = load('localCatalog');
const social = load('socialSeed');
const seed = { config, book: load('nomadCodeBook').nomadCodeReadPayload(),
catalogs: { destinations: local.LOCAL_DESTINATIONS, meetups: local.LOCAL_MEETUPS,
visas: local.LOCAL_VISAS, testimonials: local.LOCAL_TESTIMONIALS, stats: local.LOCAL_STATS,
channels: social.SEED_CHANNELS, messages: social.SEED_MESSAGES, roster: social.MOCK_ROSTER,
gigs: social.SEED_GIGS, cafes: social.SEED_CAFES, cities: social.SOCIAL_CITIES,
socialMeetups: social.SEED_MEETUPS, videos: social.SEED_VIDEOS, ebooks: social.SEED_EBOOKS } };
fs.mkdirSync(path.join(root, 'api/seed'), { recursive: true });
fs.writeFileSync(path.join(root, 'api/seed/catalog.json'), JSON.stringify(seed, null, 2) + '\n');
console.log('Exported Taro-owned seed; no production data or mini-program sources read.');