refactor(@angular-devkit/core): use chokidar types instead of custom interface

Replace `ChokidarWatcher` with types from `chokidar`
This commit is contained in:
Alan Agius 2021-10-08 13:10:47 +02:00
parent 1b3a5429b0
commit a169f26513
6 changed files with 16 additions and 17 deletions

View File

@ -4,6 +4,8 @@
```ts
/// <reference types="node" />
import { ErrorObject } from 'ajv';
import { Format } from 'ajv';
import { Observable } from 'rxjs';

View File

@ -4,6 +4,8 @@
```ts
/// <reference types="node" />
import { analytics } from '@angular-devkit/core';
import { BaseException } from '@angular-devkit/core';
import { logging } from '@angular-devkit/core';

View File

@ -4,6 +4,8 @@
```ts
/// <reference types="node" />
// @public (undocumented)
export class NodePackageInstallTask implements TaskConfigurationGenerator<NodePackageTaskOptions> {
constructor(workingDirectory?: string);

View File

@ -4,6 +4,8 @@
```ts
/// <reference types="node" />
import { analytics } from '@angular-devkit/core';
import { logging } from '@angular-devkit/core';
import { Observable } from 'rxjs';

View File

@ -28,6 +28,7 @@ ts_library(
deps = [
"//packages/angular_devkit/core",
"@npm//@types/node",
"@npm//chokidar",
"@npm//rxjs",
],
)

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import type { FSWatcher as ChokidarWatcher } from 'chokidar';
import fs, {
PathLike,
Stats,
@ -20,7 +21,7 @@ import fs, {
unlinkSync,
writeFileSync,
} from 'fs';
import { dirname as pathDirname, join as pathJoin } from 'path';
import { dirname as pathDirname } from 'path';
import { Observable, concat, from as observableFrom, of, throwError } from 'rxjs';
import { concatMap, map, mergeMap, publish, refCount } from 'rxjs/operators';
import {
@ -34,18 +35,6 @@ import {
virtualFs,
} from '../src';
interface ChokidarWatcher {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new (options: {}): ChokidarWatcher;
add(path: string): ChokidarWatcher;
on(type: 'change', cb: (path: string) => void): ChokidarWatcher;
on(type: 'add', cb: (path: string) => void): ChokidarWatcher;
on(type: 'unlink', cb: (path: string) => void): ChokidarWatcher;
close(): void;
}
async function exists(path: PathLike): Promise<boolean> {
try {
await fsPromises.access(path, constants.F_OK);
@ -59,7 +48,7 @@ async function exists(path: PathLike): Promise<boolean> {
// This will only be initialized if the watch() method is called.
// Otherwise chokidar appears only in type positions, and shouldn't be referenced
// in the JavaScript output.
let FSWatcher: ChokidarWatcher;
let FSWatcher: typeof ChokidarWatcher;
function loadFSWatcher() {
if (!FSWatcher) {
try {
@ -161,7 +150,8 @@ export class NodeJsAsyncHost implements virtualFs.Host<Stats> {
): Observable<virtualFs.HostWatchEvent> | null {
return new Observable<virtualFs.HostWatchEvent>((obs) => {
loadFSWatcher();
const watcher = new FSWatcher({ persistent: true }).add(getSystemPath(path));
const watcher = new FSWatcher({ persistent: true });
watcher.add(getSystemPath(path));
watcher
.on('change', (path) => {
@ -308,9 +298,9 @@ export class NodeJsSyncHost implements virtualFs.Host<Stats> {
_options?: virtualFs.HostWatchOptions,
): Observable<virtualFs.HostWatchEvent> | null {
return new Observable<virtualFs.HostWatchEvent>((obs) => {
const opts = { persistent: false };
loadFSWatcher();
const watcher = new FSWatcher(opts).add(getSystemPath(path));
const watcher = new FSWatcher({ persistent: false });
watcher.add(getSystemPath(path));
watcher
.on('change', (path) => {