-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbeautify-solution.js
46 lines (45 loc) · 1.51 KB
/
beautify-solution.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
let fs = require('fs');
let path = require('path');
let beautify = require('js-beautify').js;
let beautifyOptions = JSON.parse(fs.readFileSync(path.join(__dirname,
'.jsbeautifyrc'), 'utf8'));
let ignoreFolders = ['$Recycle.Bin', '.vscode', 'node_modules', 'typings',
'.git','bootstrap'
];
let ignoreFiles = ['bundle.js'];
/**
* Gets all files where the extions is `.js` and the filename
* does not start with a `.`. This method is recusive, so all
* child folders are included in the search too.
* @param {} dirPath
*/
let getJsFilesRecursive = function(dirPath) {
let files = [];
let dirContents = fs.readdirSync(dirPath);
dirContents.forEach(element => {
let elementStat = fs.statSync(path.join(dirPath, element));
if (element.startsWith('.')) {
return;
}
if (elementStat.isDirectory()) {
if (ignoreFolders.indexOf(element) >= 0) {
return;
}
files = files.concat(getJsFilesRecursive(path.join(dirPath,
element)));
} else {
if (ignoreFiles.indexOf(element) >= 0 || !element.endsWith(
'.js')) {
return;
}
files.push(path.join(dirPath, element));
}
});
return files;
};
let files = getJsFilesRecursive(__dirname);
files.forEach(filePath => {
let beautifiedFileContents = beautify(fs.readFileSync(filePath, 'utf8'),
beautifyOptions);
fs.writeFileSync(filePath, beautifiedFileContents, 'utf8');
});