-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathutil.js
92 lines (75 loc) · 2.13 KB
/
util.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
'use strict';
const execa = require('execa');
const pTimeout = require('p-timeout');
const ow = require('ow');
const npmName = require('npm-name');
const version = require('../version');
exports.checkConnection = () => pTimeout(
(async () => {
try {
await execa('npm', ['ping']);
return true;
} catch (_) {
throw new Error('Connection to npm registry failed');
}
})(),
15000,
'Connection to npm registry timed out'
);
exports.username = async ({externalRegistry}) => {
const args = ['whoami'];
if (externalRegistry) {
args.push('--registry', externalRegistry);
}
try {
return await execa.stdout('npm', args);
} catch (error) {
throw new Error(/ENEEDAUTH/.test(error.stderr) ?
'You must be logged in. Use `npm login` and try again.' :
'Authentication error. Use `npm whoami` to troubleshoot.');
}
};
exports.collaborators = async packageName => {
ow(packageName, ow.string);
try {
return await execa.stdout('npm', ['access', 'ls-collaborators', packageName]);
} catch (error) {
// Ignore non-existing package error
if (error.stderr.includes('code E404')) {
return false;
}
throw error;
}
};
exports.prereleaseTags = async packageName => {
ow(packageName, ow.string);
let tags = [];
try {
const {stdout} = await execa('npm', ['view', '--json', packageName, 'dist-tags']);
tags = Object.keys(JSON.parse(stdout))
.filter(tag => tag !== 'latest');
} catch (error) {
if (((JSON.parse(error.stdout) || {}).error || {}).code !== 'E404') {
throw error;
}
}
if (tags.length === 0) {
tags.push('next');
}
return tags;
};
exports.isPackageNameAvailable = async pkg => {
const isExternalRegistry = exports.isExternalRegistry(pkg);
if (isExternalRegistry) {
return true;
}
return npmName(pkg.name);
};
exports.isExternalRegistry = pkg => typeof pkg.publishConfig === 'object' && typeof pkg.publishConfig.registry === 'string';
exports.version = () => execa.stdout('npm', ['--version']);
exports.verifyRecentNpmVersion = async () => {
const npmVersion = await exports.version();
if (version(npmVersion).satisfies('<6.8.0')) {
throw new Error('Please upgrade to npm@6.8.0 or newer');
}
};