UNPKG

3.02 kBJavaScriptView Raw
1const fs = require('fs'), path = require('path');
2
3
4module.exports = {
5 /*
6 description : get currenct wd
7 requires : cmd - null
8 returns : str
9 */
10 getCWD: function() {
11 return __dirname;
12 },
13
14
15 /*
16 description : function to check if it is git
17 requires : root_json -> JSON
18 returns : bool
19 */
20 isGit: function(root_json){
21 json_str = JSON.stringify(root_json);
22 return json_str.includes('.git')
23 },
24
25
26 /*
27 description : function to check if it is js
28 requires : file_name -> str
29 returns : bool
30 */
31 isJS: function(file_name){
32 return file_name.endsWith('.js')
33 },
34
35
36 /*
37 description : function to walk through base dir and collect all js file
38 requires : root_json -> JSON
39 returns : bool
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 // check if this is a node_modules or .git folder
47 let isDepend = dir.includes('node_modules') || dir.includes('.git');
48 // any walk to none depend folder (main) and add .js file
49 (isDirectory && !isDepend) ? this.walkDir(dirPath, root_json) :
50 ((this.isJS(f) && !isDepend) ? root_json[dir].push(f) : {})
51 });
52 },
53
54
55 /*
56 description : load base package json
57 requires : file_path -> str
58 returns : JSON
59 */
60 loadPackage: function(file_path){
61 return JSON.parse(fs.readFileSync(file_path, 'utf8'));
62 },
63
64
65 /*
66 description : load base package json
67 requires : file_path -> str,
68 result -> JSON
69 returns : None
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 description : get correct root directory
79 requires : file_path -> str,
80 returns : str
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};