neon-cli
Version:
Build and load native Rust/Neon modules.
62 lines (61 loc) • 2.84 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 });
const toml_1 = __importDefault(require("toml"));
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const artifacts_1 = __importDefault(require("./artifacts"));
const rimraf_1 = require("./async/rimraf");
// Represents the native crate inside a Neon project.
class Crate {
constructor(project, options = {}) {
let { subdirectory = "native", nodefile = "index.node" } = options;
this.project = project;
this.subdirectory = subdirectory;
this.nodefile = nodefile;
this.root = path_1.default.resolve(project.root, subdirectory);
this.addon = path_1.default.resolve(this.root, nodefile);
this.name = loadLibName(path_1.default.resolve(this.root, "Cargo.toml"));
this.artifactsfile = path_1.default.resolve(this.root, "artifacts.json");
this.artifacts = artifacts_1.default.load(this.artifactsfile);
}
finish(dylib) {
return __awaiter(this, void 0, void 0, function* () {
yield (0, rimraf_1.rimraf)(this.addon);
yield fs_1.promises.copyFile(dylib, this.addon);
});
}
removeAddon() {
return __awaiter(this, void 0, void 0, function* () {
yield (0, rimraf_1.rimraf)(this.addon);
});
}
resetArtifacts() {
this.artifacts.reset();
}
saveArtifacts() {
this.artifacts.save(this.artifactsfile);
}
}
exports.default = Crate;
function loadLibName(file) {
var _a;
let metadata = toml_1.default.parse((0, fs_1.readFileSync)(file, "utf8"));
if (!metadata)
throw new Error(`Failed to parse TOML file "${file}"`);
if (!metadata || typeof metadata !== "object" || !((_a = metadata === null || metadata === void 0 ? void 0 : metadata.lib) === null || _a === void 0 ? void 0 : _a.name)) {
throw new Error("Cargo.toml does not contain a [lib] section with a 'name' field");
}
return metadata.lib.name;
}
;