UNPKG

1.26 kBJavaScriptView Raw
1import makeDebug from 'debug';
2import { HTTP } from 'http-call';
3import { mkdir, writeFile } from 'node:fs/promises';
4import { dirname } from 'node:path';
5async function run([name, file, version, registry, authorization]) {
6 const debug = makeDebug('get-version');
7 debug('name:', name);
8 debug('file:', file);
9 debug('version:', version);
10 debug('registry:', registry);
11 debug('authorization:', authorization);
12 const url = [
13 registry.replace(/\/+$/, ''), // remove trailing slash
14 name.replace('/', '%2f'), // scoped packages need escaped separator
15 ].join('/');
16 const headers = authorization ? { authorization } : {};
17 await mkdir(dirname(file), { recursive: true });
18 await writeFile(file, JSON.stringify({ current: version, headers })); // touch file with current version to prevent multiple updates
19 const { body } = await HTTP.get(url, { headers, timeout: 5000 });
20 await writeFile(file, JSON.stringify({ ...body['dist-tags'], authorization, current: version }));
21 // eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit
22 process.exit(0);
23}
24await run(process.argv.slice(2)).catch(async (error) => {
25 const { handle } = await import('@oclif/core');
26 await handle(error);
27});