UNPKG

trm-core

Version:

TRM (Transport Request Manager) Core

166 lines (165 loc) 6.12 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileSystem = exports.LOCAL_RESERVED_KEYWORD = void 0; const trm_registry_types_1 = require("trm-registry-types"); const RegistryType_1 = require("./RegistryType"); const trmPackage_1 = require("../trmPackage"); const os_1 = require("os"); const fs_1 = require("fs"); const path_1 = require("path"); const promises_1 = require("fs/promises"); exports.LOCAL_RESERVED_KEYWORD = 'local'; class FileSystem { constructor(_filePath) { this._filePath = _filePath; this.name = exports.LOCAL_RESERVED_KEYWORD; if (this._filePath) { this.endpoint = (0, path_1.parse)(this._filePath).dir; this.name = (0, path_1.parse)(this._filePath).base; if (this.name === this._filePath) { throw new Error(`"${this._filePath}" is not a valid file path.`); } if (!this.endpoint) { throw new Error(`Couldn't determine file directory.`); } if (!this.name) { throw new Error(`Couldn't determine file name.`); } if ((0, fs_1.existsSync)(this._filePath) && (0, fs_1.lstatSync)(this._filePath).isDirectory()) { throw new Error(`"${this._filePath}" is a directory. File name is missing.`); } if ((0, fs_1.existsSync)(this.endpoint)) { if (!(0, fs_1.lstatSync)(this.endpoint).isDirectory()) { throw new Error(`"${this.endpoint}" is not a valid directory.`); } else { try { (0, fs_1.accessSync)(this.endpoint, fs_1.constants.W_OK); } catch (e) { throw new Error(`Cannot write to directory "${this.endpoint}".`); } } } else { (0, fs_1.mkdirSync)(this.endpoint, { recursive: true }); } } } compare(registry) { if (registry instanceof FileSystem) { return this._filePath === registry._filePath; } else { return false; } } getRegistryType() { return RegistryType_1.RegistryType.LOCAL; } authenticate(defaultData) { return __awaiter(this, void 0, void 0, function* () { return this; }); } getAuthData() { return null; } ping() { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return { authenticationType: trm_registry_types_1.AuthenticationType.NO_AUTH, wallMessage: { text: `File system: "${this.name}", "${this.endpoint}"`, type: trm_registry_types_1.MessageType.INFO } }; } return null; }); } whoAmI() { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return { username: (0, os_1.userInfo)().username }; } return null; }); } packageExists(name, version) { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return false; } return null; }); } view(name_1) { return __awaiter(this, arguments, void 0, function* (name, version = 'latest') { if (this._filePath) { const userAuthorizations = { canCreateReleases: true }; var error = new Error(`File system can't view packages!`); error.response = { userAuthorizations }; throw error; } return null; }); } getArtifact(name_1) { return __awaiter(this, arguments, void 0, function* (name, version = 'latest') { if (this._filePath) { try { if (!this._artifact) { this._artifact = new trmPackage_1.TrmArtifact((0, fs_1.readFileSync)(this._filePath)); this._artifact.setFilePath(this._filePath); } return this._artifact; } catch (e) { throw new Error(`File system couldn't read package`); } } return null; }); } publishArtifact(packageName, version, artifact, readme) { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return (0, promises_1.writeFile)(this._filePath, artifact.binary, { flag: 'w' }); } return null; }); } unpublish(packageName, version) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`File system can't delete packages!`); }); } getReleases(packageName, versionRange) { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return []; } return null; }); } } exports.FileSystem = FileSystem;