UNPKG

5.16 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const Flushable_1 = require("../../internal/common/Flushable");
12const async_1 = require("../../internal/util/async");
13const string_1 = require("../../internal/util/string");
14/**
15 * Promise of an array of files. Usually sourced from Project.streamFiles
16 */
17function toPromise(stream) {
18 return new Promise((resolve, reject) => {
19 const files = [];
20 stream
21 .on("data", f => files.push(f))
22 .on("error", reject)
23 .on("end", _ => resolve(files));
24 });
25}
26exports.toPromise = toPromise;
27/**
28 * Does at least one file matching the given predicate exist in this project?
29 * If no predicate is supplied, does at least one file match the glob pattern?
30 * No guarantees about ordering
31 * @param p
32 * @param globPatterns positive and negative globs to match
33 * @param test return a boolean or promise. Defaults to true
34 * @return {Promise<boolean>}
35 */
36function fileExists(p, globPatterns, test = () => true) {
37 return __awaiter(this, void 0, void 0, function* () {
38 return (yield countFiles(p, globPatterns, test)) > 0;
39 });
40}
41exports.fileExists = fileExists;
42/**
43 * Count files matching the given predicate in this project
44 * If no predicate is supplied, does at least one file match the glob pattern?
45 * No guarantees about ordering
46 * @param p
47 * @param globPatterns positive and negative globs to match
48 * @param test return a boolean or promise. Defaults to true
49 * @return {Promise<boolean>}
50 */
51function countFiles(p, globPatterns, test = () => true) {
52 return __awaiter(this, void 0, void 0, function* () {
53 const results = yield gatherFromFiles(p, globPatterns, (f) => __awaiter(this, void 0, void 0, function* () { return (yield test(f)) === true; }));
54 return results.length;
55 });
56}
57exports.countFiles = countFiles;
58/**
59 * Gather values from files
60 * @param {ProjectAsync} project to act on
61 * @param {string} globPatterns glob pattern for files to match
62 * @param {(f: File) => Promise<T>} gather function returning a promise (of the value you're gathering) from each file.
63 * Undefined returns will be filtered out
64 * @return {Promise<T[]>}
65 */
66function gatherFromFiles(project, globPatterns, gather) {
67 return new Promise((resolve, reject) => {
68 const gathered = [];
69 project.streamFiles(...string_1.toStringArray(globPatterns))
70 .on("data", f => {
71 const g = gather(f);
72 if (g) {
73 gathered.push(g);
74 }
75 })
76 .on("error", reject)
77 .on("end", _ => {
78 resolve(Promise.all(gathered).then(ts => ts.filter(t => !!t)));
79 });
80 });
81}
82exports.gatherFromFiles = gatherFromFiles;
83/**
84 * Perform the same operation on all the files.
85 * @param project project to act on
86 * @param globPatterns glob patterns to match
87 * @param op operation to perform on files. Can return void or a promise.
88 */
89function doWithFiles(project, globPatterns, op) {
90 return new Promise((resolve, reject) => {
91 const filePromises = [];
92 return project.streamFiles(...string_1.toStringArray(globPatterns))
93 .on("data", f => {
94 const r = op(f);
95 if (async_1.isPromise(r)) {
96 filePromises.push(r.then(_ => f.flush()));
97 }
98 else {
99 if (f.dirty) {
100 filePromises.push(f.flush());
101 }
102 }
103 })
104 .on("error", reject)
105 .on("end", _ => {
106 resolve(Promise.all(filePromises));
107 });
108 }).then(files => project);
109}
110exports.doWithFiles = doWithFiles;
111/**
112 * Delete files matching the glob pattern and extra test (if supplied)
113 * @param project project to act on
114 * @param globPatterns glob patterns for files to delete
115 * @param test additional, optional test for files to be deleted
116 */
117function deleteFiles(project, globPatterns, test = () => true) {
118 const fp = project;
119 return new Promise((resolve, reject) => {
120 let deleted = 0;
121 project.streamFiles(...string_1.toStringArray(globPatterns))
122 .on("data", f => {
123 if (test(f)) {
124 ++deleted;
125 Flushable_1.defer(fp, project.deleteFile(f.path));
126 }
127 })
128 .on("error", reject)
129 .on("end", () => {
130 resolve(fp.flush()
131 .then(() => deleted));
132 });
133 });
134}
135exports.deleteFiles = deleteFiles;
136//# sourceMappingURL=projectUtils.js.map
\No newline at end of file