vm721
Version:
"7:1 based Design Pattern for Angular projects"
78 lines (77 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class Files {
constructor(nm, ftyp) {
this.nameFormat = 'default.file';
this.name = '';
this.type = '';
this.filesToGenerate = {
page: ["ts", "scss", "html"],
component: ["ts", "scss", "html"],
service: ["ts"],
middleware: ["ts"]
};
this.fileCoreImports = {
page: "Component, OnInit",
component: "Component, OnInit",
service: "Injectable",
middleware: "Injectable"
};
this.fileDecorators = {
page: (nm) => `@Component({
selector: 'app-${nm}',
templateUrl: './${this.nameFormat}.html',
styleUrls: ['./${this.nameFormat}.scss']
})`,
component: (nm) => `@Component({
selector: 'app-${nm}',
templateUrl: './${this.nameFormat}.html',
styleUrls: ['./${this.nameFormat}.scss']
})`,
service: (nm) => `@Injectable({
providedIn: 'root'
})`,
middleware: (nm) => `@Injectable({
providedIn: 'root'
})`
};
this.generateFileFormat = () => {
let format = {
nameFormat: this.nameFormat,
// @ts-ignore;
filesToGenerate: [...this.filesToGenerate[this.type]],
};
for (const ftype of format.filesToGenerate) {
// @ts-ignore;
format[ftype + '_content'] = this.generators[ftype].call(this, this.name, this.type);
}
return format;
};
this.generators = {
ts: (nm, type) => {
let name = nm.slice(0, 1).toUpperCase() + nm.slice(1);
let typ = type.slice(0, 1).toUpperCase() + type.slice(1);
let content = '';
// @ts-ignore;
content += `import { ${this.fileCoreImports[type]} } from '@angular/core';\n\n`;
// @ts-ignore;
content += `${this.fileDecorators[type].call(this, nm)}\nexport class ${name}${typ}`;
content += (type == 'page' || type == 'component' ? ` implements OnInit ` : '');
content += `{\n\nconstructor() { }\n\n`;
content += (type == 'page' || type == 'component' ? `\nngOnInit(): void { }\n` : '');
content += `}\n`;
return content;
},
scss: (nm, type) => {
return ``;
},
html: (nm, type) => {
return `<h3>${nm} ${type} works!</h3>`;
}
};
this.name = nm;
this.type = ftyp;
this.nameFormat = `${nm}.${ftyp}`;
}
}
exports.default = Files;