-
Notifications
You must be signed in to change notification settings - Fork 0
/
eyes-on.js
executable file
·154 lines (136 loc) · 5.39 KB
/
eyes-on.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
const fs = require('fs');
const chalk = require('chalk');
const Termparse = require('termparse');
const spawnSync = require('child_process').spawnSync;
var tp = new Termparse();
const banner = () => {
let B = `
▒██▀░▀▄▀▒██▀░▄▀▀░▒░░▄▀▄░█▄░█
░█▄▄░▒█▒░█▄▄▒▄██░▀▀░▀▄▀░█▒▀█
`
console.log(chalk.greenBright(B));
}
//args is list of files(not flags/options)
var filter_files = function (args) {
var file_info_list = new Object();
var found_files = "";
var not_found_files = "";
args.map(f => {
if (fs.existsSync(f)) {
file_info_list[f] = new Object();
file_info_list[f].exists = true;
file_info_list[f].modified = false;
file_info_list[f].exists = true;
file_info_list[f].mod_time_ms = fs.statSync(f).mtimeMs;
file_info_list[f].mod_time = fs.statSync(f).mtime;
found_files += ` ${f}`; //update found files list
} else {
not_found_files += ` ${f}`; //update not found files list
}
});
if (found_files.length > 0) console.log(chalk.black.bgYellowBright(`FOUND`), chalk.underline(found_files));
if (not_found_files.length > 0) {
console.log(chalk.whiteBright.bgRed(`NOT FOUND`), chalk.underline(not_found_files));
console.log(chalk.red.italic('\nfiles are missing among the files passed.'));
console.log(chalk.red('exiting...'));
process.exit();
}
return file_info_list;
}
//checks for modified file and gives signal
var onChange = (f, file_info_list) => {
if (fs.existsSync(f)) {
var f_info = fs.statSync(f);
if (file_info_list[f].mod_time_ms !== f_info.mtimeMs) {
file_info_list[f].mod_time_ms = f_info.mtimeMs;
file_info_list[f].mod_time = f_info.mtime;
file_info_list[f].exists = true;
file_info_list[f].modified = true;
} else {
file_info_list[f].modified = false;
}
} else {
file_info_list[f].exists = false;
}
return file_info_list;
}
//TODO: detect exit key and give options
//takes string as input=> splits based on line break => splits based on spaces
var commandExec = function (raw_cmd) {
let cmds = raw_cmd.split('\n');
for (c of cmds) {
if (c.length > 0) {
let p_cmd = c.split(" ");
var cmd_res = spawnSync(p_cmd[0], p_cmd.slice(1), { encoding: 'utf8' });
if (cmd_res.stderr.length > 0) {
console.log(chalk.blueBright("executing command..."));
console.log(chalk.whiteBright.bgRed.underline("ERR:"), "\n", cmd_res.stderr);
console.log(chalk.yellow("waiting for changes in file"));
break;
}
if (cmd_res.stdout.length > 0) {
console.log(chalk.blueBright("executing command..."));
console.log(chalk.black.bgYellowBright.underline(c), "\n", cmd_res.stdout);
console.log(chalk.blueBright("command exected"));
}
}
}
}
//calls onChange() => receives signal => gives appropriate message
//cmd_list :string
var watch = (file_info_list, cmd_list) => {
if (Object.keys(file_info_list).length === 0) {
console.log(chalk.redBright.italic(`\nno files left to watch`));
console.log(chalk.redBright('exiting...'));
process.exit();
}
for (file in file_info_list) {
file_info_list = onChange(file, file_info_list);
mod_time = file_info_list[file].mod_time;
if (!file_info_list[file].exists) {
console.log("\n---------------------------------------------");
console.log(`${mod_time} :: ${file} :: ${chalk.yellowBright.bold('DELETED')}`);
delete file_info_list[file];
console.log("---------------------------------------------\n");
} else if (file_info_list[file].modified) {
console.log("\n---------------------------------------------");
console.log(`${mod_time} :: ${file} :: ${chalk.blueBright.bold('MODIFIED')}`);
commandExec(cmd_list);
console.log("---------------------------------------------\n");
}
}
}
/******************** driver code **************************/
//command: watch => watches over one or multiple files
tp.addCommand({
name: "watch",
usage: "used to watch over one or multiple file",
run: function () {
if (tp.args.length === 0) {
console.log(chalk.red("no file name or pattern passed"));
process.exit();
}
let exec_command = this.getFlag('c');
let time_interval = this.getFlag('t');
banner();
console.log(chalk.green.bold('starting...'));
let file_list = filter_files(tp.args);
console.log(chalk.greenBright.bold('watching...'));
setInterval(() => {
watch(file_list, exec_command.value);
}, parseInt(time_interval.value));
}
}).setFlags({
name: "t",
type: "number",
value: 2000,
usage: "takes time in milliseconds as input to set the watch interval (default: 2000ms/2s )"
}, {
name: "c",
type: "string",
value: "",
usage: "takes one or multiple commands and executes on modification of file"
})
var args = process.argv.slice(2);
tp.parse(args); //parsing args and executing appropriate command