1 | "use strict";
|
2 | var __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 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const Flushable_1 = require("../../internal/common/Flushable");
|
12 | const async_1 = require("../../internal/util/async");
|
13 | const string_1 = require("../../internal/util/string");
|
14 |
|
15 |
|
16 |
|
17 | function 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 | }
|
26 | exports.toPromise = toPromise;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function fileExists(p, globPatterns, test = () => true) {
|
37 | return __awaiter(this, void 0, void 0, function* () {
|
38 | return (yield countFiles(p, globPatterns, test)) > 0;
|
39 | });
|
40 | }
|
41 | exports.fileExists = fileExists;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function 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 | }
|
57 | exports.countFiles = countFiles;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function 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 | }
|
82 | exports.gatherFromFiles = gatherFromFiles;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function 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 | }
|
110 | exports.doWithFiles = doWithFiles;
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function 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 | }
|
135 | exports.deleteFiles = deleteFiles;
|
136 |
|
\ | No newline at end of file |