node-ff
Version:
A CLI for creating NodeJs projects
172 lines (155 loc) • 4.65 kB
text/typescript
import { textHandling } from '../../utils';
import { IWriteFile } from '../../utils/write/interface';
import { IData, IUseful } from './interfaces';
import { OrmControl } from './ormControl';
import {
editorconfig,
eslintConfig,
gitIgnore,
prettierrc,
serverJav,
serverTyp,
tsconfig,
tsconfigOrm,
vcCodeSettings,
whichJSON,
Readme,
sequeliseJav,
sequeliseTyp,
} from './packages';
class Control {
private eslintConf = new eslintConfig(this.typing);
private initial_data: IData = {
directory_name: this.project,
filename: '',
text: '',
formate: false,
json: {},
};
private data = {
git: {
...this.initial_data,
filename: '.gitignore',
text: textHandling.return_text(gitIgnore),
},
code: {
...this.initial_data,
directory_name: `${this.project}/.vscode`,
filename: 'settings.json',
formate: true,
json: vcCodeSettings,
},
initial: {
...this.initial_data,
filename: ['.editorconfig', '.prettierrc.js', 'readme.md'],
text: [
textHandling.return_text(editorconfig),
textHandling.return_text(prettierrc),
textHandling.return_text(new Readme(this.orm).content),
],
},
json: {
...this.initial_data,
filename: 'package.json',
formate: true,
json: new whichJSON(this.typing, this.orm).json,
},
eslint: {
...this.initial_data,
filename: ['.eslintrc.js', '.eslintignore'],
text: [
textHandling.return_text(this.eslintConf.configJs),
textHandling.return_text(this.eslintConf.ignore),
],
},
with_typing: {
directory_name: [`${this.project}/src`, this.project],
filename: ['server.ts', 'tsconfig.json'],
text: [textHandling.return_text(serverTyp), ''],
formate: [false, true],
json: [{}, this.orm ? tsconfigOrm : tsconfig],
},
no_typing: {
directory_name: `${this.project}/src`,
filename: 'server.js',
text: textHandling.return_text(serverJav),
},
orms: {
sequelize: new OrmControl({
projectName: this.project,
ormWithType: this.typing,
}).content,
},
};
constructor(
private project: string,
private typing: boolean | undefined,
private infos: Array<string>,
private orm: string | boolean,
private process: IWriteFile,
) { }
async exec({
initial_msg,
final_msg,
final_comands,
}: IUseful): Promise<void> {
console.log(initial_msg);
await Promise.all(
this.infos.map(async (item) => {
await this.process.execute(this.data[item as keyof object]);
}),
);
final_comands?.execute({
commands: `cd ${this.project} && yarn`,
});
if (this.orm) await this.exec_orm({ final_comands });
console.log(final_msg);
}
private async exec_orm({ final_comands }: IUseful): Promise<void> {
final_comands?.execute({
commands: this.data.orms[this.orm as keyof object]['commands'],
});
setTimeout(async () => {
delete this.data.orms[this.orm as keyof object]['commands'];
await this.process.execute(this.data.orms[this.orm as keyof object]);
}, 1000);
if (this.orm === 'sequelize') {
await this.sequelize();
} else {
await this.prisma();
}
}
protected async sequelize() {
const data = new Date();
const day = String(data.getDate()).padStart(2, '0');
const month = String(data.getMonth() + 1).padStart(2, '0');
const year = data.getFullYear();
const current_date = year + month + day;
if (this.typing) {
setTimeout(async () => {
await this.process.execute({
directory_name: `${this.project}/app/db/migrations`,
filename: `${current_date}235103-create-user.js`,
text: sequeliseTyp.userMigration,
});
}, 1000);
setTimeout(async () => {
await this.process.execute({
directory_name: `${this.project}/app/db/seeders`,
filename: `${current_date}235103-demo-user.js`,
text: sequeliseTyp.userDemo,
});
}, 1000);
} else {
setTimeout(async () => {
await this.process.execute({
directory_name: `${this.project}/database/migrations`,
filename: `${current_date}235103-create-user.js`,
text: sequeliseJav.user_migration,
});
}, 1000);
}
}
protected async prisma() { }
}
export { Control };