-
Notifications
You must be signed in to change notification settings - Fork 582
/
getStackOutput.js
65 lines (54 loc) · 1.6 KB
/
getStackOutput.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
const execa = require("execa");
const mapStackOutput = require("./mapStackOutput");
const { getProject } = require("@webiny/cli/utils");
const cache = {};
const getOutputJson = ({ folder, env, cwd }) => {
const project = getProject();
if (cache[folder + env]) {
return cache[folder + env];
}
try {
const { stdout } = execa.sync(
"yarn",
["webiny", "output", folder, "--env", env, "--json", "--no-debug"].filter(Boolean),
{
cwd: cwd || project.root
}
);
// Let's get the output after the first line break. Everything before is just yarn stuff.
const extractedJSON = stdout.substring(stdout.indexOf("{"));
return (cache[folder + env] = JSON.parse(extractedJSON));
} catch (e) {
return null;
}
};
module.exports = (folderOrArgs, env, map) => {
if (!folderOrArgs) {
throw new Error("Missing initial argument.");
}
// Normalize arguments.
let args = {};
if (typeof folderOrArgs === "string") {
args = {
folder: folderOrArgs,
env: env,
map: map
};
} else {
args = folderOrArgs;
}
if (!args.folder) {
throw new Error(`Please specify a project application folder, for example "apps/admin".`);
}
if (!args.env) {
throw new Error(`Please specify environment, for example "dev".`);
}
const output = getOutputJson(args);
if (!output) {
return output;
}
if (!args.map) {
return output;
}
return mapStackOutput(output, args.map);
};