@azure-tools/extension
Version:
Yarn-Based extension aquisition (for Azure Open Source Projects)
120 lines • 4.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Yarn = void 0;
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
const async_io_1 = require("@azure-tools/async-io");
const exec_cmd_1 = require("./exec-cmd");
const npm_1 = require("./npm");
const package_manager_1 = require("./package-manager");
let _cli;
const getPathToYarnCli = async () => {
const nodeModulesYarn = (0, path_1.resolve)(`${__dirname}/../node_modules/yarn/lib/cli.js`);
if (await (0, async_io_1.isFile)(nodeModulesYarn)) {
_cli = nodeModulesYarn;
return _cli;
}
const fname = (0, path_1.resolve)(`${__dirname}/../yarn/cli.js`);
if (await (0, async_io_1.isFile)(fname)) {
_cli = fname;
}
else {
// otherwise, we might be in a 'static-linked' library and
// we should try to load it and put a copy in $tmp somewhere.
_cli = (0, path_1.join)((0, os_1.tmpdir)(), "yarn-cli.js");
// did we copy it already?
if (await (0, async_io_1.isFile)(_cli)) {
return _cli;
}
// no, let's copy it now.
await (0, async_io_1.writeFile)(_cli, (0, fs_1.readFileSync)(fname));
}
return _cli;
};
class Yarn {
constructor(pathToYarnCli = undefined) {
this.pathToYarnCli = pathToYarnCli;
}
async install(directory, packages, options, reportProgress) {
await (0, package_manager_1.ensurePackageJsonExists)(directory);
let total = 1;
const logs = [];
const handleYarnEvent = (event) => {
switch (event.type) {
case "progressStart":
if (event.data.total !== 0) {
reportProgress === null || reportProgress === void 0 ? void 0 : reportProgress({ current: 0, total, id: event.data.id });
total = event.data.total;
}
break;
case "progressFinish":
reportProgress === null || reportProgress === void 0 ? void 0 : reportProgress({ current: 100, total, id: event.data.id });
break;
case "progressTick":
reportProgress === null || reportProgress === void 0 ? void 0 : reportProgress({ current: Math.min(event.data.current, total), total, id: event.data.id });
break;
case "error":
case "info":
case "warning":
logs.push({ severity: event.type, message: event.data });
break;
case "step":
logs.push({ severity: "info", message: ` Step: ${event.data.message}` });
break;
}
};
const output = await this.execYarn(directory, ["add", "--global-folder", directory.replace(/\\/g, "/"), ...((options === null || options === void 0 ? void 0 : options.force) ? ["--force"] : []), ...packages], handleYarnEvent);
if (output.error) {
return {
success: false,
error: {
message: `Failed to install package '${packages}' -- ${output.error}`,
logs,
},
};
}
else {
return { success: true };
}
}
async clean(directory) {
await this.execYarn(directory, ["cache", "clean", "--force"]);
}
async execYarn(cwd, args, onYarnEvent) {
var _a;
const procArgs = [
(_a = this.pathToYarnCli) !== null && _a !== void 0 ? _a : (await getPathToYarnCli()),
"--no-node-version-check",
"--no-lockfile",
"--json",
"--registry",
process.env.autorest_registry || npm_1.DEFAULT_NPM_REGISTRY,
...args,
];
const newEnv = {
...process.env,
YARN_IGNORE_PATH: "1", // Prevent yarn from using a different version if configured in ~/.yarnrc
};
const handleYarnLog = (buffer) => {
const str = buffer.toString();
for (const line of str.split(/\r?\n/).filter((x) => x !== "")) {
try {
const data = JSON.parse(line);
onYarnEvent === null || onYarnEvent === void 0 ? void 0 : onYarnEvent(data);
}
catch (e) {
// NOOP
}
}
};
return await (0, exec_cmd_1.execute)(process.execPath, procArgs, {
cwd,
env: newEnv,
onStdOutData: handleYarnLog,
onStdErrData: handleYarnLog,
});
}
}
exports.Yarn = Yarn;
//# sourceMappingURL=yarn.js.map