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 ```ts
/// <reference types="node" />
import { ErrorObject } from 'ajv'; import { ErrorObject } from 'ajv';
import { Format } from 'ajv'; import { Format } from 'ajv';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';

View File

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

View File

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

View File

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

View File

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

View File

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