|
| 1 | +import child_process from 'node:child_process'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import {fileURLToPath} from 'node:url'; |
| 5 | + |
| 6 | +const PROJECT_ROOT = path |
| 7 | + .resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..') |
| 8 | + .replaceAll(path.sep, '/') |
| 9 | + .replace(/\/$/, ''); |
| 10 | + |
| 11 | +const DATADIR = |
| 12 | + path |
| 13 | + .resolve(path.dirname(fileURLToPath(import.meta.url)), '..') |
| 14 | + .replaceAll(path.sep, '/') |
| 15 | + .replace(/\/$/, '') + '/public/data'; |
| 16 | + |
| 17 | +const VERSIONS = JSON.parse(fs.readFileSync(`${DATADIR}/versions.json`)); |
| 18 | +if (!fs.existsSync(`${DATADIR}/versions`)) { |
| 19 | + fs.mkdirSync(`${DATADIR}/versions`); |
| 20 | +} |
| 21 | + |
| 22 | +for (let version of VERSIONS) { |
| 23 | + const outputFileCompressed = `${DATADIR}/versions/${version}.min.json`; |
| 24 | + const outputFileUncompressed = `${DATADIR}/versions/${version}.json`; |
| 25 | + if ( |
| 26 | + fs.existsSync(outputFileCompressed) && |
| 27 | + fs.existsSync(outputFileUncompressed) |
| 28 | + ) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + process.stdout.write(`# Creating data for version ${version}\n`); |
| 32 | + const importer = child_process.spawn( |
| 33 | + 'php', |
| 34 | + [`${PROJECT_ROOT}/bin/import-cldr-data`, version], |
| 35 | + { |
| 36 | + stdio: [ |
| 37 | + // stdin |
| 38 | + 'ignore', |
| 39 | + // stout |
| 40 | + 'inherit', |
| 41 | + // stderr |
| 42 | + 'inherit', |
| 43 | + ], |
| 44 | + }, |
| 45 | + ); |
| 46 | + await new Promise((resolve, reject) => { |
| 47 | + importer.on('close', (code) => { |
| 48 | + if (code === 0) { |
| 49 | + resolve(); |
| 50 | + } else { |
| 51 | + reject(new Error(`Child process exited with code ${code}`)); |
| 52 | + } |
| 53 | + }); |
| 54 | + }); |
| 55 | + process.stdout.write('Creating json files\n'); |
| 56 | + for (let compressed of [false, true]) { |
| 57 | + const outputFile = compressed |
| 58 | + ? outputFileCompressed |
| 59 | + : outputFileUncompressed; |
| 60 | + const exporter = child_process.spawn( |
| 61 | + 'php', |
| 62 | + [ |
| 63 | + `${PROJECT_ROOT}/bin/export-plural-rules`, |
| 64 | + `--output=${outputFile}`, |
| 65 | + compressed ? 'json' : 'prettyjson', |
| 66 | + ], |
| 67 | + { |
| 68 | + stdio: [ |
| 69 | + // stdin |
| 70 | + 'ignore', |
| 71 | + // stout |
| 72 | + 'inherit', |
| 73 | + // stderr |
| 74 | + 'inherit', |
| 75 | + ], |
| 76 | + }, |
| 77 | + ); |
| 78 | + await new Promise((resolve, reject) => { |
| 79 | + exporter.on('close', (code) => { |
| 80 | + if (code === 0) { |
| 81 | + resolve(); |
| 82 | + } else { |
| 83 | + reject(new Error(`Child process exited with code ${code}`)); |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments