All files / src generate.ts

0% Statements 0/23
0% Branches 0/4
0% Functions 0/2
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                                                       
import Templater from "./templater";
import {RemoteFile} from "./files";
import {InstanceReader} from "./instance";
import path from "path";
 
const TEMPLATE_FILE = "../../templates/template.handlebars";
 
function modifyExtension(filePath: string, extension: string) {
  let file = new RemoteFile(filePath);
  return path.resolve(`${file.dir()}/${file.name()}.${extension}`);
}
 
export default function generate(inFile: string, outFile: string = "") {
  try {
    let reader = new InstanceReader(inFile);
    let [type, instance] = reader.get();
    let templater = new Templater(instance);
    let compiled = templater.compile(TEMPLATE_FILE, type);
    outFile = outFile || modifyExtension(reader.filePath, "d.ts");
    let writer = new RemoteFile(outFile);
    writer.write(compiled);
    return outFile;
  } catch(err) {
    console.log(err);
    console.error("Error:", err.message);
    process.exit(1);
  }
}