-
Notifications
You must be signed in to change notification settings - Fork 250
/
helpers.js
110 lines (95 loc) · 2.48 KB
/
helpers.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
const fs = require('fs');
const ini = require('ini');
const chalk = require('chalk');
const process = require('./process');
const { NRMRC, NPMRC, REGISTRY, REGISTRIES } = require('./constants');
async function readFile(file) {
return new Promise(resolve => {
if (!fs.existsSync(file)) {
resolve({});
} else {
try {
const content = ini.parse(fs.readFileSync(file, 'utf-8'));
resolve(content);
} catch (error) {
exit(error);
}
}
});
}
async function writeFile(path, content) {
return new Promise(resolve => {
try {
fs.writeFileSync(path, ini.stringify(content));
resolve();
} catch (error) {
exit(error);
}
});
}
function padding(message = '', before = 1, after = 1) {
return new Array(before).fill(' ').join('') + message + new Array(after).fill(' ').join('');
}
function printSuccess(message) {
console.log(chalk.bgGreenBright(padding('SUCCESS')) + ' ' + message);
}
function printError(error) {
console.error(chalk.bgRed(padding('ERROR')) + ' ' + chalk.red(error));
}
function printMessages(messages) {
for (const message of messages) {
console.log(message);
}
}
function geneDashLine(message, length) {
const finalMessage = new Array(Math.max(2, length - message.length + 2)).join('-');
return padding(chalk.dim(finalMessage));
}
function isLowerCaseEqual(str1, str2) {
if (str1 && str2) {
return str1.toLowerCase() === str2.toLowerCase();
} else {
return !str1 && !str2;
}
}
async function getCurrentRegistry() {
const npmrc = await readFile(NPMRC);
return npmrc[REGISTRY];
}
async function getRegistries() {
const customRegistries = await readFile(NRMRC);
return Object.assign({}, REGISTRIES, customRegistries);
}
async function isRegistryNotFound(name, printErr = true) {
const registries = await getRegistries();
if (!Object.keys(registries).includes(name)) {
printErr && printError(`The registry '${name}' is not found.`);
return true;
}
return false;
}
async function isInternalRegistry(name, handle) {
if (Object.keys(REGISTRIES).includes(name)) {
handle && printError(`You cannot ${handle} the nrm internal registry.`);
return true;
}
return false;
}
function exit(error) {
error && printError(error);
process.exit(1);
}
module.exports = {
exit,
geneDashLine,
printError,
printSuccess,
printMessages,
isLowerCaseEqual,
readFile,
writeFile,
getRegistries,
getCurrentRegistry,
isRegistryNotFound,
isInternalRegistry,
};