UNPKG

1.4 kBJavaScriptView Raw
1/*
2 This file is the executable run by watchexec
3 when a change is detected.
4
5 It will extract changes from the environment variables
6 set by watchexec and write to stdout in a format
7 readable by the file `../watchexec_watcher.js`.
8*/
9'use strict';
10
11const { EOL } = require('os');
12
13function withPrefixes(prefixes) {
14 return function withPrefix(arr, i) {
15 return arr.map(str => {
16 return `${prefixes[i]} ${str}`;
17 });
18 };
19}
20
21const allPrefixes = ['write', 'rename', 'remove', 'create'];
22
23function extractChanges(context) {
24 const {
25 WATCHEXEC_COMMON_PATH,
26 WATCHEXEC_WRITTEN_PATH,
27 WATCHEXEC_RENAMED_PATH,
28 WATCHEXEC_REMOVED_PATH,
29 WATCHEXEC_CREATED_PATH,
30 } = context;
31
32 let events = [
33 WATCHEXEC_WRITTEN_PATH,
34 WATCHEXEC_RENAMED_PATH,
35 WATCHEXEC_REMOVED_PATH,
36 WATCHEXEC_CREATED_PATH,
37 ];
38
39 let currentPrefixes = events
40 .map((l, i) => l && allPrefixes[i])
41 .filter(Boolean);
42
43 function toFullPath(arr) {
44 return arr.map(path => (WATCHEXEC_COMMON_PATH || '') + path);
45 }
46
47 let message = events
48 .filter(Boolean)
49 .map(str => str.split(':'))
50 .map(toFullPath)
51 .map(withPrefixes(currentPrefixes))
52 .reduce((e, memo) => memo.concat(e), [])
53 .join(EOL);
54
55 return message;
56}
57
58if (require.main === module) {
59 let message = extractChanges(process.env);
60 console.log(message);
61}
62
63module.exports = extractChanges;