Skip to content

Commit c7c607a

Browse files
committed
[INTERNAL] Make all command handlers async and propagate errors to base middleware
1 parent 160b8f7 commit c7c607a

File tree

6 files changed

+96
-125
lines changed

6 files changed

+96
-125
lines changed

lib/cli/commands/build.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,29 @@ build.builder = function(cli) {
7070
"Build project and dependencies in dev mode. Only a set of essential tasks is executed.");
7171
};
7272

73-
function handleBuild(argv) {
73+
async function handleBuild(argv) {
7474
const normalizer = require("@ui5/project").normalizer;
7575
const builder = require("@ui5/builder").builder;
7676
const logger = require("@ui5/logger");
7777

7878
const command = argv._[argv._.length - 1];
7979
logger.setShowProgress(true);
8080

81-
return normalizer.generateProjectTree({
81+
const tree = await normalizer.generateProjectTree({
8282
translatorName: argv.translator,
8383
configPath: argv.config
84-
}).then(function(tree) {
85-
return builder.build({
86-
tree: tree,
87-
destPath: argv.dest,
88-
cleanDest: argv["clean-dest"],
89-
buildDependencies: argv.all,
90-
dev: command === "dev",
91-
selfContained: command === "self-contained",
92-
jsdoc: command === "jsdoc",
93-
devExcludeProject: argv["dev-exclude-project"],
94-
includedTasks: argv["include-task"],
95-
excludedTasks: argv["exclude-task"]
96-
});
84+
});
85+
await builder.build({
86+
tree: tree,
87+
destPath: argv.dest,
88+
cleanDest: argv["clean-dest"],
89+
buildDependencies: argv.all,
90+
dev: command === "dev",
91+
selfContained: command === "self-contained",
92+
jsdoc: command === "jsdoc",
93+
devExcludeProject: argv["dev-exclude-project"],
94+
includedTasks: argv["include-task"],
95+
excludedTasks: argv["exclude-task"]
9796
});
9897
}
9998

lib/cli/commands/init.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@ initCommand.handler = async function() {
1414
const jsYaml = require("js-yaml");
1515
const writeFile = promisify(fs.writeFile);
1616

17-
try {
18-
const yamlPath = path.resolve("./ui5.yaml");
19-
if (await fsHelper.exists(yamlPath)) {
20-
throw new Error("Initialization not possible: ui5.yaml already exists");
21-
}
17+
const yamlPath = path.resolve("./ui5.yaml");
18+
if (await fsHelper.exists(yamlPath)) {
19+
throw new Error("Initialization not possible: ui5.yaml already exists");
20+
}
2221

23-
const projectConfig = await init.init();
24-
const yaml = jsYaml.safeDump(projectConfig);
22+
const projectConfig = await init.init();
23+
const yaml = jsYaml.safeDump(projectConfig);
2524

26-
await writeFile(yamlPath, yaml);
27-
console.log(`Wrote ui5.yaml to ${yamlPath}:\n`);
28-
console.log(yaml);
29-
} catch (err) {
30-
console.error(err);
31-
process.exit(1);
32-
}
25+
await writeFile(yamlPath, yaml);
26+
console.log(`Wrote ui5.yaml to ${yamlPath}:\n`);
27+
console.log(yaml);
3328
};
3429

3530
module.exports = initCommand;

lib/cli/commands/serve.js

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -54,79 +54,69 @@ serve.builder = function(cli) {
5454
"Listen to port 1337 and launch default browser with http://localhost:1337/test/QUnit.html");
5555
};
5656

57-
serve.handler = function(argv) {
57+
serve.handler = async function(argv) {
5858
const normalizer = require("@ui5/project").normalizer;
5959
const ui5Server = require("@ui5/server");
6060
const server = ui5Server.server;
6161

62-
normalizer.generateProjectTree({
62+
const tree = await normalizer.generateProjectTree({
6363
translatorName: argv.translator,
6464
configPath: argv.config
65-
}).then(function(tree) {
66-
let port = argv.port;
67-
let changePortIfInUse = false;
65+
});
66+
let port = argv.port;
67+
let changePortIfInUse = false;
6868

69-
if (!port && tree.server && tree.server.settings) {
70-
if (argv.h2) {
71-
port = tree.server.settings.httpsPort;
72-
} else {
73-
port = tree.server.settings.httpPort;
74-
}
69+
if (!port && tree.server && tree.server.settings) {
70+
if (argv.h2) {
71+
port = tree.server.settings.httpsPort;
72+
} else {
73+
port = tree.server.settings.httpPort;
7574
}
75+
}
7676

77-
if (!port) {
78-
changePortIfInUse = true; // only change if port isn't explicitly set
79-
if (argv.h2) {
80-
port = 8443;
81-
} else {
82-
port = 8080;
83-
}
77+
if (!port) {
78+
changePortIfInUse = true; // only change if port isn't explicitly set
79+
if (argv.h2) {
80+
port = 8443;
81+
} else {
82+
port = 8080;
8483
}
84+
}
8585

86-
const serverConfig = {
87-
port,
88-
changePortIfInUse,
89-
h2: argv.h2,
90-
acceptRemoteConnections: !!argv.acceptRemoteConnections,
91-
cert: argv.h2 ? argv.cert : undefined,
92-
key: argv.h2 ? argv.key : undefined,
93-
sendSAPTargetCSP: !!argv.sapCspPolicies
94-
};
86+
const serverConfig = {
87+
port,
88+
changePortIfInUse,
89+
h2: argv.h2,
90+
acceptRemoteConnections: !!argv.acceptRemoteConnections,
91+
cert: argv.h2 ? argv.cert : undefined,
92+
key: argv.h2 ? argv.key : undefined,
93+
sendSAPTargetCSP: !!argv.sapCspPolicies
94+
};
9595

96-
if (!serverConfig.h2) {
97-
return {serverConfig, tree};
98-
} else {
99-
return ui5Server.sslUtil.getSslCertificate(serverConfig.key, serverConfig.cert).then(({key, cert}) => {
100-
serverConfig.key = key;
101-
serverConfig.cert = cert;
102-
return {serverConfig, tree};
103-
});
104-
}
105-
}).then(({serverConfig, tree}) => {
106-
return server.serve(tree, serverConfig).then(function({h2, port}) {
107-
const protocol = h2 ? "https" : "http";
108-
let browserUrl = protocol + "://localhost:" + port;
109-
console.log("Server started");
110-
console.log("URL: " + browserUrl);
96+
if (serverConfig.h2) {
97+
const {key, cert} = await ui5Server.sslUtil.getSslCertificate(serverConfig.key, serverConfig.cert);
98+
serverConfig.key = key;
99+
serverConfig.cert = cert;
100+
}
111101

112-
if (argv.open !== undefined) {
113-
if (typeof argv.open === "string") {
114-
let relPath = argv.open || "/";
115-
if (!relPath.startsWith("/")) {
116-
relPath = "/" + relPath;
117-
}
118-
browserUrl += relPath;
119-
}
120-
const open = require("open");
121-
open(browserUrl);
122-
}
102+
const {h2, port: actualPort} = await server.serve(tree, serverConfig);
123103

124-
return browserUrl;
125-
});
126-
}).catch(function(err) {
127-
console.error(err);
128-
process.exit(1);
129-
});
104+
const protocol = h2 ? "https" : "http";
105+
let browserUrl = protocol + "://localhost:" + actualPort;
106+
console.log("Server started");
107+
console.log("URL: " + browserUrl);
108+
109+
if (argv.open !== undefined) {
110+
if (typeof argv.open === "string") {
111+
let relPath = argv.open || "/";
112+
if (!relPath.startsWith("/")) {
113+
relPath = "/" + relPath;
114+
}
115+
browserUrl += relPath;
116+
}
117+
const open = require("open");
118+
open(browserUrl);
119+
}
130120
};
131121

132122
module.exports = serve;

lib/cli/commands/tree.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ tree.builder = function(cli) {
2626
.example("ui5 tree --json > tree.json", "Pipes the dependency tree into a new file \"tree.json\"");
2727
};
2828

29-
tree.handler = function(argv) {
29+
tree.handler = async function(argv) {
3030
const normalizer = require("@ui5/project").normalizer;
3131
const treeify = require("treeify");
3232

33-
let p;
3433
const options = {
3534
translatorName: argv.translator,
3635
translatorOptions: {
@@ -39,19 +38,13 @@ tree.handler = function(argv) {
3938
configPath: argv.config
4039
};
4140
if (argv.full) {
42-
p = normalizer.generateProjectTree(options);
41+
await normalizer.generateProjectTree(options);
4342
} else {
44-
p = normalizer.generateDependencyTree(options);
43+
await normalizer.generateDependencyTree(options);
4544
}
4645

47-
p.then(function(tree) {
48-
const output = argv.json ? JSON.stringify(tree, null, 4) : treeify.asTree(tree, true);
49-
console.log(output);
50-
return output;
51-
}).catch(function(err) {
52-
console.error(err);
53-
process.exit(1);
54-
});
46+
const output = argv.json ? JSON.stringify(tree, null, 4) : treeify.asTree(tree, true);
47+
console.log(output);
5548
};
5649

5750
module.exports = tree;

lib/cli/commands/versions.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,21 @@ versions.getVersion = (pkg) => {
1717
}
1818
};
1919

20-
versions.handler = () => {
21-
try {
22-
const cliVersion = versions.getVersion("../../..");
23-
const builderVersion = versions.getVersion("@ui5/builder");
24-
const serverVersion = versions.getVersion("@ui5/server");
25-
const fsVersion = versions.getVersion("@ui5/fs");
26-
const projectVersion = versions.getVersion("@ui5/project");
27-
const loggerVersion = versions.getVersion("@ui5/logger");
28-
console.log(`
29-
@ui5/cli: ${cliVersion}
30-
@ui5/builder: ${builderVersion}
31-
@ui5/server: ${serverVersion}
32-
@ui5/fs: ${fsVersion}
33-
@ui5/project: ${projectVersion}
34-
@ui5/logger: ${loggerVersion}
35-
`);
36-
} catch (err) {
37-
console.error(err);
38-
process.exit(1);
39-
}
20+
versions.handler = async function() {
21+
const cliVersion = versions.getVersion("../../..");
22+
const builderVersion = versions.getVersion("@ui5/builder");
23+
const serverVersion = versions.getVersion("@ui5/server");
24+
const fsVersion = versions.getVersion("@ui5/fs");
25+
const projectVersion = versions.getVersion("@ui5/project");
26+
const loggerVersion = versions.getVersion("@ui5/logger");
27+
console.log(`
28+
@ui5/cli: ${cliVersion}
29+
@ui5/builder: ${builderVersion}
30+
@ui5/server: ${serverVersion}
31+
@ui5/fs: ${fsVersion}
32+
@ui5/project: ${projectVersion}
33+
@ui5/logger: ${loggerVersion}
34+
`);
4035
};
4136

4237
module.exports = versions;

test/lib/cli/commands/versions.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ test.serial("Error: returns not installed if version was not found", (t) => {
2020
t.is(versions.getVersion("not/existing/path"), "===(not installed)", "No version found");
2121
});
2222

23-
test.serial("Error: kills process if error occurred while processing", (t) => {
23+
test.serial("Error: throws with error if error occurred while processing", async (t) => {
2424
sinon.stub(process, "exit");
25-
sinon.stub(versions, "getVersion").throws("Error occurred");
26-
versions.handler({});
27-
t.is(process.exit.getCall(0).args[0], 1, "Process was killed with code 1");
28-
process.exit.restore();
25+
sinon.stub(versions, "getVersion").throws(new Error("Error occurred"));
26+
const error = await t.throwsAsync(versions.handler({}));
27+
t.deepEqual(error.message, "Error occurred", "Correct error message thrown");
2928
});

0 commit comments

Comments
 (0)