2020-08-24 21:16:09 +00:00
|
|
|
import { copyFile, readFile } from "fs/promises";
|
2020-08-24 07:37:48 +00:00
|
|
|
import * as handlebars from 'handlebars';
|
|
|
|
import { join } from 'path';
|
2020-08-24 21:16:09 +00:00
|
|
|
import { Project } from "ts-morph";
|
2020-08-25 08:21:55 +00:00
|
|
|
import { Logger } from 'winston';
|
2020-08-24 21:16:09 +00:00
|
|
|
import { analyzeFiles } from "./analyzeTypescriptFiles";
|
2020-08-24 07:37:48 +00:00
|
|
|
import { createFile, createPath } from "./fileHelpers";
|
2020-08-23 07:25:10 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-24 07:37:48 +00:00
|
|
|
* Generate the Client Templates.
|
|
|
|
* @param options
|
2020-08-23 07:25:10 +00:00
|
|
|
*/
|
2020-08-24 07:37:48 +00:00
|
|
|
export async function generateClientTemplate(options: {
|
|
|
|
pathToTemplate: string,
|
|
|
|
outputDir: string,
|
|
|
|
inputDir: string,
|
|
|
|
tsConfigFilePath: string,
|
2020-08-25 08:21:55 +00:00
|
|
|
// A Logger.
|
|
|
|
logger?: Logger
|
2020-08-24 07:37:48 +00:00
|
|
|
}) {
|
|
|
|
|
|
|
|
// Create the Output dir (if it doenst exists)
|
|
|
|
await createPath(options.outputDir);
|
|
|
|
|
2020-08-25 08:21:55 +00:00
|
|
|
if (options.logger) {
|
|
|
|
options.logger.info('Templates will be stored in ' + options.outputDir);
|
|
|
|
}
|
|
|
|
|
2020-08-24 07:37:48 +00:00
|
|
|
// Firstly copy the nopeDispatcher
|
|
|
|
await copyFile(
|
2020-08-24 21:16:09 +00:00
|
|
|
join(__dirname, '..', '..', '..', 'lib', 'dispatcher', 'nopeDispatcher.ts'),
|
|
|
|
join(options.outputDir, 'nopeDispatcher.ts')
|
2020-08-24 07:37:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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)
|
2020-08-23 07:25:10 +00:00
|
|
|
}
|
2020-08-24 07:37:48 +00:00
|
|
|
});
|
|
|
|
|
2020-08-24 21:16:09 +00:00
|
|
|
for (const file of files) {
|
2020-08-25 08:21:55 +00:00
|
|
|
// Define the File Name:
|
2020-08-24 21:16:09 +00:00
|
|
|
const fileName = join(options.outputDir, 'clients', file.name);
|
2020-08-25 08:21:55 +00:00
|
|
|
|
2020-08-24 07:37:48 +00:00
|
|
|
await createFile(
|
|
|
|
// Generate the Path.
|
2020-08-24 21:16:09 +00:00
|
|
|
join(options.outputDir, 'clients', file.name),
|
2020-08-24 07:37:48 +00:00
|
|
|
file.content
|
2020-08-24 21:16:09 +00:00
|
|
|
);
|
|
|
|
|
2020-08-25 08:21:55 +00:00
|
|
|
if (options.logger) {
|
|
|
|
options.logger.info('Generated -> ' + fileName);
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:16:09 +00:00
|
|
|
// Function to Determine new project files.
|
|
|
|
const project = new Project({
|
|
|
|
tsConfigFilePath: options.tsConfigFilePath,
|
|
|
|
addFilesFromTsConfig: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
project.addSourceFileAtPath(fileName);
|
|
|
|
|
|
|
|
// Readin the Source-Files.
|
|
|
|
const sourceFiles = project.getSourceFiles();
|
|
|
|
|
|
|
|
for (const file of sourceFiles) {
|
|
|
|
file.formatText();
|
|
|
|
await file.save();
|
|
|
|
}
|
2020-08-23 07:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 07:37:48 +00:00
|
|
|
// Compile the Template and parse the Code.
|
2020-08-24 21:16:09 +00:00
|
|
|
return true;
|
2020-08-23 07:25:10 +00:00
|
|
|
}
|