UNPKG

@atomist/automation-client

Version:

Atomist API for software low-level client

110 lines 4.31 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 }); const micromatch = require("micromatch"); const AbstractScriptedFlushable_1 = require("../../internal/common/AbstractScriptedFlushable"); const logger_1 = require("../../util/logger"); const fileGlobs_1 = require("../fileGlobs"); const projectUtils_1 = require("../util/projectUtils"); /** * Support for implementations of Project interface */ class AbstractProject extends AbstractScriptedFlushable_1.AbstractScriptedFlushable { constructor(id, shouldCache = false) { super(); this.id = id; this.shouldCache = shouldCache; this.cache = {}; } get name() { return !!this.id ? this.id.repo : undefined; } hasFile(path) { return __awaiter(this, void 0, void 0, function* () { return !!(yield this.getFile(path)); }); } streamFiles(...globPatterns) { const globsToUse = globPatterns.length > 0 ? globPatterns.concat(fileGlobs_1.DefaultExcludes) : fileGlobs_1.DefaultFiles; return this.streamFilesRaw(globsToUse, { dot: true }); } /** * Get files matching these patterns * @param {string[]} globPatterns * @return {Promise<File[]>} */ getFiles(globPatterns = []) { return __awaiter(this, void 0, void 0, function* () { const globPatternsToUse = globPatterns ? (typeof globPatterns === "string" ? [globPatterns] : globPatterns) : []; // Deliberately checking truthiness of promise if (!this.cachedFiles || !this.shouldCache) { const globsToUse = [...fileGlobs_1.DefaultFiles]; this.cachedFiles = this.getFilesInternal(globsToUse); } return globMatchesWithin(yield this.cachedFiles, globPatternsToUse, { dot: true }); }); } /** * Subclasses can override this to optimize if they wish * @return {Promise<File[]>} */ getFilesInternal(globPatterns) { return projectUtils_1.toPromise(this.streamFiles()); } totalFileCount() { return __awaiter(this, void 0, void 0, function* () { const files = yield this.getFiles(); return files.length; }); } trackFile(f) { logger_1.logger.debug(`Project is tracking '${f.path}'`); return this.recordAction(p => { return f.flush().then(_ => p); }); } moveFile(oldPath, newPath) { return this.findFile(oldPath) .then(f => f.setPath(newPath).then(() => this)) // Not an error if no such file .catch(err => this); } recordAddFile(path, content) { return this.recordAction(p => p.addFile(path, content)); } recordDeleteFile(path) { return this.recordAction(p => p.deleteFile(path)); } // TODO set permissions add(f) { return f.getContent() .then(content => this.addFile(f.path, content)); } invalidateCache() { this.cachedFiles = undefined; } } exports.AbstractProject = AbstractProject; /** * Return the files that match these glob patterns, including negative globs */ function globMatchesWithin(files, globPatterns, opts) { if (!globPatterns || globPatterns.length === 0) { return files || []; } const paths = (files || []).map(f => f.path); const matchingPaths = micromatch(paths, globPatterns, opts); return files.filter(f => matchingPaths.includes(f.path)); } exports.globMatchesWithin = globMatchesWithin; //# sourceMappingURL=AbstractProject.js.map