forked from sindresorhus/np
/
util.js
167 lines (137 loc) · 4.3 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
const fs = require('fs');
const path = require('path');
const execa = require('execa');
const pTimeout = require('p-timeout');
const ow = require('ow');
const npmName = require('npm-name');
const chalk = require('chalk');
const pkgDir = require('pkg-dir');
const ignoreWalker = require('ignore-walk');
const minimatch = require('minimatch');
const {verifyRequirementSatisfied} = 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 {
const {stdout} = await execa('npm', args);
return stdout;
} 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 {
const {stdout} = await execa('npm', ['access', 'ls-collaborators', packageName]);
return stdout;
} 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 args = [pkg.name];
if (exports.isExternalRegistry(pkg)) {
args.push({
registryUrl: pkg.publishConfig.registry
});
}
return npmName(...args);
};
exports.isExternalRegistry = pkg => typeof pkg.publishConfig === 'object' && typeof pkg.publishConfig.registry === 'string';
exports.version = async () => {
const {stdout} = await execa('npm', ['--version']);
return stdout;
};
exports.verifyRecentNpmVersion = async () => {
const npmVersion = await exports.version();
verifyRequirementSatisfied('npm', npmVersion);
};
exports.checkIgnoreStrategy = ({files}) => {
const rootDir = pkgDir.sync();
const npmignoreExists = fs.existsSync(path.resolve(rootDir, '.npmignore'));
if (!files && !npmignoreExists) {
console.log(`
\n${chalk.bold.yellow('Warning:')} No ${chalk.bold.cyan('files')} field specified in ${chalk.bold.magenta('package.json')} nor is a ${chalk.bold.magenta('.npmignore')} file present. Having one of those will prevent you from accidentally publishing development-specific files along with your package's source code to npm.
`);
}
};
// New files added part of .npmignore or not part of files-attribute in "package.json"
exports.checkNewFiles = async (newFiles, filesFromFileAttribute) => {
const rootDir = pkgDir.sync();
const npmignoreExists = fs.existsSync(path.resolve(rootDir, '.npmignore'));
let result = [];
if (filesFromFileAttribute) {
result = getFilesNotPartOfFileAttribute(filesFromFileAttribute, newFiles);
}
if (npmignoreExists) {
result = result.concat(await getFilesIgnoredByDotnpmignore(newFiles));
}
return result;
};
async function getFilesIgnoredByDotnpmignore(fileList) {
if (!Array.isArray(fileList)) {
throw new TypeError('expected array, but got {typeof fileList}');
}
const result = [];
const whiteList = await ignoreWalker({
path: pkgDir.sync(),
ignoreFiles: ['.npmignore']
});
for (const file of fileList) {
if (!whiteList.includes(file)) {
result.push(file);
}
}
return result;
}
function getFilesNotPartOfFileAttribute(filesFromFileAttribute, fileList) {
if (!Array.isArray(fileList)) {
throw new TypeError(`expected array, but got ${typeof fileList}`);
}
return fileList.filter(minimatch.filter(getGlobPattern(filesFromFileAttribute),
{matchBase: true}));
}
function getGlobPattern(filesFromFileAttribute) {
if (filesFromFileAttribute.length === 1) {
return '!' + filesFromFileAttribute[0];
}
return '!{' + filesFromFileAttribute.join(',') + '}';
}