This repository was archived by the owner on Jun 17, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (201 loc) · 4.86 KB
/
Copy pathindex.js
File metadata and controls
214 lines (201 loc) · 4.86 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const fs = require('fs');
const path = require('path');
const hasbin = require('hasbin');
const execa = require('execa');
const pify = require('pify');
const globby = require('globby');
const git = require('simple-git')(path.join(__dirname));
const doraRepo = 'https://github.com/camac/dora.git';
const doraDir = path.join(__dirname, 'dora');
const instFile = path.join(doraDir, 'Install.pl');
const xslCleanFile = path.join(doraDir, 'xsl', 'DXLClean.xsl');
const tmpDir = '.tmp';
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const ncp = require('ncp').ncp;
ncp.limit = 16;
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
/**
* Checks for XSLTProc binary.
*
* @return boolean
*/
function hasXsltProc() {
return hasbin.sync('xsltproc');
}
/**
* Performs installation of DORA to create xsltproc bin in user directory;
* this is the same as invoking the `Install.pl` with a copy of DORA.
*/
function installUserDora() {
execa(instFile, ['--install']);
}
/**
* Checks for dependencies.
*
* @return boolean
*/
function hasDeps() {
return hasXsltProc();
}
/**
* Checks for DORA in __dirname.
*
* @return boolean
*/
function hasDoraSubmodule() {
let yn = false;
try {
const exStat = fs.statSync(doraDir);
if (exStat.isDirectory()) {
yn = true;
} else {
yn = false;
}
} catch (err) {
yn = false;
}
return yn;
}
/**
* Performs install of DORA as "submodule". This is already a git submodule
* in the git repo, but with end-user, it needs to be pulled as an asset;
* this occurs during post-install of this npm module.
*/
function installDoraSubmodule() {
git.clone(doraRepo, path.join(__dirname, 'dora'));
}
/**
* checks for and installs necessary DORA components
*/
function installDeps() {
if (!hasDoraSubmodule()) {
// this should only occur for `npm install` users, as the git repo will include the submodule
installDoraSubmodule();
}
if (!hasXsltProc()) {
installUserDora();
}
}
/**
* @param String odpPath
*
* @returns String[] of files to match.
*/
function getFileAr(odpPath) {
const odp = odpPath[odpPath.length - 1] === '/' ? odpPath : odpPath += '/';
const globAr = [
odp + '**/*.aa',
odp + '**/*.column',
odp + '**/*.dcr',
odp + '**/*.fa',
odp + '**/*.field',
odp + '**/*.folder',
odp + '**/*.form',
odp + '**/*.frameset',
odp + '**/*.ija',
odp + '**/*.ja',
odp + '**/*.javalib',
odp + '**/*.lsa',
odp + '**/*.lsdb',
odp + '**/*.metadata',
odp + '**/*.navigator',
odp + '**/*.outline',
odp + '**/*.page',
odp + '**/*.subform',
odp + '**/*.view',
odp + 'Resources/AboutDocument',
odp + 'AppProperties/database.properties',
odp + 'Resources/IconNote',
odp + 'Code/actions/Shared Actions',
odp + 'Resources/UsingDocument'
];
return globby(globAr);
}
/**
* Ensures that a given destination directory exists.
*
* @param String p
* @param function cb
*/
function ensureDir(p, cb) {
cb = cb || (() => {});
mkdirp(p, er => {
if (er) {
console.error(er);
} else {
cb();
}
});
}
/**
* @param String f
* @param function cb
*/
function performXsltTransform(f, cb) {
cb = cb || (() => {});
const cmd = 'xsltproc';
const args = ['-o', path.join(tmpDir, f), xslCleanFile, f];
const res = execa.sync(cmd, args);
cb(res.error);
}
/**
* @param String file
*/
function handleFileToTransform(file) {
// replaces running, per element `xsltproc -o ~/Desktop/tmp.txt xsl/DXLClean.xsl test/binarydxl/SomeView.view`
const nameAr = file.split('/');
nameAr.pop();
return new Promise((resolve, reject) => {
ensureDir(path.join(tmpDir, ...nameAr), () => {
performXsltTransform(file, er => {
if (er) {
reject('💩 ' + er.toString());
} else {
resolve('🦄');
}
});
});
});
}
/**
* Performs actual XSLT transformations for matching files.
*
* @param String dir
* @param function cb
*/
function performDoraXslt(dir, cb) {
cb = cb || (() => {});
let er = false;
// 1. cycle through all matching files from matches
getFileAr(dir).then(fileAr => {
// 2. perform transform, 3. output to tmp dir
Promise.all(fileAr.map(handleFileToTransform)).then(() => {
// 4. write back
ncp(path.join(tmpDir, dir), dir, err => {
if (err) {
er = true;
console.error(err);
}
// clean tmpDir
rimraf(tmpDir, () => {
console.log('\ndone!');
er = false;
});
});
})
.then(() => {
console.log(`\nprocessed ${fileAr.length} files that match criteria`);
cb(er);
});
});
}
module.exports = {
hasDependencies: hasDeps,
installDependencies: installDeps,
hasXsltProc,
installXsltProc: installUserDora,
performFilter: performDoraXslt
};