setup-npm-registry.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. 'use strict';
  6. const fs = require('fs').promises;
  7. const path = require('path');
  8. async function* getYarnLockFiles(dir) {
  9. const files = await fs.readdir(dir);
  10. for (const file of files) {
  11. const fullPath = path.join(dir, file);
  12. const stat = await fs.stat(fullPath);
  13. if (stat.isDirectory()) {
  14. yield* getYarnLockFiles(fullPath);
  15. } else if (file === 'yarn.lock') {
  16. yield fullPath;
  17. }
  18. }
  19. }
  20. async function setup(url, file) {
  21. let contents = await fs.readFile(file, 'utf8');
  22. contents = contents.replace(/https:\/\/registry\.[^.]+\.com\//g, url);
  23. await fs.writeFile(file, contents);
  24. }
  25. async function main(url, dir) {
  26. const root = dir ?? process.cwd();
  27. for await (const file of getYarnLockFiles(root)) {
  28. console.log(`Enabling custom NPM registry: ${path.relative(root, file)}`);
  29. await setup(url, file);
  30. }
  31. }
  32. main(process.argv[2], process.argv[3]);