mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-05 16:35:33 +00:00
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
// TODO: this file is really only for lexical model compiler tests. Find a good name.
|
|
//
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as ts from 'typescript';
|
|
|
|
import { SysExits } from "./sysexits";
|
|
import LexicalModelCompiler from '../lexical-model-compiler/lexical-model-compiler';
|
|
|
|
/**
|
|
* Compiles a model.ts file, using paths relative to its location.
|
|
*
|
|
* @param filename path to model.ts source.
|
|
* @return model source code
|
|
*/
|
|
export function compileModel(filename: string): string {
|
|
let modelSource = loadFromFilename(filename);
|
|
let containingDirectory = path.dirname(filename);
|
|
|
|
return (new LexicalModelCompiler)
|
|
.generateLexicalModelCode('<unknown>', modelSource, containingDirectory);
|
|
}
|
|
|
|
/**
|
|
* Loads a lexical model's source module from the given filename.
|
|
*
|
|
* @param filename path to the model source file.
|
|
*/
|
|
export function loadFromFilename(filename: string): LexicalModelSource {
|
|
let sourceCode = fs.readFileSync(filename, 'utf8');
|
|
// Compile the module to JavaScript code.
|
|
// NOTE: transpile module does a very simple TS to JS compilation.
|
|
// It DOES NOT check for types!
|
|
let compilationOutput = ts.transpile(sourceCode, {
|
|
// Our runtime should support ES6 with Node/CommonJS modules.
|
|
target: ts.ScriptTarget.ES2015,
|
|
module: ts.ModuleKind.CommonJS,
|
|
});
|
|
// Turn the module into a function in which we can inject a global.
|
|
let moduleCode = '(function(exports){' + compilationOutput + '})';
|
|
// Run the module; its exports will be assigned to `moduleExports`.
|
|
let moduleExports = {};
|
|
let module = eval(moduleCode);
|
|
module(moduleExports);
|
|
if (!moduleExports['__esModule'] || !moduleExports['default']) {
|
|
console.error(`Model source '${filename}' does have a default export. Did you remember to write \`export default source;\`?`);
|
|
// TODO: throw an Error instead.
|
|
process.exit(SysExits.EX_DATAERR);
|
|
}
|
|
return moduleExports['default'] as LexicalModelSource;
|
|
}
|