This repository was archived by the owner on Apr 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfileUtils.js
More file actions
171 lines (150 loc) · 4.94 KB
/
fileUtils.js
File metadata and controls
171 lines (150 loc) · 4.94 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
let fs = require('fs');
let path = require('path');
let wildcard = require('wildcard');
let bluebird = require('bluebird');
bluebird.promisifyAll(fs);
let arraySpread = require('./arraySpread');
/**
* Fileutils provides utilities for doing file/folder listing
* adding all files in directory excluding ignored files and parsing
* inside the directories by following package.json dependencies
* The package dependencies can either be in the top level of node_modules
* folder or it can be in a nested node_modules. The reason is that
* if directly installed version is different from the one required, then
* it choses to install the required version right inside the
* current module.
*/
class FileUtils {
/**
* Configure the ignored list
* @param {string} ignoreListFile A gitignore file to ignore
*/
setIgnoreFile(ignoreListFile) {
let ignored = fs.readFileSync('.gitignore').toString().split('\n');
// ignore comments from the ignore list
ignored = ignored.filter(
(l) => (l !== '' && l.trim().substr(0, 1) !== '#')
);
this.ignored = ignored;
}
/**
* set the value of modules which will be ignored from package deployment
* @param {Array<string>} tobeIgnoredModules Array of string of names of
* modules which will not be in zip file
*/
setIgnoredModules(tobeIgnoredModules) {
this.tobeIgnoredModules = tobeIgnoredModules;
}
/**
* get array of all files in a directory excluding ignore list
* and ignoring the dot files:
* @param {string} baseDir The base directory for which all files
* are required
* @param {boolean} Ignore should you exclude ignore list here
* @return {Promise<Array<string>>} promise to return an array of file paths
*/
getAllFiles(baseDir, Ignore) {
return fs.readdirAsync(baseDir)
.then((files) => Promise.all(files
// ignore the dot file
.filter((f) => f.substr(0, 1) !== '.')
// ignore the dot file
.filter((f) => f !== 'node_modules')
// ignore file in ignore list if 2nd arg is true
.filter(
(f) => this.ignored.reduce(
(a, b) => !Ignore || (a && !wildcard(b, f)),
true
)
)
// add relative pathname
.map((f) => path.join(baseDir, f))
// add files to list and exclude directory
.map((f) => fs.statAsync(f)
.then((stat) => {
if(stat.isDirectory())
return this.getAllFiles(f);
else if (stat.isFile())
return f;
})
)
))
.then((files) => arraySpread(files));
}
/**
* Include all dependencies of a p
* @param {string} baseDir The base directory for which all files
* are required
* @param {boolean} ignore should you exclude ignore list here
* @return {Promise<Array<string>>} promise to return an array of file paths
*/
includeDependenciesFiles(baseDir, ignore) {
let currDirFiles;
return this.getAllFiles(baseDir, ignore)
.then((files) => {
currDirFiles = files;
let packageJson = require(path.join(baseDir, 'package.json'));
let b = packageJson.dependencies;
if (packageJson.name === 'cloudhopper') {
return [];
}
return Object.keys(b || {});
})
.then((modules) =>
modules.filter((m) => this.tobeIgnoredModules.indexOf(m) === -1)
)
.then((modules) => {
let moduleFilesPromise = Promise.all(
modules.map(
(module) => {
let nextPath = path.join(baseDir, 'node_modules', module);
if (!fs.existsSync(nextPath)) {
nextPath = path.join(process.cwd(), 'node_modules', module);
}
return this.includeDependenciesFiles(nextPath, false);
}
)
);
let nextReturn = currDirFiles.concat(
moduleFilesPromise
);
return Promise.all(nextReturn);
})
.then((files) => {
let combined = arraySpread(files);
return combined.reduce((a, b) => {
return a.concat(b);
}, []);
});
}
}
module.exports = FileUtils;
/**
* Documentation for using this:
* =============================
let fu = new FileUtils();
fu.setIgnoreFile('.gitignore');
let zip = new JSZip();
fu.includeDependenciesFiles(process.cwd(), true)
.then((currDirFiles)=> {
console.log(currDirFiles);
currDirFiles.forEach((f) => {
zip.file(f.replace(process.cwd(), ''), fs.readFileSync(f));
});
console.log('writing to file ');
zip
.generateNodeStream({
type: 'nodebuffer',
streamFiles: true,
compression: 'DEFLATE',
compressionOptions: {
level: 9,
},
})
.pipe(fs.createWriteStream(this.config.tempFile))
.on('finish', () => {
console.log('Zip created');
});
});
*/