/
npm.js
262 lines (226 loc) Β· 8.22 KB
/
npm.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import path from 'node:path';
import semver from 'semver';
import urlJoin from 'url-join';
import Plugin from '../Plugin.js';
import { hasAccess, rejectAfter, parseVersion, readJSON, e } from '../../util.js';
import prompts from './prompts.js';
const docs = 'https://git.io/release-it-npm';
const options = { write: false };
const MANIFEST_PATH = './package.json';
const DEFAULT_TAG = 'latest';
const DEFAULT_TAG_PRERELEASE = 'next';
const NPM_BASE_URL = 'https://www.npmjs.com';
const fixArgs = args => (args ? (typeof args === 'string' ? args.split(' ') : args) : []);
class npm extends Plugin {
static isEnabled(options) {
return hasAccess(MANIFEST_PATH) && options !== false;
}
constructor(...args) {
super(...args);
this.registerPrompts(prompts);
}
async init() {
const { name, version: latestVersion, private: isPrivate, publishConfig } = readJSON(path.resolve(MANIFEST_PATH));
this.setContext({ name, latestVersion, private: isPrivate, publishConfig });
this.config.setContext({ npm: { name } });
const { publish, skipChecks } = this.options;
const timeout = Number(this.options.timeout) * 1000;
if (publish === false || isPrivate) return;
if (skipChecks) return;
const validations = Promise.all([this.isRegistryUp(), this.isAuthenticated(), this.getLatestRegistryVersion()]);
await Promise.race([validations, rejectAfter(timeout, e(`Timed out after ${timeout}ms.`, docs))]);
const [isRegistryUp, isAuthenticated, latestVersionInRegistry] = await validations;
if (!isRegistryUp) {
throw e(`Unable to reach npm registry (timed out after ${timeout}ms).`, docs);
}
if (!isAuthenticated) {
throw e('Not authenticated with npm. Please `npm login` and try again.', docs);
}
if (!(await this.isCollaborator())) {
const { username } = this.getContext();
throw e(`User ${username} is not a collaborator for ${name}.`, docs);
}
if (!latestVersionInRegistry) {
this.log.warn('No version found in npm registry. Assuming new package.');
} else {
if (!semver.eq(latestVersion, latestVersionInRegistry)) {
this.log.warn(
`Latest version in registry (${latestVersionInRegistry}) does not match package.json (${latestVersion}).`
);
}
}
}
getName() {
return this.getContext('name');
}
getLatestVersion() {
return this.options.ignoreVersion ? null : this.getContext('latestVersion');
}
async bump(version) {
const tag = this.options.tag || (await this.resolveTag(version));
this.setContext({ version, tag });
if (!this.config.isIncrement) return false;
const { versionArgs, allowSameVersion } = this.options;
const args = [version, '--no-git-tag-version', allowSameVersion && '--allow-same-version', ...fixArgs(versionArgs)];
const task = () => this.exec(`npm version ${args.filter(Boolean).join(' ')}`);
return this.spinner.show({ task, label: 'npm version' });
}
release() {
if (this.options.publish === false) return false;
const publish = () => this.publish({ otpCallback });
const otpCallback =
this.config.isCI && !this.config.isPromptOnlyVersion ? null : task => this.step({ prompt: 'otp', task });
return this.step({ task: publish, label: 'npm publish', prompt: 'publish' });
}
isRegistryUp() {
const registry = this.getRegistry();
const registryArg = registry ? ` --registry ${registry}` : '';
return this.exec(`npm ping${registryArg}`, { options }).then(
() => true,
err => {
if (/code E40[04]|404.*(ping not found|No content for path)/.test(err)) {
this.log.warn('Ignoring response from unsupported `npm ping` command.');
return true;
}
return false;
}
);
}
isAuthenticated() {
const registry = this.getRegistry();
const registryArg = registry ? ` --registry ${registry}` : '';
return this.exec(`npm whoami${registryArg}`, { options }).then(
output => {
const username = output ? output.trim() : null;
this.setContext({ username });
return true;
},
err => {
this.debug(err);
if (/code E40[04]/.test(err)) {
this.log.warn('Ignoring response from unsupported `npm whoami` command.');
return true;
}
return false;
}
);
}
isCollaborator() {
const registry = this.getRegistry();
const registryArg = registry ? ` --registry ${registry}` : '';
const name = this.getName();
const { username } = this.getContext();
if (username === undefined) return true;
if (username === null) return false;
return this.exec(`npm access ls-collaborators ${name}${registryArg}`, { options }).then(
output => {
try {
const collaborators = JSON.parse(output);
const permissions = collaborators[username];
return permissions && permissions.includes('write');
} catch (err) {
this.debug(err);
return false;
}
},
err => {
this.debug(err);
if (/code E400/.test(err)) {
this.log.warn('Ignoring response from unsupported `npm access` command.');
} else {
this.log.warn(`Unable to verify if user ${username} is a collaborator for ${name}.`);
}
return true;
}
);
}
async getLatestRegistryVersion() {
const registry = this.getRegistry();
const registryArg = registry ? ` --registry ${registry}` : '';
const name = this.getName();
const latestVersion = this.getLatestVersion();
const tag = await this.resolveTag(latestVersion);
return this.exec(`npm show ${name}@${tag} version${registryArg}`, { options }).catch(() => null);
}
getRegistryPreReleaseTags() {
return this.exec(`npm view ${this.getName()} dist-tags --json`, { options }).then(
output => {
try {
const tags = JSON.parse(output);
return Object.keys(tags).filter(tag => tag !== DEFAULT_TAG);
} catch (err) {
this.debug(err);
return [];
}
},
() => []
);
}
getPackageUrl() {
const baseUrl = this.getRegistry() || NPM_BASE_URL;
return urlJoin(baseUrl, 'package', this.getName());
}
getRegistry() {
const { publishConfig } = this.getContext();
const registries = publishConfig
? publishConfig.registry
? [publishConfig.registry]
: Object.keys(publishConfig)
.filter(key => key.endsWith('registry'))
.map(key => publishConfig[key])
: [];
return registries[0];
}
async guessPreReleaseTag() {
const [tag] = await this.getRegistryPreReleaseTags();
if (tag) {
return tag;
} else {
this.log.warn(`Unable to get pre-release tag(s) from npm registry. Using "${DEFAULT_TAG_PRERELEASE}".`);
return DEFAULT_TAG_PRERELEASE;
}
}
async resolveTag(version) {
const { tag } = this.options;
const { isPreRelease, preReleaseId } = parseVersion(version);
if (!isPreRelease) {
return DEFAULT_TAG;
} else {
return tag || preReleaseId || (await this.guessPreReleaseTag());
}
}
async publish({ otp = this.options.otp, otpCallback } = {}) {
const { publishPath = '.', publishArgs } = this.options;
const { private: isPrivate, tag = DEFAULT_TAG } = this.getContext();
const otpArg = otp ? `--otp ${otp}` : '';
const dryRunArg = this.config.isDryRun ? '--dry-run' : '';
if (isPrivate) {
this.log.warn('Skip publish: package is private.');
return false;
}
const args = [publishPath, `--tag ${tag}`, otpArg, dryRunArg, ...fixArgs(publishArgs)].filter(Boolean);
return this.exec(`npm publish ${args.join(' ')}`, { options })
.then(() => {
this.setContext({ isReleased: true });
})
.catch(err => {
this.debug(err);
if (/one-time pass/.test(err)) {
if (otp != null) {
this.log.warn('The provided OTP is incorrect or has expired.');
}
if (otpCallback) {
return otpCallback(otp => this.publish({ otp, otpCallback }));
}
}
throw err;
});
}
afterRelease() {
const { isReleased } = this.getContext();
if (isReleased) {
this.log.log(`π ${this.getPackageUrl()}`);
}
}
}
export default npm;