-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathclean-generated-files.js
99 lines (86 loc) · 3.11 KB
/
clean-generated-files.js
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
'use strict';
const path = require('path');
const Glob = require('glob').Glob;
const fse = require('fs-extra');
const scriptDir = __dirname;
const baseDir = path.resolve(scriptDir, '..');
// Execute a function for each match to a glob query
//
// Parameters:
// globPattern: String glob pattern for node-glob
// mapFn: Function function(pathRelativeToCwd), should return a promise or list of promises
// globOptions: Object of options passed directly to node-glob
//
// Returns: Promise that resolves with array of results from mapFn applies to all glob matches
function mapPromiseFnOverGlob(globPattern, mapFn, globOptions) {
return new Promise(function(resolve, reject) {
let promises = [];
// trailing slash will match only directories
new Glob(globPattern, globOptions)
.on('match', function(match) {
var result = mapFn(match);
if (result instanceof Array) {
promises = promises.concat(result);
} else if (result instanceof Promise) {
promises.push(result);
} else {
promises.push(Promise.resolve(result));
}
})
.on('end', function() {
// wait for all file ops to finish
Promise.all(promises).then(resolve).catch(reject);
})
.on('error', function(err) {
reject(err);
})
.on('abort', function() {
reject(new Error('Aborted'));
});
});
}
function rmFileGlobAsync(globPattern) {
return mapPromiseFnOverGlob(globPattern, function(filePath) {
console.log(filePath);
var absPath = path.resolve(baseDir, filePath);
return fse.remove(absPath);
}, {
cwd: baseDir,
nodir: true,
ignore: [
'./node_modules/**'
]
});
}
function cleanGeneratedFilesAsync() {
// trailing slash will match only directories
var jsPromise = rmFileGlobAsync('./**/*.autogen.js');
var jsonPromise = rmFileGlobAsync('./**/*.autogen.json');
var jsIndexPromise = rmFileGlobAsync('./**/index.js');
var pyPromise = rmFileGlobAsync('../pythreejs/**/*_autogen.py');
var pyIndexPromise = rmFileGlobAsync('../pythreejs/**/__init__.py');
var cppPromise1 = rmFileGlobAsync('../xthreejs/**/*_autogen.hpp');
var cppPromise2 = rmFileGlobAsync('../xthreejs/include/xthreejs/*.hpp');
var cppPromise3 = rmFileGlobAsync('../xthreejs/**/*_autogen.cpp');
var cmakePromise = rmFileGlobAsync('../xthreejs/CMakeLists.txt');
var docPromise = rmFileGlobAsync('../docs/source/**/*_autogen.rst');
var docIndexPromise = rmFileGlobAsync('../docs/source/api/**/index.rst');
return Promise.all([
jsPromise,
jsonPromise,
jsIndexPromise,
pyPromise,
pyIndexPromise,
cppPromise1,
cppPromise2,
cppPromise3,
cmakePromise,
docPromise,
docIndexPromise,
]);
}
if (require.main === module) {
cleanGeneratedFilesAsync().then(function() {
console.log('DONE');
});
}