UNPKG

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