-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateComp.js
More file actions
105 lines (91 loc) · 3.1 KB
/
createComp.js
File metadata and controls
105 lines (91 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var fs = require('fs');
var prompt = require('prompt');
var colors = require('colors');
var centerPrint = (str, length) => {
var extraPadding = length - str.length;
var centeredString = str + " ".repeat(extraPadding);
var test = centeredString.split('');
test.unshift(" ".repeat(extraPadding/2));
test.splice(extraPadding/2 + str.length, extraPadding/2);
return test.join('');
};
console.log(colors.cyan("*".repeat(45)));
console.log(colors.cyan(centerPrint("Angular 2 Component Creator", 45)));
console.log(colors.cyan("*".repeat(45)));
prompt.message = '';
prompt.start();
const inputList = [
{
name: 'basePath',
description: colors.gray('Enter your base app path'),
type: 'string',
default: 'app'
},
{
name: 'componentName',
description: colors.gray('Enter your component name'),
type: 'string',
default: 'my-element'
},
];
prompt.get(inputList, function(err, result) {
if (err) {
return onErr(err);
}
createComponent(result.basePath, result.componentName, getClassName(result.componentName));
});
function onErr(err) {
console.log(err);
return 1;
}
var capitalize = (str) => {
return str.substring(0, 1).toUpperCase() + str.substring(1);
};
var getClassName = (componentName) => {
return componentName.split('-').map((item) => {
return capitalize(item);
}).join('') + 'Component';
};
var getComponentContent = (basePath, componentName, className) => {
return `import { Component } from '@angular/core';
@Component({
selector: '${componentName}',
templateUrl: '${basePath}/${componentName}/${componentName}.component.html',
styleUrls: ['${basePath}/${componentName}/${componentName}.component.css']
})
export class ${className} { }
`;
};
var getHtmlContent = (className) => {
return `<div>
This is ${className}
</div>
`;
};
var createComponent = (basePath, componentName, className) => {
try {
console.log(colors.cyan("*".repeat(45)));
try {
process.chdir(basePath);
}
catch (e) {
console.log(colors.yellow("ERROR: Cannot change dir to base [" + basePath + "]"));
console.log(colors.green("Creating new base"));
fs.mkdirSync(basePath);
process.chdir(basePath);
}
console.log(colors.green("Creating directory", componentName));
fs.mkdirSync(componentName);
process.chdir(componentName);
console.log(colors.green("Creating Component " + componentName + '.component.ts'));
fs.writeFileSync(componentName + '.component.ts', getComponentContent(basePath, componentName, className));
console.log(colors.green("Creating HTML " + componentName + '.component.html'));
fs.writeFileSync(componentName + '.component.html', getHtmlContent(className));
console.log(colors.green("Creating CSS " + componentName + '.component.css'));
fs.writeFileSync(componentName + '.component.css', '');
console.log(colors.cyan("*".repeat(45)));
}
catch (e) {
console.error(colors.red("ERROR: ", e));
}
};