swap-lock-registry
Version:
A CLI tool to swap the registry URL in the lock file without having to remove it
35 lines (34 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseYarnLockFile = exports.traitYarnLockFile = void 0;
const utils_1 = require("./utils");
const lockfile_1 = require("@yarnpkg/lockfile");
const fs_1 = require("fs");
const logger_1 = require("./logger");
async function traitYarnLockFile(lockFile, opts) {
const lockFileObject = await parseYarnLockFile(lockFile);
try {
await Promise.all(Object.keys(lockFileObject).map((pkg) => {
return (0, utils_1.traitPackage)(lockFileObject, pkg, pkg.startsWith('@')
? `@${pkg.substr(1).replace(/@[^@]*/g, '')}`
: pkg.replace(/@[^@]*/g, ''), pkg.replace(/^@?[^@]*@/g, ''), Object.assign(Object.assign({}, opts), { lockFile, tarballWithShaSum: true }));
}));
await fs_1.promises.writeFile(lockFile, (0, lockfile_1.stringify)(lockFileObject));
logger_1.logger.clearLine();
logger_1.logger.success(lockFile, 'done');
}
catch (error) {
throw new Error('Error while fetching packages metadata, please check the registry url');
}
}
exports.traitYarnLockFile = traitYarnLockFile;
async function parseYarnLockFile(lockFile) {
const lockFileString = await (0, utils_1.readFile)(lockFile);
try {
return (0, lockfile_1.parse)(lockFileString).object;
}
catch (error) {
throw new Error(`Could not parse '${lockFile}'!`);
}
}
exports.parseYarnLockFile = parseYarnLockFile;