2018-12-08 23:13:33 +01:00
|
|
|
import * as ts from "typescript";
|
2018-12-09 20:18:49 +01:00
|
|
|
import * as ts_generator from "./ts_generator";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as mkdirp from "mkdirp";
|
|
|
|
|
2018-12-08 23:13:33 +01:00
|
|
|
import {PluginConfig} from "ttypescript/lib/PluginCreator";
|
|
|
|
import {writeFileSync} from "fs";
|
2018-12-09 20:18:49 +01:00
|
|
|
import {TranslationEntry} from "./generator";
|
2018-12-08 23:13:33 +01:00
|
|
|
|
|
|
|
interface Config {
|
|
|
|
target_file?: string;
|
|
|
|
verbose?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
//(program: ts.Program, config?: PluginConfig) => ts.TransformerFactory
|
|
|
|
let process_config: Config;
|
|
|
|
export default function(program: ts.Program, config?: PluginConfig) : (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile {
|
|
|
|
process_config = config as any || {};
|
|
|
|
|
2018-12-09 20:18:49 +01:00
|
|
|
const base_path = path.dirname(program.getCompilerOptions().project || program.getCurrentDirectory());
|
|
|
|
if(process_config.verbose) {
|
2018-12-08 23:13:33 +01:00
|
|
|
console.log("TRGen transformer called");
|
2018-12-09 20:18:49 +01:00
|
|
|
console.log("Base path: %s", base_path);
|
|
|
|
}
|
2018-12-08 23:13:33 +01:00
|
|
|
|
|
|
|
process.on('exit', function () {
|
2018-12-09 20:18:49 +01:00
|
|
|
const target = path.isAbsolute(process_config.target_file) ? process_config.target_file : path.join(base_path, process_config.target_file);
|
2018-12-08 23:13:33 +01:00
|
|
|
if(process_config.target_file) {
|
|
|
|
if(process_config.verbose)
|
2018-12-09 20:18:49 +01:00
|
|
|
console.log("Writing translation file to " + target);
|
2018-12-08 23:13:33 +01:00
|
|
|
|
2018-12-09 20:18:49 +01:00
|
|
|
mkdirp.sync(path.dirname(target));
|
|
|
|
writeFileSync(target, JSON.stringify(translations, null, 2));
|
2018-12-08 23:13:33 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ctx => transformer(ctx);
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:18:49 +01:00
|
|
|
const translations: TranslationEntry[] = [];
|
2018-12-08 23:13:33 +01:00
|
|
|
const transformer = (context: ts.TransformationContext) => (rootNode: ts.SourceFile) => {
|
|
|
|
console.log("Processing " + rootNode.fileName);
|
2018-12-09 20:18:49 +01:00
|
|
|
const result = ts_generator.transform({
|
2018-12-08 23:13:33 +01:00
|
|
|
use_window: false,
|
|
|
|
replace_cache: true
|
|
|
|
}, context, rootNode);
|
|
|
|
translations.push(...result.translations);
|
|
|
|
return result.node;
|
|
|
|
};
|