tstosc
Version:
A transpiler that convert TypeScript to SuperCollider's SCLang.
79 lines (73 loc) • 2.64 kB
JavaScript
;
var fs = require('fs');
var path = require('path');
var file_conv_cjs = require('./ts_to_sc_convert/file_conv.cjs');
var context_cjs = require('./context.cjs');
var error_cjs = require('../../util/error.cjs');
var Result_cjs = require('../../util/Result.cjs');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var path__default = /*#__PURE__*/_interopDefault(path);
class Generator {
program;
context;
constructor({
compiler_program,
context = context_cjs.default_generator_context,
transpiler_option = context_cjs.default_transpiler_option
}) {
this.program = compiler_program;
this.context = { ...context, compiler_program, transpiler_option };
}
/**
* Generate the SCLang result of inputted files.
*
* For now, all the files recorded in compiler program's `root_files_path` (`rootNames`)
* will be generated.
*
* @param output_path By default, current work directory.
*/
generateAll(output_path = ".") {
let outputed_file_name_index_dict = /* @__PURE__ */ new Map();
for (const f_name of this.program.getRootFileNames()) {
let output_index = outputed_file_name_index_dict.get(f_name) ?? -1;
const output_full_path = path.join(
output_path,
output_index >= 0 ? `${f_name}_${++output_index}` : f_name
).replace(/\.ts$/g, ".scd");
const result = this.generateFile({
name: f_name,
output_path: output_full_path
});
if (result.isErr()) {
return result;
}
outputed_file_name_index_dict.set(f_name, output_index);
}
return Result_cjs.Result.createOk(null);
}
/**
*
* @throws `SourceFileNotFoundError` if the `name` cannot be used to get source file.
*/
generateFile({
name,
output_path = path__default.default.join(path__default.default.dirname(name), path__default.default.basename(name).replace(/\.ts$/g, ".scd")),
convertTSSourceFileToSC_option = file_conv_cjs.default_convert_file_option
}) {
const source_file = this.program.getSourceFile(name);
if (source_file == void 0) {
return Result_cjs.Result.createErr(error_cjs.SourceFileNotFoundError.forFile(name));
}
const converted = file_conv_cjs.convertTSSourceFileToSC(source_file, this.context, convertTSSourceFileToSC_option);
if (converted.isOk()) {
fs.writeFileSync(output_path, converted.unwrapOk().file);
return Result_cjs.Result.createOk(null);
} else {
return Result_cjs.Result.fromErr(converted);
}
}
getContext() {
return { ...this.context };
}
}
exports.Generator = Generator;