terrac
Version:
A minimal private module registry for Terraform and OpenTofu
87 lines (86 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackendLocal = exports.configSchemaLocal = void 0;
const shared_1 = require("./shared");
const errors_1 = require("../errors");
const Joi = require("joi");
const fs_extra_1 = require("fs-extra");
const node_path_1 = require("node:path");
exports.configSchemaLocal = Joi.object({
type: Joi.string().allow('local').required().description('Backend type'),
path: Joi.string().required().description('Local directory path'),
});
class BackendLocal {
constructor(config) {
this.config = config;
}
async upload(name, version, packagePath) {
await (0, fs_extra_1.copy)(packagePath, this.getPackagePath(name, version));
}
async getSourceUrl(name, version) {
let targetVersion = version;
if (!targetVersion) {
const meta = await this.getMeta(name);
targetVersion = meta.version;
}
const path = this.getPackagePath(name, targetVersion);
if (!(await (0, fs_extra_1.pathExists)(path))) {
throw new errors_1.ModuleNotFoundError();
}
return path;
}
async list(name) {
const moduleList = [];
if (name) {
const modulePath = `${this.config.path}/${name}`;
if (!(await (0, fs_extra_1.pathExists)(modulePath))) {
throw new errors_1.ModuleNotFoundError();
}
const versions = await this.listDir(modulePath);
for (const version of versions) {
moduleList.push({
name,
version,
});
}
}
else {
const names = await this.listDir(this.config.path);
for (const name of names) {
moduleList.push({
name,
});
}
}
return moduleList;
}
async exists(name, version) {
const path = version ? this.getPackagePath(name, version) : this.getMetaPath(name);
return (0, fs_extra_1.pathExists)(path);
}
async getMeta(name) {
const path = this.getMetaPath(name);
if (await (0, fs_extra_1.pathExists)(path)) {
return (0, fs_extra_1.readJson)(path);
}
return (0, shared_1.getNewMeta)(name);
}
async saveMeta(meta) {
const path = this.getMetaPath(meta.name);
await (0, fs_extra_1.ensureDir)((0, node_path_1.dirname)(path));
await (0, fs_extra_1.writeJson)(path, meta);
}
getMetaPath(name) {
return `${this.config.path}/${name}/meta.json`;
}
getPackagePath(name, version) {
return `${this.config.path}/${name}/${version}/module.zip`;
}
async listDir(path) {
const files = await (0, fs_extra_1.readdir)(path, { withFileTypes: true });
return files
.filter(file => file.isDirectory())
.map(file => file.name);
}
}
exports.BackendLocal = BackendLocal;