UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

73 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelExists = exports.ModelPackageDirectory = exports.modelPackageCache = void 0; const path_1 = require("path"); const fs_1 = require("fs"); const util_1 = require("util"); const mkdirp = require("mkdirp"); const pump = require("pump"); const rimraf_1 = require("rimraf"); const model_id_1 = require("./model-id"); const paths_1 = require("../../paths"); const util_2 = require("../../util"); const infrastructure_1 = require("../../infrastructure"); exports.modelPackageCache = { has(id, version) { const modelPackagePath = ModelPackagePath(id, version); return (0, fs_1.existsSync)(modelPackagePath); }, read(id, version) { const modelPackagePath = ModelPackagePath(id, version); return (0, fs_1.createReadStream)(modelPackagePath); }, async clear() { await (0, rimraf_1.default)(ModelPackageDirectory()); }, async write(id, version, readable) { const modelPackagePath = ModelPackagePath(id, version); mkdirp.sync((0, path_1.dirname)(modelPackagePath)); const tmpFilePath = `${modelPackagePath}.${(0, util_2.RandomString)()}.tmp`; try { await new Promise((resolve, reject) => { const writeable = (0, fs_1.createWriteStream)(tmpFilePath); pump(readable, writeable, (err) => { if (err) { reject(err); } else { resolve(); } }); }); await (0, util_1.promisify)(fs_1.rename)(tmpFilePath, modelPackagePath); } catch (exception) { try { // Attempt to delete the new files since they could be corrupted await (0, rimraf_1.default)(tmpFilePath); await (0, rimraf_1.default)(modelPackagePath); } finally { // Do nothing } throw exception; } }, async remove(id, version) { const modelPackagePath = ModelPackagePath(id, version); await (0, rimraf_1.default)(modelPackagePath); } }; function ModelPackageDirectory() { return (0, path_1.join)(paths_1.MODEL_PACKAGE_CACHE_DIR, (0, infrastructure_1.getSystemId)()); } exports.ModelPackageDirectory = ModelPackageDirectory; function ModelExists(modelId) { return (0, fs_1.existsSync)((0, path_1.join)(ModelPackageDirectory(), modelId)); } exports.ModelExists = ModelExists; function ModelPackagePath(id, version) { const { publisher, name } = model_id_1.ModelId.parse(id); return (0, path_1.join)(ModelPackageDirectory(), publisher, name, `${version}.tar.gz`); } //# sourceMappingURL=model-package-cache.js.map