Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/setup-npm-registry.ts
4770 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { promises as fs } from 'fs';
7
import path from 'path';
8
9
/**
10
* Recursively find all package-lock.json files in a directory
11
*/
12
async function* getPackageLockFiles(dir: string): AsyncGenerator<string> {
13
const files = await fs.readdir(dir);
14
15
for (const file of files) {
16
const fullPath = path.join(dir, file);
17
const stat = await fs.stat(fullPath);
18
19
if (stat.isDirectory()) {
20
yield* getPackageLockFiles(fullPath);
21
} else if (file === 'package-lock.json') {
22
yield fullPath;
23
}
24
}
25
}
26
27
/**
28
* Replace the registry URL in a package-lock.json file
29
*/
30
async function setup(url: string, file: string): Promise<void> {
31
let contents = await fs.readFile(file, 'utf8');
32
contents = contents.replace(/https:\/\/registry\.[^.]+\.org\//g, url);
33
await fs.writeFile(file, contents);
34
}
35
36
/**
37
* Main function to set up custom NPM registry
38
*/
39
async function main(url: string, dir?: string): Promise<void> {
40
const root = dir ?? process.cwd();
41
42
for await (const file of getPackageLockFiles(root)) {
43
console.log(`Enabling custom NPM registry: ${path.relative(root, file)}`);
44
await setup(url, file);
45
}
46
}
47
48
main(process.argv[2], process.argv[3]);
49
50