-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
61 lines (56 loc) · 1.52 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
const https = require("https");
const fs = require("fs");
const pkg = require("./package.json");
const NPM_RC_FILE =
"https://gist.githubusercontent.com/wayou/baa18849de3424db5d7ca24e94645c25/raw/.npmrc";
const YARN_RC_FILE =
"https://gist.githubusercontent.com/wayou/a1a6fb1fc5153bc20829c7b2700ec0bc/raw/.yarnrc";
/**
* download file
* @param {*} url url to download from
* @param {*} cb callback
*/
function fetchFile(url, fileName, cb) {
const file = fs.createWriteStream(fileName);
https
.get(url, function(response) {
const { statusCode } = response;
let error;
if (statusCode !== 200) {
error = new Error("Request Failed.\n" + `Status Code: ${statusCode}`);
}
if (error) {
console.error(error.message);
file.close();
return;
}
response.pipe(file);
file.on("finish", function() {
file.close(cb);
});
})
.on("error", error => {
console.error(`request error: ${error.message}`);
});
}
module.exports = function(type = "npm") {
if (type === "-v" || type === "-V" || type === "--version") {
console.log(pkg.version);
return;
}
type = type || "npm";
let fileUrl = NPM_RC_FILE,
fileName = ".npmrc";
if (type === "yarn") {
fileUrl = YARN_RC_FILE;
fileName = ".yarnrc";
} else if (type === "npm") {
fileUrl = NPM_RC_FILE;
} else {
console.error("usage: pkgrc [npm|yarn]");
return;
}
fetchFile(fileUrl, fileName, function() {
console.log(`${fileName} created`);
});
};