-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
126 lines (99 loc) · 4.66 KB
/
app.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
import init from "./init/init";
import * as shell from "shelljs"
import {createComponent} from "./generators/single/component"
import {createPipe} from "./generators/single/pipe"
import {createDirective} from "./generators/single/directive"
import {createService} from "./generators/single/service"
import {readJson} from "./helpers/helpers";
const args = process.argv.slice(2);
// Handling functions
function handleErr(err) {
console.error("error: ", err);
process.exit(0)
}
function handleRes(res) {
console.log(res);
process.exit(1)
}
function onCall(args: string[]): void {
if (args[0] === "init") {
init()
.catch(err => handleErr(err))
.then((val) => {
if (!val) shell.exec("npm install", (data) => process.exit(1));
else process.exit(1)
});
}
else {
// Read the json config file
readJson(true).then(
res => {
let baseLocation = "",
bootLocation = "",
componentLocation = "",
serviceLocation = "",
pipeLocation = "",
directiveLocation = "",
splicedLength = args[1].split("/").length;
if (res) {
baseLocation = res["appFolder"] ? `${res["appFolder"]}/` : baseLocation;
bootLocation = res["bootLocation"] ? baseLocation + res["bootLocation"] : baseLocation;
// If only the name of the folder is passed add the file in to the folder specified in the config
if (splicedLength === 1) {
componentLocation = res["defaultFolders"].components ? `${baseLocation + res["defaultFolders"].components}/` : componentLocation;
serviceLocation = res["defaultFolders"].services ? `${baseLocation + res["defaultFolders"].services}/` : serviceLocation;
directiveLocation = res["defaultFolders"].directives ? `${baseLocation + res["defaultFolders"].directives}/` : directiveLocation;
pipeLocation = res["defaultFolders"].pipes ? `${baseLocation + res["defaultFolders"].pipes}/` : pipeLocation;
}
// If the file has a dept set the base as the starting point
else {
componentLocation = baseLocation;
serviceLocation = baseLocation;
directiveLocation = baseLocation;
pipeLocation = baseLocation;
}
}
// Check witch function to call
switch (args[0]) {
// Component generation
case "c":
case "component":
// Check if the user specified that a html template should also be created
let createHtmlTemplate = args.indexOf("-t") > -1 || args.indexOf("-template") > -1;
createComponent(`${componentLocation + args[1]}`, createHtmlTemplate)
.catch(err => handleErr(err))
.then(val => handleRes(val));
break;
// Service generation
case "s":
case "service":
// Check if the user specified that the service should be injected in to boot
let bootInject = args.indexOf("-i") > -1 || args.indexOf("-inject") > -1;
createService(`${serviceLocation + args[1]}`, bootInject, bootLocation)
.catch(err => handleErr(err))
.then(val => handleRes(val));
break;
// Pipe generation
case "p":
case "pipe":
createPipe(`${pipeLocation + args[1]}`)
.catch(err => handleErr(err))
.then(val => handleRes(val));
break;
// Directive generation
case "d":
case "directive":
createDirective(`${directiveLocation + args[1]}`)
.catch(err => handleErr(err))
.then(val => handleRes(val));
break;
default:
console.error("Sorry that command isn't recognised.");
break;
}
}
)
}
}
onCall(args);