@limlabs/limo
Version:
Infrastructure as Code generator
64 lines (63 loc) • 3.12 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addNodePackageScript = exports.hasDependency = exports.installDependencies = void 0;
const child_process_1 = require("child_process");
const promises_1 = __importDefault(require("fs/promises"));
const files_1 = require("./files");
/**
* Installs the specified npm packages as dependencies in the 'infrastructure' directory.
*
* @param packages - An array of package names to install.
* @returns A promise that resolves with the standard output of the npm install command, or rejects with an error.
*/
function installDependencies(packages) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
(0, child_process_1.exec)(`npm install --save ${packages.join(" ")}`, { cwd: "infrastructure" }, (err, stdout, stderr) => {
if (err) {
reject(err);
}
else {
resolve(stdout);
}
});
});
});
}
exports.installDependencies = installDependencies;
const hasDependency = (dependency) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
if (!(yield (0, files_1.fileExists)("package.json"))) {
return false;
}
const packageJson = JSON.parse(yield promises_1.default.readFile("package.json", "utf-8"));
return Boolean(((_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a[dependency]) ||
((_b = packageJson.devDependencies) === null || _b === void 0 ? void 0 : _b[dependency]));
});
exports.hasDependency = hasDependency;
function addNodePackageScript(name, cmd) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield (0, files_1.fileExists)("package.json"))) {
return; // we only support creating commands in package.json right now
}
const packageJSON = JSON.parse(yield promises_1.default.readFile("package.json", "utf-8"));
if (!packageJSON.scripts) {
packageJSON.scripts = {};
}
packageJSON.scripts[name] = cmd;
yield promises_1.default.writeFile("package.json", JSON.stringify(packageJSON, null, 2));
});
}
exports.addNodePackageScript = addNodePackageScript;