sinotron
Version:
Simple framework for Typescript Electron projects
158 lines (157 loc) • 6.09 kB
JavaScript
import { CodeSourceId, CodeSources } from '../../__code_sources/index.js';
import * as ChangeCase from 'change-case';
import { ask } from '../../utils/ask.js';
import Path from 'node:path';
import { logError, logInfo, logNotice } from '../../utils/log.js';
import { _str } from '../../utils/_str.js';
import { FileRep, fsutil } from '@utilis/fs';
export class GenerateApiFiles {
seekRoot;
apiName;
apiTag;
dest = '';
apiFolder = '';
symbolClassName = '';
apiTagClassName = '';
symbolInstanceName = '';
symbolFileName = '';
constructor(opts) {
this.seekRoot = opts.seekRoot || process.cwd();
this.apiName = this.parseApiName(opts.apiName);
this.apiTag = this.apiName.replace(/-api$/i, '');
}
static async task(opts) {
const ob = new GenerateApiFiles(opts);
await ob.task();
}
parseApiName(name) {
name = name.replace(new RegExp('^-+'), '').replace(new RegExp('-+$'), '');
if (name.endsWith('api') && !name.endsWith('-api')) {
name = name.replace(/(api)$/i, '-api');
}
if (!name.endsWith('-api')) {
name = name.concat('-api');
}
return name;
}
async task() {
const seekRoot = this.seekRoot || process.cwd();
// let apiName = this.apiName;
//
// apiName = apiName
// .replace(/(api)+$/i, '')
// .replace(/-+$/, '')
// .concat('-api');
this.symbolClassName = ChangeCase.pascalCase(this.apiName);
this.symbolInstanceName = ChangeCase.camelCase(this.apiName);
this.symbolFileName = ChangeCase.kebabCase(this.apiName);
this.apiTagClassName = ChangeCase.pascalCase(this.apiTag);
console.log('');
const { dest } = await ask.selectPath('dest', {
refDir: seekRoot,
targetType: 'directory',
message: `Select target folder where "${this.apiTag}/" folder should be created:`
});
if (dest) {
this.dest = dest;
}
else {
const { ans } = await ask.input('ans', 'No destination directory selected. The current directory will be used. PROCEED? (y/n)');
if (_str.isYesInput(ans)) {
this.dest = process.cwd();
}
else {
logNotice('Aborting');
process.exit(0);
}
}
if (!fsutil.exists(dest)) {
fsutil.ensureDir(dest);
}
this.generateApiFolder();
this.generateInterfaceFile();
this.generateClientFile();
this.generateServiceFile();
this.generateMetaFile();
}
generateApiFolder() {
this.apiFolder = Path.join(this.dest, this.apiTag);
// console.log({ apiFolder: this.apiFolder });
fsutil.dir.create(this.apiFolder);
}
generateInterfaceFile() {
this.assertApiFolder();
// [interface]
const interfaceFilename = `${this.symbolFileName}.interface.ts`;
const interfaceFilepath = Path.join(this.apiFolder, interfaceFilename);
const interfaceTemplate = CodeSources.read(CodeSourceId.api_interface_template);
const interfaceSourceCode = this.hydrateTemplate(interfaceTemplate);
if (!fsutil.exists(interfaceFilepath)) {
new FileRep(interfaceFilepath).write(interfaceSourceCode);
logInfo(`${interfaceFilepath} created.`);
}
else {
logNotice(`${interfaceFilepath} already exists. Ignoring.`);
}
}
generateClientFile() {
this.assertApiFolder();
const clientFilename = `${this.symbolFileName}.client.ts`;
const clientFilepath = Path.join(this.apiFolder, clientFilename);
// [client]
const clientTemplate = CodeSources.read(CodeSourceId.api_client_template);
const clientSourceCode = this.hydrateTemplate(clientTemplate);
// console.log({ clientSourceCode });
if (!fsutil.exists(clientFilepath)) {
new FileRep(clientFilepath).write(clientSourceCode);
logInfo(`${clientFilepath} created.`);
}
else {
logNotice(`${clientFilepath} already exists. Ignoring.`);
}
}
generateServiceFile() {
this.assertApiFolder();
const serviceFilename = `${this.symbolFileName}.service.ts`;
const serviceFilepath = Path.join(this.apiFolder, serviceFilename);
// [service]
const serviceTemplate = CodeSources.read(CodeSourceId.api_service_template);
const serviceSourceCode = this.hydrateTemplate(serviceTemplate);
// console.log({ serviceSourceCode });
if (!fsutil.exists(serviceFilepath)) {
new FileRep(serviceFilepath).write(serviceSourceCode);
logInfo(`${serviceFilepath} created.`);
}
else {
logNotice(`${serviceFilepath} already exists. Ignoring.`);
}
}
generateMetaFile() {
this.assertApiFolder();
const metaFilename = `${this.symbolFileName}.meta.ts`;
const metaFilepath = Path.join(this.apiFolder, metaFilename);
const metaTemplate = CodeSources.read(CodeSourceId.api_meta_template);
const metaSourceCode = this.hydrateTemplate(metaTemplate);
if (!fsutil.exists(metaFilepath)) {
new FileRep(metaFilepath).write(metaSourceCode);
logInfo(`${metaFilepath} created.`);
}
else {
logNotice(`${metaFilepath} already exists. Ignoring.`);
}
}
hydrateTemplate(template) {
return template
.replace(/\[ClassName]/g, this.symbolClassName)
.replace(/\[instanceName]/g, this.symbolInstanceName)
.replace(/\[filename]/g, this.symbolFileName)
.replace(/\[apiName]/g, this.apiName)
.replace(/\[ApiTag]/g, this.apiTagClassName);
}
assertApiFolder() {
if (!fsutil.isDir(this.apiFolder)) {
logError('No api folder. Aborting');
process.exit(0);
}
}
}