forked from changesets/changesets
/
publishPackages.ts
210 lines (187 loc) 路 5.21 KB
/
publishPackages.ts
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
import { join } from "path";
import semver from "semver";
import chalk from "chalk";
import { AccessType } from "@changesets/types";
import { Package } from "@manypkg/get-packages";
import { info, warn } from "@changesets/logger";
import { PreState } from "@changesets/types";
import * as npmUtils from "./npm-utils";
import { TwoFactorState } from "../../utils/types";
import isCI from "is-ci";
type PublishedState = "never" | "published" | "only-pre";
type PkgInfo = {
name: string;
localVersion: string;
publishedState: PublishedState;
publishedVersions: string[];
};
export type PublishedResult = {
name: string;
newVersion: string;
published: boolean;
};
function getReleaseTag(pkgInfo: PkgInfo, preState?: PreState, tag?: string) {
if (tag) return tag;
if (preState !== undefined && pkgInfo.publishedState !== "only-pre") {
return preState.tag;
}
return "latest";
}
const isCustomRegistry = (registry?: string): boolean =>
!!registry &&
registry !== "https://registry.npmjs.org" &&
registry !== "https://registry.yarnpkg.com";
const getTwoFactorState = ({
otp,
publicPackages,
}: {
otp?: string;
publicPackages: Package[];
}): TwoFactorState => {
if (otp) {
return {
token: otp,
isRequired: Promise.resolve(true),
};
}
if (
isCI ||
publicPackages.some((pkg) =>
isCustomRegistry(pkg.packageJson.publishConfig?.registry)
) ||
isCustomRegistry(process.env.npm_config_registry)
) {
return {
token: null,
isRequired: Promise.resolve(false),
};
}
return {
token: null,
// note: we're not awaiting this here, we want this request to happen in parallel with getUnpublishedPackages
isRequired: npmUtils.getTokenIsRequired(),
};
};
export default async function publishPackages({
packages,
access,
otp,
preState,
tag,
}: {
packages: Package[];
access: AccessType;
otp?: string;
preState: PreState | undefined;
tag?: string;
}) {
const packagesByName = new Map(packages.map((x) => [x.packageJson.name, x]));
const publicPackages = packages.filter((pkg) => !pkg.packageJson.private);
const unpublishedPackagesInfo = await getUnpublishedPackages(
publicPackages,
preState
);
if (unpublishedPackagesInfo.length === 0) {
return [];
}
const twoFactorState: TwoFactorState = getTwoFactorState({
otp,
publicPackages,
});
return Promise.all(
unpublishedPackagesInfo.map((pkgInfo) => {
let pkg = packagesByName.get(pkgInfo.name)!;
return publishAPackage(
pkg,
access,
twoFactorState,
getReleaseTag(pkgInfo, preState, tag)
);
})
);
}
async function publishAPackage(
pkg: Package,
access: AccessType,
twoFactorState: TwoFactorState,
tag: string
): Promise<PublishedResult> {
const { name, version, publishConfig } = pkg.packageJson;
const localAccess = publishConfig?.access;
info(
`Publishing ${chalk.cyan(`"${name}"`)} at ${chalk.green(`"${version}"`)}`
);
const publishDir = publishConfig?.directory
? join(pkg.dir, publishConfig.directory)
: pkg.dir;
const publishConfirmation = await npmUtils.publish(
name,
{
cwd: publishDir,
access: localAccess || access,
tag,
},
twoFactorState
);
return {
name,
newVersion: version,
published: publishConfirmation.published,
};
}
async function getUnpublishedPackages(
packages: Array<Package>,
preState: PreState | undefined
) {
const results: Array<PkgInfo> = await Promise.all(
packages.map(async ({ packageJson }) => {
const response = await npmUtils.infoAllow404(packageJson);
let publishedState: PublishedState = "never";
if (response.published) {
publishedState = "published";
if (preState !== undefined) {
if (
response.pkgInfo.versions &&
response.pkgInfo.versions.every(
(version: string) =>
semver.parse(version)!.prerelease[0] === preState.tag
)
) {
publishedState = "only-pre";
}
}
}
return {
name: packageJson.name,
localVersion: packageJson.version,
publishedState,
publishedVersions: response.pkgInfo.versions || [],
};
})
);
const packagesToPublish: Array<PkgInfo> = [];
for (const pkgInfo of results) {
const { name, publishedState, localVersion, publishedVersions } = pkgInfo;
if (!publishedVersions.includes(localVersion)) {
packagesToPublish.push(pkgInfo);
info(
`${name} is being published because our local version (${localVersion}) has not been published on npm`
);
if (preState !== undefined && publishedState === "only-pre") {
info(
`${name} is being published to ${chalk.cyan(
"latest"
)} rather than ${chalk.cyan(
preState.tag
)} because there has not been a regular release of it yet`
);
}
} else {
// If the local version is behind npm, something is wrong, we warn here, and by not getting published later, it will fail
warn(
`${name} is not being published because version ${localVersion} is already published on npm`
);
}
}
return packagesToPublish;
}