UNPKG

veendor

Version:

a tool for stroing your npm dependencies in arbitraty storage

84 lines (83 loc) 3.97 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const types_1 = require("../../types"); const errors = __importStar(require("../errors")); const logger_1 = require("../util/logger"); const helpers = __importStar(require("./helpers")); const progress_1 = require("../util/progress"); function pushBackends(backendConfigs, hash, rePull, clearCache) { return __awaiter(this, void 0, void 0, function* () { const logger = logger_1.getLogger(); logger.trace(`Pushing '${hash}' to backends`); const pushingBackends = backendConfigs.filter(backend => backend.push); if (pushingBackends.length === 0 && backendConfigs.length > 0) { logger.info(`No backends with push: true found. Exiting`); return; } const dirPromises = pushingBackends.map(backend => { return helpers.createCleanCacheDir(backend); }); const sharedCachePath = path_1.default.join(process.cwd(), 'node_modules', '.cache'); if (clearCache) { yield fs_extra_1.default.pathExists(sharedCachePath).then(() => { logger.info(`Shared cache directory found at '${sharedCachePath}'. Removing`); return fs_extra_1.default.remove(sharedCachePath); }, (err) => { if (err.code !== 'ENOENT') { throw err; } }); } const cacheDirs = yield Promise.all(dirPromises); const pushingPromises = []; for (const [index, backend] of pushingBackends.entries()) { logger.info(`Pushing '${hash}' to '${backend.alias}' backend`); let promise = backend.backend .push(hash, backend.options, cacheDirs[index], progress_1.provideBackendCallTools(backend, types_1.BackendCalls.push)) .then(() => { logger.info(`Pushing ${hash}' to '${backend.alias}' backend completed succsessfully`); }); if (backend.pushMayFail) { promise = promise.catch((error) => { logger.warn(error); }); } pushingPromises.push(promise); } try { yield Promise.all(pushingPromises); } catch (error) { if (error instanceof errors.BundleAlreadyExistsError) { if (!rePull) { const message = `Bundle '${hash}' already exists in remote repo! Re-pulling it`; logger.error(message); throw new errors.RePullNeeded(message); } } throw error; } logger.debug('Pushing to all backends completed succsessfully'); }); } exports.pushBackends = pushBackends; ;