-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathrun-registry.js
executable file
·279 lines (239 loc) · 7.57 KB
/
run-registry.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env node -r esm
import { spawn, exec } from 'child_process';
import inquirer from 'inquirer';
import chalk from 'chalk';
import detectFreePort from 'detect-port';
import dedent from 'ts-dedent';
import fs from 'fs';
import nodeCleanup from 'node-cleanup';
const logger = console;
const freePort = port => detectFreePort(port);
let verdaccioProcess;
const startVerdaccio = port => {
let resolved = false;
return Promise.race([
new Promise(res => {
verdaccioProcess = spawn('npx', [
'verdaccio@4.0.1',
'-c',
'scripts/verdaccio.yaml',
'-l',
port,
]);
verdaccioProcess.stdout.on('data', data => {
if (!resolved && data && data.toString().match(/http address/)) {
const [url] = data.toString().match(/(http:.*\d\/)/);
res(url);
resolved = true;
}
fs.appendFile('verdaccio.log', data, err => {
if (err) {
throw err;
}
});
});
}),
new Promise((res, rej) => {
setTimeout(() => {
if (!resolved) {
rej(new Error(`TIMEOUT - verdaccio didn't start within 60s`));
resolved = true;
verdaccioProcess.kill();
}
}, 60000);
}),
]);
};
const registryUrl = (command, url) =>
new Promise((res, rej) => {
const args = url ? ['config', 'set', 'registry', url] : ['config', 'get', 'registry'];
exec(`${command} ${args.join(' ')}`, (e, stdout) => {
if (e) {
rej(e);
} else {
res(url || stdout.toString().trim());
}
});
});
const registriesUrl = (yarnUrl, npmUrl) =>
Promise.all([registryUrl('yarn', yarnUrl), registryUrl('npm', npmUrl || yarnUrl)]);
nodeCleanup(() => {
try {
verdaccioProcess.kill();
} catch (e) {
//
}
});
const applyRegistriesUrl = (yarnUrl, npmUrl, originalYarnUrl, originalNpmUrl) => {
logger.log(`↪️ changing system config`);
nodeCleanup(() => {
registriesUrl(originalYarnUrl, originalNpmUrl);
logger.log(dedent`
Your registry config has been restored from:
npm: ${npmUrl} to ${originalNpmUrl}
yarn: ${yarnUrl} to ${originalYarnUrl}
`);
});
return registriesUrl(yarnUrl, npmUrl);
};
const addUser = url =>
new Promise((res, rej) => {
logger.log(`👤 add temp user to verdaccio`);
exec(`npx npm-cli-adduser -r "${url}" -a -u user -p password -e user@example.com`, e => {
if (e) {
rej(e);
} else {
res();
}
});
});
const currentVersion = async () => {
const { version } = (await import('../lerna.json')).default;
return version;
};
const publish = (packages, url) =>
packages.reduce((acc, { name, location }) => {
return acc.then(() => {
return new Promise((res, rej) => {
logger.log(`🛫 publishing ${name} (${location})`);
const command = `cd ${location} && npm publish --registry ${url} --force --access restricted`;
exec(command, e => {
if (e) {
rej(e);
} else {
logger.log(`🛬 successful publish of ${name}!`);
res();
}
});
});
});
}, Promise.resolve());
const listOfPackages = () =>
new Promise((res, rej) => {
const command = `./node_modules/.bin/lerna list --json`;
exec(command, (e, result) => {
if (e) {
rej(e);
} else {
const data = JSON.parse(result.toString().trim());
res(data);
}
});
});
const askForPermission = () =>
inquirer
.prompt([
{
type: 'confirm',
message: `${chalk.red('BE WARNED')} do you want to change your ${chalk.underline(
'system'
)} default registry to the temp verdacio registry?`,
name: 'sure',
},
])
.then(({ sure }) => sure);
const askForReset = () =>
inquirer
.prompt([
{
type: 'confirm',
message: `${chalk.red(
'THIS IS BAD'
)} looks like something bad happened, OR you're already using a local registry, shall we reset to the default registry https://registry.npmjs.org/ ?`,
name: 'sure',
},
])
.then(({ sure }) => {
if (sure) {
logger.log(`↩️ changing system config`);
return registriesUrl('https://registry.npmjs.org/');
}
return process.exit(1);
});
const askForPublish = (packages, url, version) =>
inquirer
.prompt([
{
type: 'confirm',
message: `${chalk.green('READY TO PUBLISH')} shall we kick off a publish?`,
name: 'sure',
},
])
.then(({ sure }) => {
if (sure) {
logger.log(`🚀 publishing version ${version}`);
return publish(packages, url).then(() => askForPublish(packages, url, version));
}
return false;
});
const askForSubset = packages =>
inquirer
.prompt([
{
type: 'checkbox',
message: 'which packages?',
name: 'subset',
pageSize: packages.length,
choices: packages.map(p => ({ name: p.name, checked: true })),
},
])
.then(({ subset }) => packages.filter(p => subset.includes(p.name)));
const run = async () => {
const port = await freePort(4873);
logger.log(`🌏 found a open port: ${port}`);
logger.log(`🔖 reading current registry settings`);
let [originalYarnRegistryUrl, originalNpmRegistryUrl] = await registriesUrl();
if (
originalYarnRegistryUrl.includes('localhost') ||
originalNpmRegistryUrl.includes('localhost')
) {
await askForReset();
originalYarnRegistryUrl = 'https://registry.npmjs.org/';
originalNpmRegistryUrl = 'https://registry.npmjs.org/';
}
logger.log(`📐 reading version of storybook`);
logger.log(`🚛 listing storybook packages`);
logger.log(`🎬 starting verdaccio (this takes ±20 seconds, so be patient)`);
const [shouldOverwrite, verdaccioUrl, packages, version] = await Promise.all([
askForPermission(),
startVerdaccio(port),
listOfPackages(),
currentVersion(),
]);
logger.log(`🌿 verdaccio running on ${verdaccioUrl}`);
if (shouldOverwrite) {
logger.log(dedent`
You have chosen to change your system's default registry url. If this process fails for some reason and doesn't exit correctly, you may be stuck with a npm/yarn config that's broken.
To fix this you can revert back to the registry urls you had before by running:
> npm config set registry ${originalNpmRegistryUrl}
> yarn config set registry ${originalYarnRegistryUrl}
You can now use regular install procedure anywhere on your machine and the storybook packages will be installed from this local registry
The registry url is: ${verdaccioUrl}
`);
} else {
logger.log(dedent`
You have chosen to NOT change your system's default registry url.
The registry is running locally, but you'll need to add a npm/yarn config file in your project in that points to the registry.
Here's a documentation for npm: https://docs.npmjs.com/files/npmrc
Yarn is able to read this file as well
The registry url is: ${verdaccioUrl}
`);
}
if (shouldOverwrite) {
await applyRegistriesUrl(
verdaccioUrl,
verdaccioUrl,
originalYarnRegistryUrl,
originalNpmRegistryUrl
);
}
await addUser(verdaccioUrl);
logger.log(`📦 found ${packages.length} storybook packages at version ${chalk.blue(version)}`);
const subset = await askForSubset(packages);
await askForPublish(subset, verdaccioUrl, version);
logger.log(dedent`
The verdaccio registry will now be terminated (this can take ±15 seconds, please be patient)
`);
verdaccioProcess.kill();
};
run();