Skip to content

Commit caad431

Browse files
alan-agius4filipesilva
authored andcommitted
refactor(@angular-devkit/core): clean up NodeJsAsyncHost and NodeJsSyncHost
- Remove redundant try/catch block - Use `mkdir` with `recursive: true` instead of custom implementation
1 parent be4dbd0 commit caad431

File tree

1 file changed

+33
-95
lines changed
  • packages/angular_devkit/core/node

1 file changed

+33
-95
lines changed

packages/angular_devkit/core/node/host.ts

+33-95
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929

3030

3131
interface ChokidarWatcher {
32-
new (options: {}): ChokidarWatcher;
32+
new(options: {}): ChokidarWatcher;
3333

3434
add(path: string): ChokidarWatcher;
3535
on(type: 'change', cb: (path: string) => void): ChokidarWatcher;
@@ -51,7 +51,7 @@ function loadFSWatcher() {
5151
} catch (e) {
5252
if (e.code !== 'MODULE_NOT_FOUND') {
5353
throw new Error('As of angular-devkit version 8.0, the "chokidar" package ' +
54-
'must be installed in order to use watch() features.');
54+
'must be installed in order to use watch() features.');
5555
}
5656
throw e;
5757
}
@@ -92,26 +92,17 @@ export class NodeJsAsyncHost implements virtualFs.Host<fs.Stats> {
9292
}
9393

9494
write(path: Path, content: virtualFs.FileBuffer): Observable<void> {
95-
return new Observable<void>(obs => {
96-
// Create folders if necessary.
97-
const _createDir = (path: Path) => {
98-
if (fs.existsSync(getSystemPath(path))) {
99-
return;
100-
}
101-
if (dirname(path) === path) {
102-
throw new Error();
103-
}
104-
_createDir(dirname(path));
105-
fs.mkdirSync(getSystemPath(path));
106-
};
107-
_createDir(dirname(path));
108-
109-
_callFs<void, string, Uint8Array>(
95+
return _callFs<void, string, fs.MakeDirectoryOptions>(
96+
fs.mkdir,
97+
getSystemPath(dirname(path)),
98+
{ recursive: true },
99+
).pipe(
100+
mergeMap(() => _callFs<void, string, Uint8Array>(
110101
fs.writeFile,
111102
getSystemPath(path),
112103
new Uint8Array(content),
113-
).subscribe(obs);
114-
});
104+
)),
105+
);
115106
}
116107

