Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/setup-npm-registry.js
3520 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
// @ts-check
6
'use strict';
7
8
const fs = require('fs').promises;
9
const path = require('path');
10
11
/**
12
* @param {string} dir
13
*
14
* @returns {AsyncGenerator<string>}
15
*/
16
async function* getPackageLockFiles(dir) {
17
const files = await fs.readdir(dir);
18
19
for (const file of files) {
20
const fullPath = path.join(dir, file);
21
const stat = await fs.stat(fullPath);
22
23
if (stat.isDirectory()) {
24
yield* getPackageLockFiles(fullPath);
25
} else if (file === 'package-lock.json') {
26
yield fullPath;
27
}
28
}
29
}
30
31
/**
32
* @param {string} url
33
* @param {string} file
34
*/
35
async function setup(url, file) {
36
let contents = await fs.readFile(file, 'utf8');
37
contents = contents.replace(/https:\/\/registry\.[^.]+\.org\//g, url);
38
await fs.writeFile(file, contents);
39
}
40
41
/**
42
* @param {string} url
43
* @param {string} dir
44
*/
45
async function main(url, dir) {
46
const root = dir ?? process.cwd();
47
48
for await (const file of getPackageLockFiles(root)) {
49
console.log(`Enabling custom NPM registry: ${path.relative(root, file)}`);
50
await setup(url, file);
51
}
52
}
53
54
main(process.argv[2], process.argv[3]);
55
56