Skip to content

Commit 30d35c0

Browse files
authored
Add watcher
1 parent afe8be5 commit 30d35c0

File tree

4 files changed

+127
-20
lines changed

4 files changed

+127
-20
lines changed

package-lock.json

Lines changed: 109 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"@octokit/plugin-throttling": "^3.5.2",
7575
"@octokit/rest": "18.0.0",
7676
"colors": "1.4.0",
77+
"cross-platform-file-watch": "^3.5.2",
7778
"ec2-spot-notification": "^2.0.3",
7879
"exponential-backoff": "^3.1.0",
7980
"form-data": "^3.0.1",

report.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/cml.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const globby = require('globby');
55
const git = require('simple-git')('./');
66
const path = require('path');
77
const fs = require('fs');
8+
const chokidar = require('chokidar');
89

910
const winston = require('winston');
1011

@@ -22,8 +23,6 @@ const GITHUB = 'github';
2223
const GITLAB = 'gitlab';
2324
const BB = 'bitbucket';
2425

25-
const paths = new Set();
26-
2726
const uriNoTrailingSlash = (uri) => {
2827
return uri.endsWith('/') ? uri.substr(0, uri.length - 1) : uri;
2928
};
@@ -171,7 +170,21 @@ class CML {
171170
const { remark } = await import('remark');
172171
const { visit } = await import('unist-util-visit');
173172

174-
paths.add(markdownfile);
173+
const watcher = chokidar.watch(markdownfile, {
174+
persistent: true,
175+
176+
awaitWriteFinish: {
177+
stabilityThreshold: 2000,
178+
pollInterval: 100
179+
}
180+
});
181+
182+
if (watch) {
183+
watcher.on('all', () => {
184+
console.log('updating report...');
185+
this.commentCreate({ ...opts, update: true, watch: false });
186+
});
187+
}
175188

176189
const publishLocalFiles = () => async (tree) => {
177190
const nodes = [];
@@ -186,7 +199,7 @@ class CML {
186199
return;
187200
}
188201
node.url = await this.publish({ ...opts, path: node.url });
189-
paths.add(node.url);
202+
watcher.add(node.url);
190203
}
191204
};
192205

@@ -196,12 +209,6 @@ class CML {
196209
if (publish)
197210
report = String(await remark().use(publishLocalFiles).process(report));
198211

199-
if (watch)
200-
setInterval(() => {
201-
console.log('updating report...');
202-
this.commentCreate({ ...opts, update: true, watch: false });
203-
}, 16384); // TODO: use a proper file watcher on the `paths` set
204-
205212
let comment;
206213
const updatableComment = (comments) => {
207214
return comments.reverse().find(({ body }) => {

0 commit comments

Comments
 (0)