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