1 | const fs = require('fs'), path = require('path');
|
2 |
|
3 |
|
4 | module.exports = {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
10 | getCWD: function() {
|
11 | return __dirname;
|
12 | },
|
13 |
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 | isGit: function(root_json){
|
21 | json_str = JSON.stringify(root_json);
|
22 | return json_str.includes('.git')
|
23 | },
|
24 |
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 | isJS: function(file_name){
|
32 | return file_name.endsWith('.js')
|
33 | },
|
34 |
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | walkDir: function(dir, root_json) {
|
42 | root_json[dir] = [];
|
43 | fs.readdirSync(dir).forEach( f => {
|
44 | let dirPath = path.join(dir, f);
|
45 | let isDirectory = fs.statSync(dirPath).isDirectory();
|
46 |
|
47 | let isDepend = dir.includes('node_modules') || dir.includes('.git');
|
48 |
|
49 | (isDirectory && !isDepend) ? this.walkDir(dirPath, root_json) :
|
50 | ((this.isJS(f) && !isDepend) ? root_json[dir].push(f) : {})
|
51 | });
|
52 | },
|
53 |
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | loadPackage: function(file_path){
|
61 | return JSON.parse(fs.readFileSync(file_path, 'utf8'));
|
62 | },
|
63 |
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | saveResult: function(file_path, result){
|
72 | fs.writeFileSync(file_path, JSON.stringify(result));
|
73 | console.log("INFO - Created temp file for created report.");
|
74 | },
|
75 |
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | correctRoot: function(file_path){
|
83 | if (file_path.includes('node_modules')){
|
84 | end_index = file_path.indexOf('node_modules');
|
85 | return file_path.slice(0, end_index);
|
86 | }
|
87 | return file_path
|
88 | },
|
89 |
|
90 | getDependencyList: function(package_json){
|
91 | keys = Object.keys(package_json);
|
92 | dependencies_base = keys.includes('dependencies') ? package_json.dependencies : null;
|
93 | dependencies_dev = keys.includes('devDependencies') ? package_json.devDependencies : null;
|
94 |
|
95 | if (dependencies_base == null){
|
96 | return dependencies_dev;
|
97 | }
|
98 | if (dependencies_dev == null){
|
99 | return dependencies_base;
|
100 | }
|
101 | else{
|
102 | return Object.assign(dependencies_base, dependencies_dev);
|
103 | }
|
104 | }
|
105 | };
|