nope/lib/helpers/generateTemplate.ts

74 lines
1.9 KiB
TypeScript

import { Project } from "ts-morph";
import { analyzeFiles } from "./analyzeTypescriptFiles";
import * as handlebars from 'handlebars';
import { readFile, copyFile } from "fs/promises";
import { join } from 'path';
import { createFile, createPath } from "./fileHelpers";
/**
* Generate the Client Templates.
* @param options
*/
export async function generateClientTemplate(options: {
pathToTemplate: string,
outputDir: string,
inputDir: string,
tsConfigFilePath: string,
}) {
// Create the Output dir (if it doenst exists)
await createPath(options.outputDir);
// Firstly copy the nopeDispatcher
await copyFile(
join(__dirname,'..','..','..','lib','dispatcher','nopeDispatcher.ts'),
join(options.outputDir,'nopeDispatcher.ts')
);
// Function to Determine new project files.
const project = new Project({
tsConfigFilePath: options.tsConfigFilePath,
addFilesFromTsConfig: false,
});
project.addSourceFilesAtPaths(options.inputDir);
// Readin the Source-Files.
const sourceFiles = project.getSourceFiles();
// Generate the Files
const result = analyzeFiles(sourceFiles);
// load the File.
const template = await readFile(options.pathToTemplate, {
encoding: 'utf-8'
})
// Renderfuncting
const render = handlebars.compile(template);
// Generate the Files.
const files: {
name: string,
content: string,
}[] = result.map(item => {
item.className = item.className + 'ClientInterface'
return {
name: item.className + '.ts',
content: render(item)
}
});
for (const file of files){
await createFile(
// Generate the Path.
join(options.outputDir,'clients',file.name),
file.content
)
}
// Compile the Template and parse the Code.
return true;
}