scv-bilara
Version:
SuttaCentral bilara-data library
74 lines (69 loc) • 2.65 kB
JavaScript
const fs = require('fs');
const path = require('path');
const { logger } = require('log-instance');
const APP_DIR = path.join(__dirname, '..', '..');
const LOCAL_DIR = path.join(APP_DIR, 'local');
const {
BilaraData,
Seeker,
} = require(`${APP_DIR}/index.js`);
const SRC_DIR = path.join(APP_DIR, 'src');
const EXAMPLES_PATH = path.join(SRC_DIR, 'examples.json');
const IS_EXAMPLE_PATH = path.join(SRC_DIR, 'is-example.json');
const EBT_DATA_DIR = path.join(LOCAL_DIR, 'ebt-data');
const EXAMPLES_DIR = path.join(EBT_DATA_DIR, 'examples');
const SCRIPT = path.basename(__filename);
const COMMENT = [`Auto-generated by ${SCRIPT}`];
logger.logLevel = 'info';
(async function(){ try {
let bilaraData = new BilaraData();
await bilaraData.syncEbtData();
let exampleFiles = (await fs.promises.readdir(EXAMPLES_DIR))
.filter(f=>/examples-/.test(f));
logger.info(`exampleFiles`, exampleFiles);
let now = new Date();
let authors = [];
let examples = {
comment: COMMENT,
authors,
};
let languages = [];
for (exampleFile of exampleFiles) {
let examplePath = path.join(EXAMPLES_DIR, exampleFile);
let langExamples = (await fs.promises.readFile(examplePath))
.toString()
.split('\n')
.filter(ex=>!!ex);
if (langExamples.length) {
let fileKeys = exampleFile.replace('.txt', '').split('-');
let [prefix, lang, category='sutta', version, ] = fileKeys;
if (version == null) {
logger.log(`${exampleFile}: ignored (deprecated file name)`);
continue;
}
let author = fileKeys.slice(4).join('-');
if (author) {
languages.push(lang);
authors.push({lang, category, version, author});
examples[lang] = langExamples;
logger.log(`${exampleFile}: ${langExamples.length}`,
JSON.stringify({lang, category, version, author}));
} else {
logger.warn(`${exampleFile}: ignored (no author?)`);
}
}
}
let examplesJson = JSON.stringify(examples,null,2) + '\n';
await fs.promises.writeFile(EXAMPLES_PATH, examplesJson);
logger.info(`updated ${EXAMPLES_PATH} (OK)`);
delete examples.comment;
delete examples.authors;
let exampleCache = Seeker.buildExampleCache(examples);
await fs.promises.writeFile(IS_EXAMPLE_PATH,
JSON.stringify(exampleCache,null,'\t')+'\n');
logger.info(`updated ${IS_EXAMPLE_PATH} (OK)`);
logger.info(`DONE`);
} catch(e) {
logger.warn(e);
}})();