-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
126 lines (112 loc) · 3.49 KB
/
api.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
const exec = require("child_process").exec;
const fs = require("fs");
const path = require("path");
const canUseYarn = () => {
return fs.existsSync("yarn.lock");
};
const useYarn = canUseYarn();
const commands = {
list: "npm list --depth=0 --json --silent",
outdated: "npm outdated --json --silent",
update: "npm update --silent"
};
if (useYarn) {
commands.list = "yarn list --depth=0 --json --no-progress --silent";
commands.update = "yarn upgrade --silent";
//outdated is safe to be used by npm
}
const yarnGetPkgName = pkg => {
return pkg.name.substr(0, pkg.name.indexOf("@"));
};
const yarnGetPkgVersion = pkg => {
return pkg.name.substr(pkg.name.indexOf("@") + 1);
};
const options = { maxBuffer: 1024 * 100000 };
module.exports = {
routes: {
get: {
"package-json": (req, res) => {
const appRoot = req.app.locals.appRoot;
exec(commands.list, options, (error, list) => {
/* unexpected behavior in npm
* npm list returns status code 1 when unmet deps
* thus we're not checking for error */
let result = {};
if (!list){
return res.send({ result });
}
let installed = [];
try {
installed = JSON.parse(list);
if (!installed){
return res.send({ result })
}
} catch (error) {
console.log('There was an error');
console.log(error);
return res.send({ result })
}
if (!fs.existsSync(`${appRoot}/package.json`)) {
return res.send({ result });
}
if (useYarn) {
installed = installed.data.trees;
//need to filter out packages that are not in package.json
const appRoot = req.app.locals.appRoot;
const packageJson = require(`${appRoot}/package.json`);
const deps = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.devDependencies || {})
];
installed = installed.filter(pkg =>
deps.includes(yarnGetPkgName(pkg))
);
let result = {};
for (const pkg of installed) {
result[yarnGetPkgName(pkg)] = {
version: yarnGetPkgVersion(pkg)
};
}
res.send({ result });
} else {
try {
result = installed.dependencies;
} catch (error) {}
res.send({ result });
}
});
},
outdated: (req, res) => {
exec(commands.outdated, options, (error, outdated) => {
/* unexpected behavior in npm
* npm outdated returns status code 1 rather than 0
* thus we're not checking for error */
//outdated will be empty if there are no outdated packages
if (!outdated) {
return res.send({ result: false });
}
let result = {};
try {
result = JSON.parse(outdated);
} catch (error) {}
res.send({ result });
});
}
},
post: {
update: (req, res) => {
const body = req.body;
if (!body || !body.name) {
res.send(false);
}
exec(`${commands.update} ${body.name}`, options, (error, response) => {
if (error !== null) {
console.log("exec error: " + error);
return;
}
res.send({ result: true });
});
}
}
}
};