@parcel/watcher-watchman-js 2.12.1-canary.3335
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/lib/index.js +12 -0
- package/lib/index.js.map +1 -0
- package/lib/wrapper.js +135 -0
- package/package.json +18 -0
- package/src/index.js +8 -0
- package/src/wrapper.js +177 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017-present Devon Govett
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# @parcel/watcher-watchman-js
|
2
|
+
|
3
|
+
This package acts as a drop-in replacements for `@parcel/watcher` but only for
|
4
|
+
watchman. It's a temporary workaround for some bugs and feature gaps with the
|
5
|
+
C++ watchman client.
|
6
|
+
|
7
|
+
It is intended to replace `@parcel/watcher` within node_modules.
|
package/lib/index.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.writeSnapshot = exports.unsubscribe = exports.subscribe = exports.getEventsSince = void 0;
|
7
|
+
var _wrapper = require("./wrapper");
|
8
|
+
const wrapper = new _wrapper.ParcelWatcherWatchmanJS();
|
9
|
+
const writeSnapshot = exports.writeSnapshot = wrapper.writeSnapshot.bind(wrapper);
|
10
|
+
const getEventsSince = exports.getEventsSince = wrapper.getEventsSince.bind(wrapper);
|
11
|
+
const subscribe = exports.subscribe = wrapper.subscribe.bind(wrapper);
|
12
|
+
const unsubscribe = exports.unsubscribe = wrapper.unsubscribe.bind(wrapper);
|
package/lib/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"mappings":"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","sources":["packages/utils/parcel-watcher-watchman-js/src/index.js","packages/utils/parcel-watcher-watchman-js/src/wrapper.js"],"sourcesContent":["import {ParcelWatcherWatchmanJS} from './wrapper';\n\nconst wrapper = new ParcelWatcherWatchmanJS();\n\nexport const writeSnapshot = wrapper.writeSnapshot.bind(wrapper);\nexport const getEventsSince = wrapper.getEventsSince.bind(wrapper);\nexport const subscribe = wrapper.subscribe.bind(wrapper);\nexport const unsubscribe = wrapper.unsubscribe.bind(wrapper);\n","// @flow\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as watchman from 'fb-watchman';\nimport {isGlob} from '@parcel/utils';\nimport type {\n Options,\n Event,\n SubscribeCallback,\n AsyncSubscription,\n} from '@parcel/watcher';\n\ntype WatchmanArgs = any;\ntype FilePath = string;\ntype GlobPattern = string;\n\n// Matches the Watcher API from \"@parcel/watcher\"\nexport interface Watcher {\n getEventsSince(\n dir: FilePath,\n snapshot: FilePath,\n opts?: Options,\n ): Promise<Array<Event>>;\n subscribe(\n dir: FilePath,\n fn: SubscribeCallback,\n opts?: Options,\n ): Promise<AsyncSubscription>;\n unsubscribe(\n dir: FilePath,\n fn: SubscribeCallback,\n opts?: Options,\n ): Promise<void>;\n writeSnapshot(\n dir: FilePath,\n snapshot: FilePath,\n opts?: Options,\n ): Promise<FilePath>;\n}\n\nexport class ParcelWatcherWatchmanJS implements Watcher {\n subscriptionName: string;\n client: watchman.Client;\n\n constructor() {\n this.subscriptionName = 'parcel-watcher-subscription-' + Date.now();\n this.client = new watchman.Client();\n }\n\n commandAsync(args: any[]): Promise<any> {\n return new Promise((resolve, reject) => {\n const client = this.client;\n // $FlowFixMe\n client.command(args, (err: Error | null | undefined, response: any) => {\n if (err) reject(err);\n else resolve(response);\n });\n });\n }\n\n // Types should match @parcel/watcher/index.js.flow\n async writeSnapshot(dir: string, snapshot: FilePath): Promise<string> {\n const response = await this.commandAsync(['clock', dir]);\n fs.writeFileSync(snapshot, response.clock, {\n encoding: 'utf-8',\n });\n return response.clock;\n }\n\n async getEventsSince(\n dir: string,\n snapshot: FilePath,\n opts?: Options,\n ): Promise<Event[]> {\n const clock = fs.readFileSync(snapshot, {\n encoding: 'utf-8',\n });\n\n const response = await this.commandAsync([\n 'query',\n dir,\n {\n expression: this._createExpression(dir, opts?.ignore),\n fields: ['name', 'mode', 'exists', 'new'],\n since: clock,\n },\n ]);\n\n return (response.files || []).map((file: any) => ({\n path: file.name,\n type: file.new ? 'create' : file.exists ? 'update' : 'delete',\n }));\n }\n\n _createExpression(\n dir: string,\n ignore?: Array<FilePath | GlobPattern>,\n ): WatchmanArgs {\n const ignores = [\n // Ignore the watchman cookie\n ['match', '.watchman-cookie-*'],\n // Ignore directory changes as they are just noise\n ['type', 'd'],\n ];\n\n if (ignore) {\n const customIgnores = ignore?.map(filePathOrGlob => {\n const relative = path.relative(dir, filePathOrGlob);\n\n if (isGlob(filePathOrGlob)) {\n return ['match', relative, 'wholename'];\n }\n\n // If pattern is not a glob, then assume it's a directory.\n // Ignoring single files is not currently supported\n return ['dirname', relative];\n });\n\n ignores.push(...customIgnores);\n }\n\n return ['not', ['anyof', ...ignores]];\n }\n\n async subscribe(\n dir: string,\n fn: SubscribeCallback,\n opts?: Options,\n ): Promise<AsyncSubscription> {\n const {subscriptionName} = this;\n const {clock} = await this.commandAsync(['clock', dir]);\n\n await this.commandAsync([\n 'subscribe',\n dir,\n subscriptionName,\n {\n // `defer` can be used here if you want to pause the\n // notification stream until something has finished.\n //\n // https://facebook.github.io/watchman/docs/cmd/subscribe#defer\n // defer: ['my-company-example'],\n expression: this._createExpression(dir, opts?.ignore),\n fields: ['name', 'mode', 'exists', 'new'],\n since: clock,\n },\n ]);\n\n this.client.on('subscription', function (resp) {\n if (!resp.files || resp.subscription !== subscriptionName) {\n return;\n }\n\n fn(\n null /* err */,\n resp.files.map((file: any) => {\n return {\n path: path.join(dir, file.name),\n type: file.new ? 'create' : file.exists ? 'update' : 'delete',\n };\n }),\n );\n });\n\n const unsubscribe = async () => {\n await this.commandAsync(['unsubscribe', dir, subscriptionName]);\n };\n\n return {\n unsubscribe,\n };\n }\n\n async unsubscribe(dir: string): Promise<void> {\n await this.commandAsync(['unsubscribe', dir, this.subscriptionName]);\n }\n}\n"],"names":["ParcelWatcherWatchmanJS","wrapper","writeSnapshot","bind","getEventsSince","subscribe","unsubscribe","fs","path","watchman","isGlob","constructor","subscriptionName","Date","now","client","Client","commandAsync","args","Promise","resolve","reject","command","err","response","dir","snapshot","writeFileSync","clock","encoding","opts","readFileSync","expression","_createExpression","ignore","fields","since","files","map","file","name","type","new","exists","ignores","customIgnores","filePathOrGlob","relative","push","fn","on","resp","subscription","join"],"version":3,"file":"index.js.map"}
|
package/lib/wrapper.js
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.ParcelWatcherWatchmanJS = void 0;
|
7
|
+
function fs() {
|
8
|
+
const data = _interopRequireWildcard(require("fs"));
|
9
|
+
fs = function () {
|
10
|
+
return data;
|
11
|
+
};
|
12
|
+
return data;
|
13
|
+
}
|
14
|
+
function path() {
|
15
|
+
const data = _interopRequireWildcard(require("path"));
|
16
|
+
path = function () {
|
17
|
+
return data;
|
18
|
+
};
|
19
|
+
return data;
|
20
|
+
}
|
21
|
+
function watchman() {
|
22
|
+
const data = _interopRequireWildcard(require("fb-watchman"));
|
23
|
+
watchman = function () {
|
24
|
+
return data;
|
25
|
+
};
|
26
|
+
return data;
|
27
|
+
}
|
28
|
+
function _utils() {
|
29
|
+
const data = require("@parcel/utils");
|
30
|
+
_utils = function () {
|
31
|
+
return data;
|
32
|
+
};
|
33
|
+
return data;
|
34
|
+
}
|
35
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
36
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
37
|
+
// Matches the Watcher API from "@parcel/watcher"
|
38
|
+
class ParcelWatcherWatchmanJS {
|
39
|
+
constructor() {
|
40
|
+
this.subscriptionName = 'parcel-watcher-subscription-' + Date.now();
|
41
|
+
this.client = new (watchman().Client)();
|
42
|
+
}
|
43
|
+
commandAsync(args) {
|
44
|
+
return new Promise((resolve, reject) => {
|
45
|
+
const client = this.client;
|
46
|
+
// $FlowFixMe
|
47
|
+
client.command(args, (err, response) => {
|
48
|
+
if (err) reject(err);else resolve(response);
|
49
|
+
});
|
50
|
+
});
|
51
|
+
}
|
52
|
+
|
53
|
+
// Types should match @parcel/watcher/index.js.flow
|
54
|
+
async writeSnapshot(dir, snapshot) {
|
55
|
+
const response = await this.commandAsync(['clock', dir]);
|
56
|
+
fs().writeFileSync(snapshot, response.clock, {
|
57
|
+
encoding: 'utf-8'
|
58
|
+
});
|
59
|
+
return response.clock;
|
60
|
+
}
|
61
|
+
async getEventsSince(dir, snapshot, opts) {
|
62
|
+
const clock = fs().readFileSync(snapshot, {
|
63
|
+
encoding: 'utf-8'
|
64
|
+
});
|
65
|
+
const response = await this.commandAsync(['query', dir, {
|
66
|
+
expression: this._createExpression(dir, opts === null || opts === void 0 ? void 0 : opts.ignore),
|
67
|
+
fields: ['name', 'mode', 'exists', 'new'],
|
68
|
+
since: clock
|
69
|
+
}]);
|
70
|
+
return (response.files || []).map(file => ({
|
71
|
+
path: file.name,
|
72
|
+
type: file.new ? 'create' : file.exists ? 'update' : 'delete'
|
73
|
+
}));
|
74
|
+
}
|
75
|
+
_createExpression(dir, ignore) {
|
76
|
+
const ignores = [
|
77
|
+
// Ignore the watchman cookie
|
78
|
+
['match', '.watchman-cookie-*'],
|
79
|
+
// Ignore directory changes as they are just noise
|
80
|
+
['type', 'd']];
|
81
|
+
if (ignore) {
|
82
|
+
const customIgnores = ignore === null || ignore === void 0 ? void 0 : ignore.map(filePathOrGlob => {
|
83
|
+
const relative = path().relative(dir, filePathOrGlob);
|
84
|
+
if ((0, _utils().isGlob)(filePathOrGlob)) {
|
85
|
+
return ['match', relative, 'wholename'];
|
86
|
+
}
|
87
|
+
|
88
|
+
// If pattern is not a glob, then assume it's a directory.
|
89
|
+
// Ignoring single files is not currently supported
|
90
|
+
return ['dirname', relative];
|
91
|
+
});
|
92
|
+
ignores.push(...customIgnores);
|
93
|
+
}
|
94
|
+
return ['not', ['anyof', ...ignores]];
|
95
|
+
}
|
96
|
+
async subscribe(dir, fn, opts) {
|
97
|
+
const {
|
98
|
+
subscriptionName
|
99
|
+
} = this;
|
100
|
+
const {
|
101
|
+
clock
|
102
|
+
} = await this.commandAsync(['clock', dir]);
|
103
|
+
await this.commandAsync(['subscribe', dir, subscriptionName, {
|
104
|
+
// `defer` can be used here if you want to pause the
|
105
|
+
// notification stream until something has finished.
|
106
|
+
//
|
107
|
+
// https://facebook.github.io/watchman/docs/cmd/subscribe#defer
|
108
|
+
// defer: ['my-company-example'],
|
109
|
+
expression: this._createExpression(dir, opts === null || opts === void 0 ? void 0 : opts.ignore),
|
110
|
+
fields: ['name', 'mode', 'exists', 'new'],
|
111
|
+
since: clock
|
112
|
+
}]);
|
113
|
+
this.client.on('subscription', function (resp) {
|
114
|
+
if (!resp.files || resp.subscription !== subscriptionName) {
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
fn(null /* err */, resp.files.map(file => {
|
118
|
+
return {
|
119
|
+
path: path().join(dir, file.name),
|
120
|
+
type: file.new ? 'create' : file.exists ? 'update' : 'delete'
|
121
|
+
};
|
122
|
+
}));
|
123
|
+
});
|
124
|
+
const unsubscribe = async () => {
|
125
|
+
await this.commandAsync(['unsubscribe', dir, subscriptionName]);
|
126
|
+
};
|
127
|
+
return {
|
128
|
+
unsubscribe
|
129
|
+
};
|
130
|
+
}
|
131
|
+
async unsubscribe(dir) {
|
132
|
+
await this.commandAsync(['unsubscribe', dir, this.subscriptionName]);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
exports.ParcelWatcherWatchmanJS = ParcelWatcherWatchmanJS;
|
package/package.json
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"name": "@parcel/watcher-watchman-js",
|
3
|
+
"version": "2.12.1-canary.3335+1afd7f7d2",
|
4
|
+
"license": "MIT",
|
5
|
+
"main": "lib/index.js",
|
6
|
+
"source": "src/index.js",
|
7
|
+
"publishConfig": {
|
8
|
+
"access": "public"
|
9
|
+
},
|
10
|
+
"dependencies": {
|
11
|
+
"@parcel/utils": "2.0.0-canary.1712+1afd7f7d2",
|
12
|
+
"fb-watchman": "^2.0.2"
|
13
|
+
},
|
14
|
+
"devDependencies": {
|
15
|
+
"@parcel/watcher": "^2.4.1"
|
16
|
+
},
|
17
|
+
"gitHead": "1afd7f7d2894679880ff0d2b644a61d8734f3f89"
|
18
|
+
}
|
package/src/index.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
import {ParcelWatcherWatchmanJS} from './wrapper';
|
2
|
+
|
3
|
+
const wrapper = new ParcelWatcherWatchmanJS();
|
4
|
+
|
5
|
+
export const writeSnapshot = wrapper.writeSnapshot.bind(wrapper);
|
6
|
+
export const getEventsSince = wrapper.getEventsSince.bind(wrapper);
|
7
|
+
export const subscribe = wrapper.subscribe.bind(wrapper);
|
8
|
+
export const unsubscribe = wrapper.unsubscribe.bind(wrapper);
|
package/src/wrapper.js
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
// @flow
|
2
|
+
import * as fs from 'fs';
|
3
|
+
import * as path from 'path';
|
4
|
+
import * as watchman from 'fb-watchman';
|
5
|
+
import {isGlob} from '@parcel/utils';
|
6
|
+
import type {
|
7
|
+
Options,
|
8
|
+
Event,
|
9
|
+
SubscribeCallback,
|
10
|
+
AsyncSubscription,
|
11
|
+
} from '@parcel/watcher';
|
12
|
+
|
13
|
+
type WatchmanArgs = any;
|
14
|
+
type FilePath = string;
|
15
|
+
type GlobPattern = string;
|
16
|
+
|
17
|
+
// Matches the Watcher API from "@parcel/watcher"
|
18
|
+
export interface Watcher {
|
19
|
+
getEventsSince(
|
20
|
+
dir: FilePath,
|
21
|
+
snapshot: FilePath,
|
22
|
+
opts?: Options,
|
23
|
+
): Promise<Array<Event>>;
|
24
|
+
subscribe(
|
25
|
+
dir: FilePath,
|
26
|
+
fn: SubscribeCallback,
|
27
|
+
opts?: Options,
|
28
|
+
): Promise<AsyncSubscription>;
|
29
|
+
unsubscribe(
|
30
|
+
dir: FilePath,
|
31
|
+
fn: SubscribeCallback,
|
32
|
+
opts?: Options,
|
33
|
+
): Promise<void>;
|
34
|
+
writeSnapshot(
|
35
|
+
dir: FilePath,
|
36
|
+
snapshot: FilePath,
|
37
|
+
opts?: Options,
|
38
|
+
): Promise<FilePath>;
|
39
|
+
}
|
40
|
+
|
41
|
+
export class ParcelWatcherWatchmanJS implements Watcher {
|
42
|
+
subscriptionName: string;
|
43
|
+
client: watchman.Client;
|
44
|
+
|
45
|
+
constructor() {
|
46
|
+
this.subscriptionName = 'parcel-watcher-subscription-' + Date.now();
|
47
|
+
this.client = new watchman.Client();
|
48
|
+
}
|
49
|
+
|
50
|
+
commandAsync(args: any[]): Promise<any> {
|
51
|
+
return new Promise((resolve, reject) => {
|
52
|
+
const client = this.client;
|
53
|
+
// $FlowFixMe
|
54
|
+
client.command(args, (err: Error | null | undefined, response: any) => {
|
55
|
+
if (err) reject(err);
|
56
|
+
else resolve(response);
|
57
|
+
});
|
58
|
+
});
|
59
|
+
}
|
60
|
+
|
61
|
+
// Types should match @parcel/watcher/index.js.flow
|
62
|
+
async writeSnapshot(dir: string, snapshot: FilePath): Promise<string> {
|
63
|
+
const response = await this.commandAsync(['clock', dir]);
|
64
|
+
fs.writeFileSync(snapshot, response.clock, {
|
65
|
+
encoding: 'utf-8',
|
66
|
+
});
|
67
|
+
return response.clock;
|
68
|
+
}
|
69
|
+
|
70
|
+
async getEventsSince(
|
71
|
+
dir: string,
|
72
|
+
snapshot: FilePath,
|
73
|
+
opts?: Options,
|
74
|
+
): Promise<Event[]> {
|
75
|
+
const clock = fs.readFileSync(snapshot, {
|
76
|
+
encoding: 'utf-8',
|
77
|
+
});
|
78
|
+
|
79
|
+
const response = await this.commandAsync([
|
80
|
+
'query',
|
81
|
+
dir,
|
82
|
+
{
|
83
|
+
expression: this._createExpression(dir, opts?.ignore),
|
84
|
+
fields: ['name', 'mode', 'exists', 'new'],
|
85
|
+
since: clock,
|
86
|
+
},
|
87
|
+
]);
|
88
|
+
|
89
|
+
return (response.files || []).map((file: any) => ({
|
90
|
+
path: file.name,
|
91
|
+
type: file.new ? 'create' : file.exists ? 'update' : 'delete',
|
92
|
+
}));
|
93
|
+
}
|
94
|
+
|
95
|
+
_createExpression(
|
96
|
+
dir: string,
|
97
|
+
ignore?: Array<FilePath | GlobPattern>,
|
98
|
+
): WatchmanArgs {
|
99
|
+
const ignores = [
|
100
|
+
// Ignore the watchman cookie
|
101
|
+
['match', '.watchman-cookie-*'],
|
102
|
+
// Ignore directory changes as they are just noise
|
103
|
+
['type', 'd'],
|
104
|
+
];
|
105
|
+
|
106
|
+
if (ignore) {
|
107
|
+
const customIgnores = ignore?.map(filePathOrGlob => {
|
108
|
+
const relative = path.relative(dir, filePathOrGlob);
|
109
|
+
|
110
|
+
if (isGlob(filePathOrGlob)) {
|
111
|
+
return ['match', relative, 'wholename'];
|
112
|
+
}
|
113
|
+
|
114
|
+
// If pattern is not a glob, then assume it's a directory.
|
115
|
+
// Ignoring single files is not currently supported
|
116
|
+
return ['dirname', relative];
|
117
|
+
});
|
118
|
+
|
119
|
+
ignores.push(...customIgnores);
|
120
|
+
}
|
121
|
+
|
122
|
+
return ['not', ['anyof', ...ignores]];
|
123
|
+
}
|
124
|
+
|
125
|
+
async subscribe(
|
126
|
+
dir: string,
|
127
|
+
fn: SubscribeCallback,
|
128
|
+
opts?: Options,
|
129
|
+
): Promise<AsyncSubscription> {
|
130
|
+
const {subscriptionName} = this;
|
131
|
+
const {clock} = await this.commandAsync(['clock', dir]);
|
132
|
+
|
133
|
+
await this.commandAsync([
|
134
|
+
'subscribe',
|
135
|
+
dir,
|
136
|
+
subscriptionName,
|
137
|
+
{
|
138
|
+
// `defer` can be used here if you want to pause the
|
139
|
+
// notification stream until something has finished.
|
140
|
+
//
|
141
|
+
// https://facebook.github.io/watchman/docs/cmd/subscribe#defer
|
142
|
+
// defer: ['my-company-example'],
|
143
|
+
expression: this._createExpression(dir, opts?.ignore),
|
144
|
+
fields: ['name', 'mode', 'exists', 'new'],
|
145
|
+
since: clock,
|
146
|
+
},
|
147
|
+
]);
|
148
|
+
|
149
|
+
this.client.on('subscription', function (resp) {
|
150
|
+
if (!resp.files || resp.subscription !== subscriptionName) {
|
151
|
+
return;
|
152
|
+
}
|
153
|
+
|
154
|
+
fn(
|
155
|
+
null /* err */,
|
156
|
+
resp.files.map((file: any) => {
|
157
|
+
return {
|
158
|
+
path: path.join(dir, file.name),
|
159
|
+
type: file.new ? 'create' : file.exists ? 'update' : 'delete',
|
160
|
+
};
|
161
|
+
}),
|
162
|
+
);
|
163
|
+
});
|
164
|
+
|
165
|
+
const unsubscribe = async () => {
|
166
|
+
await this.commandAsync(['unsubscribe', dir, subscriptionName]);
|
167
|
+
};
|
168
|
+
|
169
|
+
return {
|
170
|
+
unsubscribe,
|
171
|
+
};
|
172
|
+
}
|
173
|
+
|
174
|
+
async unsubscribe(dir: string): Promise<void> {
|
175
|
+
await this.commandAsync(['unsubscribe', dir, this.subscriptionName]);
|
176
|
+
}
|
177
|
+
}
|