This repository was archived by the owner on Feb 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (99 loc) · 4.41 KB
/
Copy pathindex.js
File metadata and controls
114 lines (99 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2018 CaptoMD
*/
const path = require('path');
const chalk = require('chalk');
const _ = require('lodash');
const log = require('loglevel');
const fs = require('fs');
const readConcepts = require('./lib/source/read-concepts');
const readRequests = require('./lib/source/read-requests');
const processConcepts = require('./lib/concept/process-concepts');
const { openSpreadsheet } = require('./lib/speadsheet/spreadsheet');
const pushConcepts = require('./lib/speadsheet/push-concepts');
const extractTranslations = require('./lib/extract/extract-translations');
const extractConceptValues = require('./lib/extract/extract-concept-values');
const extractConceptLabels = require('./lib/extract/extract-concept-labels');
const extractMapping = require('./lib/mapping/extract-mapping');
const writeTranslations = require('./lib/source/write-translations');
const writeValues = require('./lib/source/write-values');
const writeConfig = require('./lib/source/write-config');
const LANG = ['fr', 'en'];
function getLevel(verbose) {
return verbose ? log.levels.INFO : log.levels.WARN;
}
function defaultCredentials() {
return require(path.resolve('./.spreadsheet-creds.json'));
}
async function open(spreadsheetId, credentials = defaultCredentials()) {
return openSpreadsheet(spreadsheetId, credentials);
}
async function extractConcepts(conceptsRootPath, { verbose = true }) {
log.getLogger('process-concepts').setLevel(getLevel(verbose));
const conceptData = await readConcepts(conceptsRootPath, { verbose });
const concepts = processConcepts(conceptData);
if (verbose) {
console.log(chalk`Extracted: {bold ${Object.keys(concepts).length}} concepts`);
}
return concepts;
}
async function processTranslationSpreadsheet(translationSpreadsheetId, concepts, targetPath, dryRun) {
const doc = await open(translationSpreadsheetId);
const { Concepts: conceptsSheet, ['Concepts-Values']: conceptValuesSheet, ...otherSheets } = doc.sheetsByTitle;
await pushConcepts(concepts, conceptsSheet, conceptValuesSheet, dryRun);
const translations = await extractTranslations(otherSheets, LANG);
const conceptLabels = await extractConceptLabels(conceptsSheet, conceptValuesSheet, concepts, LANG);
LANG.forEach((lang) => {
_.set(translations, [lang, 'concept'], conceptLabels[lang]);
});
await writeTranslations(targetPath, translations);
await writeConfig(targetPath, conceptLabels.config);
}
async function processValueSpreadsheet(valueSpreadsheetId, concepts, targetPath, verbose) {
const doc = await open(valueSpreadsheetId);
const conceptsValues = await extractConceptValues(doc, concepts, LANG, verbose);
await writeValues(targetPath, conceptsValues);
}
async function runTranslations({
translations: translationSpreadsheetId,
values: valueSpreadsheetId,
concepts: conceptsRootPath,
target: targetPath,
verbose = true,
dryRun,
}) {
log.setLevel(getLevel(verbose));
log.getLogger('read-file').setLevel(getLevel(verbose));
log.getLogger('write-file').setLevel(getLevel(verbose));
log.getLogger('spreadsheet').setLevel(getLevel(verbose));
log.getLogger('push-concept').setLevel(getLevel(verbose));
log.getLogger('extract-values').setLevel(getLevel(verbose));
log.getLogger('extract-translations').setLevel(getLevel(verbose));
const concepts = await extractConcepts(conceptsRootPath, { verbose });
if (translationSpreadsheetId) {
await processTranslationSpreadsheet(translationSpreadsheetId, concepts, targetPath, dryRun);
} else if (valueSpreadsheetId) {
await processValueSpreadsheet(valueSpreadsheetId, concepts, path.resolve(conceptsRootPath, 'values'), verbose);
} else {
_.map(concepts, (value, id) => {
console.log(id, value);
});
}
}
async function processMappingSpreadsheet(valueSpreadsheetId, targetFile, verbose) {
log.setLevel(getLevel(verbose));
log.getLogger('extract-mapping').setLevel(getLevel(verbose));
const doc = await open(valueSpreadsheetId);
const mapping = await extractMapping(doc.sheetsByTitle);
const resolvedPath = path.resolve(__dirname, targetFile);
log.info(chalk`Writing to {cyan ${resolvedPath}}`);
fs.writeFileSync(resolvedPath, JSON.stringify(mapping), 'utf8');
}
async function run(params) {
if (params.mapping) {
await processMappingSpreadsheet(params.mapping, params.target || 'temp.json', params.verbose || true);
} else {
await runTranslations(params);
}
}
module.exports = { run, extractConcepts, readConcepts, readRequests };