1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.NodeJsSyncHost = exports.NodeJsAsyncHost = void 0;
|
---|
11 | const fs_1 = require("fs");
|
---|
12 | const path_1 = require("path");
|
---|
13 | const rxjs_1 = require("rxjs");
|
---|
14 | const operators_1 = require("rxjs/operators");
|
---|
15 | const src_1 = require("../src");
|
---|
16 | async function exists(path) {
|
---|
17 | try {
|
---|
18 | await fs_1.promises.access(path, fs_1.constants.F_OK);
|
---|
19 | return true;
|
---|
20 | }
|
---|
21 | catch {
|
---|
22 | return false;
|
---|
23 | }
|
---|
24 | }
|
---|
25 | // This will only be initialized if the watch() method is called.
|
---|
26 | // Otherwise chokidar appears only in type positions, and shouldn't be referenced
|
---|
27 | // in the JavaScript output.
|
---|
28 | let FSWatcher;
|
---|
29 | function loadFSWatcher() {
|
---|
30 | if (!FSWatcher) {
|
---|
31 | try {
|
---|
32 | // eslint-disable-next-line import/no-extraneous-dependencies
|
---|
33 | FSWatcher = require('chokidar').FSWatcher;
|
---|
34 | }
|
---|
35 | catch (e) {
|
---|
36 | if (e.code !== 'MODULE_NOT_FOUND') {
|
---|
37 | throw new Error('As of angular-devkit version 8.0, the "chokidar" package ' +
|
---|
38 | 'must be installed in order to use watch() features.');
|
---|
39 | }
|
---|
40 | throw e;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 | /**
|
---|
45 | * An implementation of the Virtual FS using Node as the background. There are two versions; one
|
---|
46 | * synchronous and one asynchronous.
|
---|
47 | */
|
---|
48 | class NodeJsAsyncHost {
|
---|
49 | get capabilities() {
|
---|
50 | return { synchronous: false };
|
---|
51 | }
|
---|
52 | write(path, content) {
|
---|
53 | return rxjs_1.from(fs_1.promises.mkdir(src_1.getSystemPath(src_1.dirname(path)), { recursive: true })).pipe(operators_1.mergeMap(() => fs_1.promises.writeFile(src_1.getSystemPath(path), new Uint8Array(content))));
|
---|
54 | }
|
---|
55 | read(path) {
|
---|
56 | return rxjs_1.from(fs_1.promises.readFile(src_1.getSystemPath(path))).pipe(operators_1.map((buffer) => new Uint8Array(buffer).buffer));
|
---|
57 | }
|
---|
58 | delete(path) {
|
---|
59 | return this.isDirectory(path).pipe(operators_1.mergeMap(async (isDirectory) => {
|
---|
60 | if (isDirectory) {
|
---|
61 | const recursiveDelete = async (dirPath) => {
|
---|
62 | for (const fragment of await fs_1.promises.readdir(dirPath)) {
|
---|
63 | const sysPath = path_1.join(dirPath, fragment);
|
---|
64 | const stats = await fs_1.promises.stat(sysPath);
|
---|
65 | if (stats.isDirectory()) {
|
---|
66 | await recursiveDelete(sysPath);
|
---|
67 | await fs_1.promises.rmdir(sysPath);
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | await fs_1.promises.unlink(sysPath);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | };
|
---|
74 | await recursiveDelete(src_1.getSystemPath(path));
|
---|
75 | }
|
---|
76 | else {
|
---|
77 | await fs_1.promises.unlink(src_1.getSystemPath(path));
|
---|
78 | }
|
---|
79 | }));
|
---|
80 | }
|
---|
81 | rename(from, to) {
|
---|
82 | return rxjs_1.from(fs_1.promises.rename(src_1.getSystemPath(from), src_1.getSystemPath(to)));
|
---|
83 | }
|
---|
84 | list(path) {
|
---|
85 | return rxjs_1.from(fs_1.promises.readdir(src_1.getSystemPath(path))).pipe(operators_1.map((names) => names.map((name) => src_1.fragment(name))));
|
---|
86 | }
|
---|
87 | exists(path) {
|
---|
88 | return rxjs_1.from(exists(src_1.getSystemPath(path)));
|
---|
89 | }
|
---|
90 | isDirectory(path) {
|
---|
91 | return this.stat(path).pipe(operators_1.map((stat) => stat.isDirectory()));
|
---|
92 | }
|
---|
93 | isFile(path) {
|
---|
94 | return this.stat(path).pipe(operators_1.map((stat) => stat.isFile()));
|
---|
95 | }
|
---|
96 | // Some hosts may not support stat.
|
---|
97 | stat(path) {
|
---|
98 | return rxjs_1.from(fs_1.promises.stat(src_1.getSystemPath(path)));
|
---|
99 | }
|
---|
100 | // Some hosts may not support watching.
|
---|
101 | watch(path, _options) {
|
---|
102 | return new rxjs_1.Observable((obs) => {
|
---|
103 | loadFSWatcher();
|
---|
104 | const watcher = new FSWatcher({ persistent: true }).add(src_1.getSystemPath(path));
|
---|
105 | watcher
|
---|
106 | .on('change', (path) => {
|
---|
107 | obs.next({
|
---|
108 | path: src_1.normalize(path),
|
---|
109 | time: new Date(),
|
---|
110 | type: 0 /* Changed */,
|
---|
111 | });
|
---|
112 | })
|
---|
113 | .on('add', (path) => {
|
---|
114 | obs.next({
|
---|
115 | path: src_1.normalize(path),
|
---|
116 | time: new Date(),
|
---|
117 | type: 1 /* Created */,
|
---|
118 | });
|
---|
119 | })
|
---|
120 | .on('unlink', (path) => {
|
---|
121 | obs.next({
|
---|
122 | path: src_1.normalize(path),
|
---|
123 | time: new Date(),
|
---|
124 | type: 2 /* Deleted */,
|
---|
125 | });
|
---|
126 | });
|
---|
127 | return () => watcher.close();
|
---|
128 | }).pipe(operators_1.publish(), operators_1.refCount());
|
---|
129 | }
|
---|
130 | }
|
---|
131 | exports.NodeJsAsyncHost = NodeJsAsyncHost;
|
---|
132 | /**
|
---|
133 | * An implementation of the Virtual FS using Node as the backend, synchronously.
|
---|
134 | */
|
---|
135 | class NodeJsSyncHost {
|
---|
136 | get capabilities() {
|
---|
137 | return { synchronous: true };
|
---|
138 | }
|
---|
139 | write(path, content) {
|
---|
140 | return new rxjs_1.Observable((obs) => {
|
---|
141 | fs_1.mkdirSync(src_1.getSystemPath(src_1.dirname(path)), { recursive: true });
|
---|
142 | fs_1.writeFileSync(src_1.getSystemPath(path), new Uint8Array(content));
|
---|
143 | obs.next();
|
---|
144 | obs.complete();
|
---|
145 | });
|
---|
146 | }
|
---|
147 | read(path) {
|
---|
148 | return new rxjs_1.Observable((obs) => {
|
---|
149 | const buffer = fs_1.readFileSync(src_1.getSystemPath(path));
|
---|
150 | obs.next(new Uint8Array(buffer).buffer);
|
---|
151 | obs.complete();
|
---|
152 | });
|
---|
153 | }
|
---|
154 | delete(path) {
|
---|
155 | return this.isDirectory(path).pipe(operators_1.concatMap((isDir) => {
|
---|
156 | if (isDir) {
|
---|
157 | const dirPaths = fs_1.readdirSync(src_1.getSystemPath(path));
|
---|
158 | const rmDirComplete = new rxjs_1.Observable((obs) => {
|
---|
159 | fs_1.rmdirSync(src_1.getSystemPath(path));
|
---|
160 | obs.complete();
|
---|
161 | });
|
---|
162 | return rxjs_1.concat(...dirPaths.map((name) => this.delete(src_1.join(path, name))), rmDirComplete);
|
---|
163 | }
|
---|
164 | else {
|
---|
165 | try {
|
---|
166 | fs_1.unlinkSync(src_1.getSystemPath(path));
|
---|
167 | }
|
---|
168 | catch (err) {
|
---|
169 | return rxjs_1.throwError(err);
|
---|
170 | }
|
---|
171 | return rxjs_1.of(undefined);
|
---|
172 | }
|
---|
173 | }));
|
---|
174 | }
|
---|
175 | rename(from, to) {
|
---|
176 | return new rxjs_1.Observable((obs) => {
|
---|
177 | const toSystemPath = src_1.getSystemPath(to);
|
---|
178 | fs_1.mkdirSync(path_1.dirname(toSystemPath), { recursive: true });
|
---|
179 | fs_1.renameSync(src_1.getSystemPath(from), toSystemPath);
|
---|
180 | obs.next();
|
---|
181 | obs.complete();
|
---|
182 | });
|
---|
183 | }
|
---|
184 | list(path) {
|
---|
185 | return new rxjs_1.Observable((obs) => {
|
---|
186 | const names = fs_1.readdirSync(src_1.getSystemPath(path));
|
---|
187 | obs.next(names.map((name) => src_1.fragment(name)));
|
---|
188 | obs.complete();
|
---|
189 | });
|
---|
190 | }
|
---|
191 | exists(path) {
|
---|
192 | return new rxjs_1.Observable((obs) => {
|
---|
193 | obs.next(fs_1.existsSync(src_1.getSystemPath(path)));
|
---|
194 | obs.complete();
|
---|
195 | });
|
---|
196 | }
|
---|
197 | isDirectory(path) {
|
---|
198 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
199 | return this.stat(path).pipe(operators_1.map((stat) => stat.isDirectory()));
|
---|
200 | }
|
---|
201 | isFile(path) {
|
---|
202 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
203 | return this.stat(path).pipe(operators_1.map((stat) => stat.isFile()));
|
---|
204 | }
|
---|
205 | // Some hosts may not support stat.
|
---|
206 | stat(path) {
|
---|
207 | return new rxjs_1.Observable((obs) => {
|
---|
208 | obs.next(fs_1.statSync(src_1.getSystemPath(path)));
|
---|
209 | obs.complete();
|
---|
210 | });
|
---|
211 | }
|
---|
212 | // Some hosts may not support watching.
|
---|
213 | watch(path, _options) {
|
---|
214 | return new rxjs_1.Observable((obs) => {
|
---|
215 | const opts = { persistent: false };
|
---|
216 | loadFSWatcher();
|
---|
217 | const watcher = new FSWatcher(opts).add(src_1.getSystemPath(path));
|
---|
218 | watcher
|
---|
219 | .on('change', (path) => {
|
---|
220 | obs.next({
|
---|
221 | path: src_1.normalize(path),
|
---|
222 | time: new Date(),
|
---|
223 | type: 0 /* Changed */,
|
---|
224 | });
|
---|
225 | })
|
---|
226 | .on('add', (path) => {
|
---|
227 | obs.next({
|
---|
228 | path: src_1.normalize(path),
|
---|
229 | time: new Date(),
|
---|
230 | type: 1 /* Created */,
|
---|
231 | });
|
---|
232 | })
|
---|
233 | .on('unlink', (path) => {
|
---|
234 | obs.next({
|
---|
235 | path: src_1.normalize(path),
|
---|
236 | time: new Date(),
|
---|
237 | type: 2 /* Deleted */,
|
---|
238 | });
|
---|
239 | });
|
---|
240 | return () => watcher.close();
|
---|
241 | }).pipe(operators_1.publish(), operators_1.refCount());
|
---|
242 | }
|
---|
243 | }
|
---|
244 | exports.NodeJsSyncHost = NodeJsSyncHost;
|
---|