117108
read(path: Path): Observable<virtualFs.FileBuffer> {
@@ -242,57 +233,30 @@ export class NodeJsSyncHost implements virtualFs.Host<fs.Stats> {
242233

243234
write(path: Path, content: virtualFs.FileBuffer): Observable<void> {
244235
return new Observable(obs => {
245-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
246-
// fixed.
247-
try {
248-
// Create folders if necessary.
249-
const _createDir = (path: Path) => {
250-
if (fs.existsSync(getSystemPath(path))) {
251-
return;
252-
}
253-
_createDir(dirname(path));
254-
fs.mkdirSync(getSystemPath(path));
255-
};
256-
_createDir(dirname(path));
257-
fs.writeFileSync(getSystemPath(path), new Uint8Array(content));
258-
259-
obs.next();
260-
obs.complete();
261-
} catch (err) {
262-
obs.error(err);
263-
}
236+
fs.mkdirSync(getSystemPath(dirname(path)), { recursive: true });
237+
fs.writeFileSync(getSystemPath(path), new Uint8Array(content));
238+
obs.next();
239+
obs.complete();
264240
});
265241
}
266242

267243
read(path: Path): Observable<virtualFs.FileBuffer> {
268244
return new Observable(obs => {
269-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
270-
// fixed.
271-
try {
272-
const buffer = fs.readFileSync(getSystemPath(path));
245+
const buffer = fs.readFileSync(getSystemPath(path));
273246

274-
obs.next(new Uint8Array(buffer).buffer as virtualFs.FileBuffer);
275-
obs.complete();
276-
} catch (err) {
277-
obs.error(err);
278-
}
247+
obs.next(new Uint8Array(buffer).buffer as virtualFs.FileBuffer);
248+
obs.complete();
279249
});
280250
}
281251

282252
delete(path: Path): Observable<void> {
283253
return this.isDirectory(path).pipe(
284254
concatMap(isDir => {
285-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
286-
// fixed.
287255
if (isDir) {
288256
const dirPaths = fs.readdirSync(getSystemPath(path));
289257
const rmDirComplete = new Observable<void>((obs) => {
290-
try {
291-
fs.rmdirSync(getSystemPath(path));
292-
obs.complete();
293-
} catch (e) {
294-
obs.error(e);
295-
}
258+
fs.rmdirSync(getSystemPath(path));
259+
obs.complete();
296260
});
297261

298262
return concat(
@@ -314,69 +278,43 @@ export class NodeJsSyncHost implements virtualFs.Host<fs.Stats> {
314278

315279
rename(from: Path, to: Path): Observable<void> {
316280
return new Observable(obs => {
317-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
318-
// fixed.
319-
try {
320-
const toSystemPath = getSystemPath(to);
321-
if (!fs.existsSync(path.dirname(toSystemPath))) {
322-
fs.mkdirSync(path.dirname(toSystemPath), { recursive: true });
323-
}
324-
fs.renameSync(getSystemPath(from), getSystemPath(to));
325-
obs.next();
326-
obs.complete();
327-
} catch (err) {
328-
obs.error(err);
329-
}
281+
const toSystemPath = getSystemPath(to);
282+
fs.mkdirSync(path.dirname(toSystemPath), { recursive: true });
283+
fs.renameSync(getSystemPath(from), toSystemPath);
284+
obs.next();
285+
obs.complete();
330286
});
331287
}
332288

333289
list(path: Path): Observable<PathFragment[]> {
334290
return new Observable(obs => {
335-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
336-
// fixed.
337-
try {
338-
const names = fs.readdirSync(getSystemPath(path));
339-
obs.next(names.map(name => fragment(name)));
340-
obs.complete();
341-
} catch (err) {
342-
obs.error(err);
343-
}
291+
const names = fs.readdirSync(getSystemPath(path));
292+
obs.next(names.map(name => fragment(name)));
293+
obs.complete();
344294
});
345295
}
346296

347297
exists(path: Path): Observable<boolean> {
348298
return new Observable(obs => {
349-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
350-
// fixed.
351-
try {
352-
obs.next(fs.existsSync(getSystemPath(path)));
353-
obs.complete();
354-
} catch (err) {
355-
obs.error(err);
356-
}
299+
obs.next(fs.existsSync(getSystemPath(path)));
300+
obs.complete();
357301
});
358302
}
359303

360304
isDirectory(path: Path): Observable<boolean> {
361305
// tslint:disable-next-line:no-non-null-assertion
362-
return this.stat(path) !.pipe(map(stat => stat.isDirectory()));
306+
return this.stat(path)!.pipe(map(stat => stat.isDirectory()));
363307
}
364308
isFile(path: Path): Observable<boolean> {
365309
// tslint:disable-next-line:no-non-null-assertion
366-
return this.stat(path) !.pipe(map(stat => stat.isFile()));
310+
return this.stat(path)!.pipe(map(stat => stat.isFile()));
367311
}
368312

369313
// Some hosts may not support stat.
370314
stat(path: Path): Observable<virtualFs.Stats<fs.Stats>> {
371315
return new Observable(obs => {
372-
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
373-
// fixed.
374-
try {
375-
obs.next(fs.statSync(getSystemPath(path)));
376-
obs.complete();
377-
} catch (err) {
378-
obs.error(err);
379-
}
316+
obs.next(fs.statSync(getSystemPath(path)));
317+
obs.complete();
380318
});
381319
}
382320

0 commit comments

Comments
 (0)