forked from natemcdowel/angular-mock-and-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clear.mocks.js
56 lines (48 loc) · 1.33 KB
/
clear.mocks.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
let find = require('find');
let rl = require('readline');
let fs = require('fs');
let config = require('./config.json');
class ClearMocks {
ask(question, callback) {
let r = rl.createInterface({
input: process.stdin,
output: process.stdout});
r.question(question + '\n', function(answer) {
r.close();
callback(answer);
});
}
getQuestionText(mock, files) {
let out = '';
console.log(files);
out = '\nAre you sure you want to delete mocks for "' + mock + '" ? (Y/N)';
out += '\n\nThe files above will be removed.\n';
return out;
}
clear(mock) {
if (!mock) {
console.log('Please add an argument with the name of the mock.');
}
find.file(config.recording_dir + '/' + mock + '.json', __dirname, files => {
if (files.length) {
this.ask(this.getQuestionText(mock, files), answer => {
if (answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes') {
files.forEach(file => this.deleteFile(file));
}
});
} else {
console.log('No mocks were found for "' + mock + '"');
}
});
}
deleteFile(file) {
fs.unlink(file, (err) => {
if (err) {
throw err;
}
console.log(file + ' was deleted');
});
}
}
let clearMocks = new ClearMocks();
clearMocks.clear(process.argv[2]);