UNPKG

@atomist/automation-client

Version:

Atomist API for software low-level client

167 lines • 7.14 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 __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); const Flushable_1 = require("../../internal/common/Flushable"); const async_1 = require("../../internal/util/async"); const string_1 = require("../../internal/util/string"); /** * Promise of an array of files. Usually sourced from Project.streamFiles */ function toPromise(stream) { return new Promise((resolve, reject) => { const files = []; stream .on("data", f => files.push(f)) .on("error", reject) .on("end", _ => resolve(files)); }); } exports.toPromise = toPromise; /** * Does at least one file matching the given predicate exist in this project? * If no predicate is supplied, does at least one file match the glob pattern? * No guarantees about ordering * @param p * @param globPatterns positive and negative globs to match * @param test return a boolean or promise. Defaults to true * @return {Promise<boolean>} */ function fileExists(p, globPatterns, test = () => true) { return __awaiter(this, void 0, void 0, function* () { return (yield countFiles(p, globPatterns, test)) > 0; }); } exports.fileExists = fileExists; /** * Count files matching the given predicate in this project * If no predicate is supplied, does at least one file match the glob pattern? * No guarantees about ordering * @param p * @param globPatterns positive and negative globs to match * @param test return a boolean or promise. Defaults to true * @return {Promise<boolean>} */ function countFiles(p, globPatterns, test = () => true) { return __awaiter(this, void 0, void 0, function* () { const results = yield gatherFromFiles(p, globPatterns, (f) => __awaiter(this, void 0, void 0, function* () { return (yield test(f)) === true; })); return results.length; }); } exports.countFiles = countFiles; /** * Gather values from files * @param {ProjectAsync} project to act on * @param {string} globPatterns glob pattern for files to match * @param {(f: File) => Promise<T>} gather function returning a promise (of the value you're gathering) from each file. * Undefined returns will be filtered out * @return {Promise<T[]>} */ function gatherFromFiles(project, globPatterns, gather) { return __awaiter(this, void 0, void 0, function* () { const allFiles = yield project.getFiles(globPatterns); const matches = allFiles.map(gather); return (yield Promise.all(matches)).filter(t => !!t); }); } exports.gatherFromFiles = gatherFromFiles; /** * Async generator to iterate over files. * @param {Project} project to act on * @param {string} globPatterns glob pattern for files to match * @param filter function to determine whether this file should be included. * Include all files if this function isn't supplied. * @return {Promise<T[]>} */ function fileIterator(project, globPatterns, filter = () => __awaiter(this, void 0, void 0, function* () { return true; })) { return __asyncGenerator(this, arguments, function* fileIterator_1() { const files = yield __await(project.getFiles(globPatterns)); for (const file of files) { if (yield __await(filter(file))) { yield yield __await(file); } } }); } exports.fileIterator = fileIterator; /** * Perform the same operation on all the files. * @param project project to act on * @param globPatterns glob patterns to match * @param op operation to perform on files. Can return void or a promise. */ function doWithFiles(project, globPatterns, op) { return __awaiter(this, void 0, void 0, function* () { const files = yield project.getFiles(globPatterns); const filePromises = []; files.map(f => { const r = op(f); if (async_1.isPromise(r)) { filePromises.push(r.then(_ => f.flush())); } else { if (f.dirty) { filePromises.push(f.flush()); } } }); yield Promise.all(filePromises); return project; }); } exports.doWithFiles = doWithFiles; /** * Delete files matching the glob pattern and extra test (if supplied) * @param project project to act on * @param globPatterns glob patterns for files to delete * @param test additional, optional test for files to be deleted */ function deleteFiles(project, globPatterns, test = () => true) { const fp = project; return new Promise((resolve, reject) => { let deleted = 0; project.streamFiles(...string_1.toStringArray(globPatterns)) .on("data", f => { if (test(f)) { ++deleted; Flushable_1.defer(fp, project.deleteFile(f.path)); } }) .on("error", reject) .on("end", () => { resolve(fp.flush() .then(() => deleted)); }); }); } exports.deleteFiles = deleteFiles; /** * Copy files from one project to another * @param from project to copy files from * @param to project to copy files to */ function copyFiles(from, to) { return toPromise(from.streamFiles()) .then(files => Promise.all(files.map(f => f.getContent().then(content => to.addFile(f.path, content))))).then(() => to); } exports.copyFiles = copyFiles; //# sourceMappingURL=projectUtils.js.map