@keymanapp/kmc
Version:
Keyman Developer compiler command line tools
41 lines • 1.93 kB
JavaScript
import * as fs from 'fs';
import * as path from 'path';
import * as kmcLdml from '@keymanapp/kmc-ldml';
import { defaultCompilerOptions, LDMLKeyboardXMLSourceFileReader } from '@keymanapp/developer-utils';
import { NodeCompilerCallbacks } from '../../util/NodeCompilerCallbacks.js';
import { fileURLToPath } from 'url';
import { exitProcess } from '../../util/sysexits.js';
import { InfrastructureMessages } from '../../messages/infrastructureMessages.js';
import { dirname } from 'node:path';
export async function buildTestData(infile, _options, commander) {
const options = commander.optsWithGlobals();
let compilerOptions = {
...defaultCompilerOptions,
...options,
saveDebug: false,
shouldAddCompilerVersion: false,
readerOptions: {
cldrImportsPath: fileURLToPath(new URL(...LDMLKeyboardXMLSourceFileReader.defaultImportsURL)),
localImportsPaths: [dirname(infile)], // local dir
}
};
const callbacks = new NodeCompilerCallbacks(options);
let testData = await loadTestData(infile, callbacks, compilerOptions);
if (!testData || callbacks.hasFailureMessage()) {
await exitProcess(1);
}
const fileBaseName = options.outFile ?? infile;
const outFileBase = path.basename(fileBaseName, path.extname(fileBaseName));
const outFileDir = path.dirname(fileBaseName);
const outFileJson = path.join(outFileDir, outFileBase + '.json');
fs.writeFileSync(outFileJson, JSON.stringify(testData, null, ' '));
callbacks.reportMessage(InfrastructureMessages.Info_FileBuiltSuccessfully({ filename: outFileJson, relativeFilename: infile }));
}
async function loadTestData(inputFilename, callbacks, options) {
const k = new kmcLdml.LdmlKeyboardCompiler();
if (!await k.init(callbacks, options)) {
return null;
}
return await k.loadTestData(inputFilename);
}
//# sourceMappingURL=index.js.map