UNPKG

trm-core

Version:

TRM (Transport Request Manager) Core

184 lines (183 loc) 7.04 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 { authentication_type: trm_registry_types_1.AuthenticationType.NO_AUTH, messages: [{ text: `File system: "${this.name}", "${this.endpoint}"`, type: trm_registry_types_1.MessageType.INFO }] }; } throw new Error(`Missing file path!`); }); } whoAmI() { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return { user: (0, os_1.userInfo)().username }; } throw new Error(`Missing file path!`); }); } getPackage(fullName, version) { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { return { name: fullName, dist_tags: { latest: version }, versions: [], yanked_versions: [], deprecated: false, manifest: null, checksum: null, download_link: this._filePath }; } throw new Error(`File system can't view packages!`); }); } downloadArtifact(fullName, version) { return __awaiter(this, void 0, void 0, function* () { return new trmPackage_1.TrmArtifact((0, fs_1.readFileSync)(this._filePath)); }); } 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`); } } throw new Error(`Missing file path!`); }); } validatePublish(fullName, version) { return __awaiter(this, void 0, void 0, function* () { }); } publish(fullName, version, artifact, readme) { return __awaiter(this, void 0, void 0, function* () { if (this._filePath) { yield (0, promises_1.writeFile)(this._filePath, artifact.binary, { flag: 'w' }); return this.getPackage(fullName, version); } throw new Error(`Missing file path!`); }); } unpublish(fullName, version) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`File system can't delete packages!`); }); } deprecate(fullName, version, deprecate) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`File system can't deprecate packages!`); }); } addDistTag(fullName, distTag) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`File system can't add dist tags!`); }); } rmDistTag(fullName, distTag) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`File system can't remove dist tags!`); }); } contents(fullName, version) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`File system can't see contents!`); }); } } exports.FileSystem = FileSystem;