-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (124 loc) · 4.66 KB
/
index.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
var fs = require('fs'),
chalk = require('chalk'),
inquirer = require('inquirer');
var fin = (function() {
var _self;
return {
init: function() {
_self = this;
_self.promptUser();
},
promptUser: function(err) {
if(err != undefined && typeof err == 'string') {
console.log('\n%s\n', chalk.red(err));
}
inquirer.prompt([
{
type: 'list',
name: 'inputPreference',
message: 'How would you like to proceed?',
choices: [
{
name: 'Choose from a list of files in this directory',
value: 'choose',
checked: false
},
{
name: 'Manually enter a file path',
value: 'input',
checked: false
}
]
}
], function(answers) {
if(answers.inputPreference == 'choose') {
_self.listSpecificFiles();
} else {
_self.receiveFilePath();
}
});
},
receiveFilePath: function() {
inquirer.prompt([
{
type: 'input',
name: 'filePath',
message: 'Enter the file path you would like Fin to work with:'
}
], function(answers) {
if(answers.filePath !== '') {
_self.startFin([answers.filePath]);
} else {
_self.promptUser('No file path provided. Returning to main prompt.');
}
});
},
listSpecificFiles: function() {
var eligibleFiles = [],
allDirectoryFiles = fs.readdirSync(__dirname),
fTypePattern = new RegExp(/^.*\.(scss|SCSS)$/g),
selectedFiles = [];
for(var file in allDirectoryFiles) {
var fname = allDirectoryFiles[file];
if(! fname.match(fTypePattern)) {
continue;
}
eligibleFiles.push({
name: fname,
value: [__dirname,fname].join('/'),
checked: false
});
}
if(eligibleFiles.length) {
inquirer.prompt([
{
type: 'checkbox',
name: 'files',
message: 'Select the file(s) you would like run through Fin:',
choices: eligibleFiles
}
], function(answers) {
if(answers.files.length) {
for(var file in answers.files) {
selectedFiles.push(answers.files[file]);
}
_self.startFin(selectedFiles);
} else {
_self.promptUser('No file(s) selected. Returning to main prompt.');
}
});
} else {
_self.promptUser('There are no SCSS files in the current directory.');
}
},
startFin: function(filePaths) {
if(typeof filePaths != 'object') {
return;
}
for(var path in filePaths) {
fs.open(filePaths[path], 'r+', function(err, fd) {
if(err) {
throw err;
} else {
var fileName = filePaths[path];
fs.stat(fileName, function(err, stats) {
if(stats.size) {
var buffer = new Buffer(stats.size);
console.log(buffer.length);
fs.read(fd, buffer, 0, buffer.length, null, function(err, bytesRead, buffer) {
if(err) {
throw err;
}
var data = buffer.toString('utf8', 0, buffer.length);
console.log(data);
fs.close(fd);
});
}
});
}
});
}
}
};
})();
fin.init();