-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·162 lines (141 loc) · 5.05 KB
/
index.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const inquirer = require("inquirer");
const commander = require("commander");
inquirer.registerPrompt('path', require("inquirer-path").PathPrompt);
const files = require("./lib/files");
const ui5init = require("./lib/ui5init");
const jsGenerator = require("./lib/jsGenerator");
const viewGenerator = require("./lib/viewGenerator");
const runtime = require("./lib/runtime");
// Inquirer questions
const questions = [{
type: "input",
name: "appTitle",
message: "Project title",
default: files.capitalize(path.basename(files.projectPath())),
validate: answer => answer ? true : "Project title is required"
}, {
type: "input",
name: "appDescription",
message: "Project description"
}, {
type: "input",
name: "packageName",
message: "Package name",
validate: answer => {
let pattern = new RegExp("^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]$");
return answer && pattern.test(answer) ? true : "Invalid package name";
}
}, {
type: "list",
name: "type",
message: "View type",
choices: [{ name: "Single view", value: "single" }, { name: "Split view", value: "split" }]
}, {
type: "confirm",
name: "routing",
message: "Use routing",
when: (answers) => {
return answers.type === "single";
}
}, {
type: "path",
name: "openui5",
message: "Path to OpenUI5",
validate: answer => answer && files.directoryExists(answer) ? true : `The path "${answer} does not exist`
}];
// Setup command line argument parsing
commander
.command("init")
.description("Initialize a new app")
.action((cmd, options) => {
init();
});
commander
.command("add <name>")
.option("-c, --controller", "Create a new controller")
.option("-v, --view", "Create a new view")
.action((name, options) => {
if (!name) {
console.log("No name provided");
}
generateFile(name, options.controller, options.view);
});
commander.parse(process.argv);
if (!process.argv.slice(2).length) {
commander.outputHelp();
}
/**
* Initialize a new UI5 application.
*/
function init() {
inquirer.prompt(questions).then(answers => {
let appTitle = answers.appTitle.trim();
let appDescription = answers.appDescription ? answers.appDescription.trim() : "";
let packageName = answers.packageName.trim();
let openui5 = answers.openui5.trim();
let appType = answers.type.trim();
let splitApp = appType === "split";
let routing = answers.routing || splitApp;
ui5init.createFolders();
ui5init.createComponent(packageName, routing);
ui5init.createManifest(packageName, appType, routing);
ui5init.createIndex(packageName, appTitle);
ui5init.createI18n(appTitle, appDescription);
jsGenerator.generateFile("App", packageName, jsGenerator.types.controller);
viewGenerator.generateAppView(packageName, splitApp, routing);
if (routing || splitApp) {
jsGenerator.generateFile("Master", packageName, jsGenerator.types.controller);
viewGenerator.generateView("Master", packageName);
}
if (splitApp) {
jsGenerator.generateFile("Detail", packageName, jsGenerator.types.controller);
viewGenerator.generateView("Detail", packageName);
}
runtime.createPackage(appTitle, appDescription);
runtime.createServer(openui5);
runtime.createGruntEnvironment();
console.log(`Application "${appTitle}" initialized.`);
console.log(`Run npm install && npm start to install and start the app.`);
});
}
/**
* Generate a new controller or view.
*
* @param {string} name The name of the controller or view
* @param {boolean} controller Flag indicating if a controller should be created
* @param {boolean} view Flag indicating if a view should be created
*/
function generateFile(name, controller, view) {
let manifestPath = `${files.getWebappPath()}/manifest.json`;
let manifest = require(manifestPath);
if (!manifest || !manifest["sap.app"].id) {
console.log(error);
console.log("Not a UI5 project");
return;
}
let packageName = manifest["sap.app"].id;
name = files.capitalize(name);
if (controller || (!controller && !view)) {
jsGenerator.generateFile(name, packageName, jsGenerator.types.controller);
}
if (view || (!controller && !view)) {
viewGenerator.generateView(name, packageName);
if (manifest["sap.ui5"] && manifest["sap.ui5"].routing) {
let routing = manifest["sap.ui5"].routing;
let lowerName = files.lowercase(name);
routing.routes.push({
"pattern": lowerName,
"name": lowerName,
"target": lowerName
});
routing.targets[lowerName] = {
"viewID": lowerName,
"viewName": name
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
}
}
}