genezio
Version:
Command line utility to interact with Genezio infrastructure.
100 lines (99 loc) • 5.56 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _NodeJsBinaryDependenciesBundler_instances, _NodeJsBinaryDependenciesBundler_handleBinaryDependencies;
import path from "path";
import fs from "fs";
import { CloudProviderIdentifier } from "../../models/cloudProviderIdentifier.js";
import { fileExists } from "../../utils/file.js";
import { log } from "../../utils/logging.js";
import { debugLogger } from "../../utils/logging.js";
import { getGlobalPackageManager } from "../../packageManagers/packageManager.js";
import { UserError } from "../../errors.js";
import { $ } from "execa";
var Architecture;
(function (Architecture) {
Architecture["ARM64"] = "arm64";
Architecture["X64"] = "x64";
})(Architecture || (Architecture = {}));
export class NodeJsBinaryDependenciesBundler {
constructor() {
_NodeJsBinaryDependenciesBundler_instances.add(this);
}
async bundle(input) {
if (!input.extra.dependenciesInfo) {
debugLogger.debug(`[NodeJSBinaryDependenciesBundler] No dependencies info for file ${input.path}... Something might be wrong.`);
return Promise.resolve(input);
}
debugLogger.debug(`[NodeJSBinaryDependenciesBundler] Redownload binary dependencies if necessary for file ${input.path}...`);
const architecture = input.projectConfiguration.cloudProvider == CloudProviderIdentifier.GENEZIO_AWS
? Architecture.ARM64
: Architecture.X64;
// 4. Redownload binary dependencies if necessary
await __classPrivateFieldGet(this, _NodeJsBinaryDependenciesBundler_instances, "m", _NodeJsBinaryDependenciesBundler_handleBinaryDependencies).call(this, input.extra.dependenciesInfo, input.path, architecture);
debugLogger.debug(`[NodeJSBinaryDependenciesBundler] Redownload binary dependencies done for file ${input.path}.`);
return Promise.resolve(input);
}
}
_NodeJsBinaryDependenciesBundler_instances = new WeakSet(), _NodeJsBinaryDependenciesBundler_handleBinaryDependencies = async function _NodeJsBinaryDependenciesBundler_handleBinaryDependencies(dependenciesInfo, tempFolderPath, architecture) {
// create node_modules folder in tmp folder
const nodeModulesPath = path.join(tempFolderPath, "node_modules");
const binaryDependencies = [];
if (!fs.existsSync(nodeModulesPath)) {
fs.mkdirSync(nodeModulesPath, { recursive: true });
}
// copy all dependencies to node_modules folder
for (const dependency of dependenciesInfo) {
const dependencyPath = path.join(nodeModulesPath, dependency.name);
// read package.json file
if (dependency.name[0] === "@") {
// Get List of all files in a directory
const files = fs.readdirSync(dependencyPath);
// iterate files and check if there is a package.json file
for (const file of files) {
const packageJsonPath = path.join(dependencyPath, file, "package.json");
if (await fileExists(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
if (packageJson.binary) {
binaryDependencies.push({
path: path.join(dependencyPath, file),
name: file,
});
}
}
}
}
else {
const packageJsonPath = path.join(dependencyPath, "package.json");
if (await fileExists(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
// check if package.json has binary property
if (packageJson.binary) {
binaryDependencies.push({
path: dependencyPath,
name: dependency.name,
});
}
}
}
}
if (binaryDependencies.length > 0) {
await getGlobalPackageManager().install(["node-addon-api", "@mapbox/node-pre-gyp"], tempFolderPath);
}
for (const dependency of binaryDependencies) {
try {
const { stdout, stderr } = await $({
cwd: dependency.path,
}) `npx node-pre-gyp --update-binary --fallback-to-build --target_arch=${architecture} --target_platform=linux --target_libc=glibc clean install ${dependency.name}`;
debugLogger.debug("[BinaryDepStdOut]", stdout);
debugLogger.debug("[BinaryDepStdErr]", stderr);
}
catch (error) {
debugLogger.debug("[BinaryDepStdOut]", error);
log.error("An error has occurred while installing binary dependencies.");
throw new UserError("An error has occurred while installing binary dependencies. If you are using binary dependecies in your project make sure your machine can run node-gyp. If you need to know the requirements for running node-gyp you can check out this link: https://github.com/nodejs/node-gyp?tab=readme-ov-file#installation");
}
}
};