-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathsetup-npm-registry.js
More file actions
70 lines (61 loc) · 1.83 KB
/
setup-npm-registry.js
File metadata and controls
70 lines (61 loc) · 1.83 KB
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
/**
* Rewrites lockfiles to use a custom npm registry.
*
* Purpose
* - Some lockfiles contain hardcoded references to public registries.
* - In Azure Pipelines, we want installs and npx to consistently resolve from a
* configured private/custom registry feed.
*
* Inputs
* - Environment variable: NPM_CONFIG_REGISTRY (required)
*
* Behavior
* - Recursively scans the repo (excluding node_modules and .git) for:
* - package-lock.json
* - yarn.lock
* - Replaces URLs matching: https://registry.<something>.(com|org)/
* with the provided registry URL.
*/
const fs = require('fs').promises;
const path = require('path');
async function* getLockFiles(dir) {
const files = await fs.readdir(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
if (file === 'node_modules' || file === '.git') {
continue;
}
yield* getLockFiles(fullPath);
continue;
}
if (file === 'yarn.lock' || file === 'package-lock.json') {
yield fullPath;
}
}
}
async function rewrite(file, registry) {
let contents = await fs.readFile(file, 'utf8');
const re = /https:\/\/registry\.[^.]+\.(com|org)\//g;
contents = contents.replace(re, registry);
await fs.writeFile(file, contents);
}
async function main() {
let registry = process.env.NPM_CONFIG_REGISTRY;
if (!registry) {
throw new Error('NPM_CONFIG_REGISTRY is not set');
}
if (!registry.endsWith('/')) {
registry += '/';
}
const root = process.cwd();
for await (const file of getLockFiles(root)) {
await rewrite(file, registry);
console.log('Updated node registry:', file);
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});