UNPKG

3.38 kBJavaScriptView Raw
1const fs = require('fs'), path = require('path');
2
3
4let is_git = false;
5
6module.exports = {
7 /*
8 description : get currenct wd
9 requires : cmd - null
10 returns : str
11 */
12 getCWD: function() {
13 return __dirname;
14 },
15
16
17 /*
18 description : function to check if it is git
19 requires : root_json -> JSON
20 returns : bool
21 */
22 isGit: function(root_json){
23 return is_git;
24 },
25
26
27 /*
28 description : function to check if it is js
29 requires : file_name -> str
30 returns : bool
31 */
32 isJS: function(file_name){
33 return file_name.endsWith('.js')
34 },
35
36
37 /*
38 description : function to walk through base dir and collect all js file
39 requires : root_json -> JSON
40 returns : bool
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 // some project might contain broken linked files
47 try{
48 let isDirectory = fs.statSync(dirPath).isDirectory();
49 // check if this is a node_modules or .git folder
50 let isDepend = dir.includes('node_modules') || dir.includes('.git') || dir.includes('.vscode');
51 if(dir.includes('.git')) { is_git = true; }
52 // any walk to none depend folder (main) and add .js file
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 // clean empty one, we care only existing one
60 if(root_json[dir + '/'].length === 0){
61 delete root_json[dir + '/']
62 }
63 },
64
65
66 /*
67 description : load base package json
68 requires : file_path -> str
69 returns : JSON
70 */
71 loadPackage: function(file_path){
72 return JSON.parse(fs.readFileSync(file_path, 'utf8'));
73 },
74
75
76 /*
77 description : load base package json
78 requires : file_path -> str,
79 result -> JSON
80 returns : None
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 description : get correct root directory
90 requires : file_path -> str,
91 returns : str
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};