Repository navigation
Expand file tree
/
Copy pathnpm.js
More file actions
144 lines (130 loc) · 4.24 KB
/
Copy pathnpm.js
File metadata and controls
144 lines (130 loc) · 4.24 KB
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
// @flow
import { BoltError } from './errors';
import * as logger from './logger';
import * as messages from './messages';
import * as processes from './processes';
import pLimit from 'p-limit';
const npmRequestLimit = pLimit(40);
function getCorrectRegistry() {
let registry =
process.env.npm_config_registry === 'https://registry.yarnpkg.com'
? undefined
: process.env.npm_config_registry;
return registry;
}
export function info(pkgName: string) {
return npmRequestLimit(async () => {
logger.info(messages.npmInfo(pkgName));
// Due to a couple of issues with yarnpkg, we also want to override the npm registry when doing
// npm info.
// Issues: We sometimes get back cached responses, i.e old data about packages which causes
// `publish` to behave incorrectly. It can also cause issues when publishing private packages
// as they will always give a 404, which will tell `publish` to always try to publish.
// See: https://github.com/yarnpkg/yarn/issues/2935#issuecomment-355292633
const envOverride = {
npm_config_registry: getCorrectRegistry()
};
let result = await processes.spawn('npm', ['info', pkgName, '--json'], {
silent: true,
env: Object.assign({}, process.env, envOverride)
});
return JSON.parse(result.stdout);
});
}
export async function infoAllow404(pkgName: string) {
try {
let pkgInfo = await info(pkgName);
return { published: true, pkgInfo };
} catch (error) {
let output = JSON.parse(error.stdout);
if (output.error && output.error.code === 'E404') {
logger.warn(messages.npmInfo404(pkgName));
return { published: false, pkgInfo: {} };
}
throw error;
}
}
export function publish(
pkgName: string,
opts: { cwd?: string, access?: string } = {}
) {
return npmRequestLimit(async () => {
logger.info(messages.npmPublish(pkgName));
let publishFlags = opts.access ? ['--access', opts.access] : [];
try {
// Due to a super annoying issue in yarn, we have to manually override this env variable
// See: https://github.com/yarnpkg/yarn/issues/2935#issuecomment-355292633
const envOverride = {
npm_config_registry: getCorrectRegistry()
};
await processes.spawn('npm', ['publish', ...publishFlags], {
cwd: opts.cwd,
env: Object.assign({}, process.env, envOverride)
});
return { published: true };
} catch (error) {
// Publish failed
return { published: false };
}
});
}
export function addTag(pkgName: string, pkgVersion: string, tag: string) {
return npmRequestLimit(async () => {
logger.info(messages.npmDistTagAdd(pkgName, pkgVersion, tag));
let pkgStr = `${pkgName}@${pkgVersion}`;
let result = await processes.spawn('npm', ['dist-tag', 'add', pkgStr, tag]);
// An existing tag will return a warning to stderr, but a 0 status code
if (result.stderr) {
throw new BoltError(
`Could not add tag ${tag} to ${pkgStr} as one already exists`
);
}
return result;
});
}
export function removeTag(pkgName: string, tag: string) {
return npmRequestLimit(async () => {
logger.info(messages.npmDistTagRm(pkgName, tag));
try {
return await processes.spawn('npm', ['dist-tag', 'rm', pkgName, tag], {
silent: true
});
} catch (error) {
// The dist tag not existing is unexpected, but shouldn't prevent execution
if (error.code === 1 && error.stderr.includes('is not a dist-tag on')) {
logger.warn(messages.notDistTagFound(tag, pkgName));
return;
} else if (
error.stderr &&
error.stderr.startsWith('npm ERR! code E404')
) {
// the package does not exist yet, warn but dont error
logger.warn(messages.npmPackageCouldNotBeFound(pkgName));
return;
}
throw error;
}
});
}
export async function login(cwd: string) {
await processes.spawn('npm', ['login'], {
cwd,
tty: true
});
}
export async function logout(cwd: string) {
await processes.spawn('npm', ['logout'], {
cwd,
tty: true
});
}
export async function cliCommand(
cwd: string,
command: string = '',
spawnArgs: Array<string> = []
) {
return await processes.spawn('npm', [command, ...spawnArgs], {
cwd,
tty: true
});
}