-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·440 lines (390 loc) · 14.1 KB
/
index.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
const got = require('got');
const getStream = require('get-stream');
const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
const xml2js = require('xml2js');
const pify = require('pify');
const makeDir = require('make-dir');
const pathExists = require('path-exists');
const FormData = require('form-data');
const globby = require('globby');
const tmpPromise = require('tmp-promise');
const prettyBytes = require('pretty-bytes');
const logUpdate = require('log-update');
const isUrl = require('is-url');
const xml2jsAsync = pify(xml2js);
const fsAsync = pify(fs);
const log = console.log.bind(console); //eslint-disable-line
/**
* @typedef {Object}
* @property {String} [protocol="http"] Protocol for package manager service. (optional, default `http`)
* @property {String} [host="localhost"] Host for package manager service. (optional, default `localhost`)
* @property {Number} [port=4502] Port number for package manager service. (optional, default `4502`)
* @property {Boolean} [extractMetaDir=false] Flag to extract meta directory during push and pull.. (optional, default `false`)
* @property {String} [pkgPropFile="./META-INF/vault/properties.xml"] Path to package meta . properties.xml file (optional, default `./META-INF/vault/properties.xml`)
* @property {String} [jcrRootDir="jcr_root"] Name of JCR root directory. (optional, default `jcr_root`)
* @property {String} [pkgService="/crx/packmgr/service.jsp"] Path of package manager service. (optional, default `/crx/packmgr/service.jsp`)
* @property {String} [username="admin"] Username for package manager service authentication. (optional, default `admin`)
* @property {String} [password="admin"] Password for package manager service authentication. (optional, default `admin`)
* @property {Boolean} [installPkg=true] Flag, whether you want uploaded package installation. (optional, default `true`)
* @property {String} [pkgFilePattern="*.zip"] Package zip file search pattern. (optional, default `*.zip`)
* @property {String} [cwd=process.cwd()] Current working directory for operation. (optional, default `process.cwd()`)
*/
const defaultOptions = {
protocol: 'http',
host: 'localhost',
port: 4502,
extractMetaDir: false,
pkgPropFile: './META-INF/vault/properties.xml',
jcrRootDir: 'jcr_root',
pkgService: '/crx/packmgr/service.jsp',
username: 'admin',
password: 'admin',
installPkg: true,
pkgFilePattern: '*.zip',
cwd: process.cwd()
};
/**
* @namespace
*/
const aemPkg = {
/**
* @private
* @param {String} pkgPropFile Path for AEM package properties.xml file
* @returns {Promise}
*/
async getPkgNameFromMeta(pkgPropFile) {
let pkgPropsXml;
try {
pkgPropsXml = await fsAsync.readFile(path.resolve(pkgPropFile), 'utf-8');
} catch (err) {
let errMsg;
if (err.code === 'ENOENT') {
errMsg = `Error: Not a AEM package directory: ${err.path}`;
} else {
errMsg = err;
}
throw new Error(errMsg);
}
const pkgProps = await xml2jsAsync.parseString(pkgPropsXml);
const filteredNameNode = pkgProps.properties.entry.filter(
entry => entry.$.key === 'name'
);
return filteredNameNode[0]._;
},
/**
* @private
* @param {Object} opts Options to override default options
* @returns {Promise}
*/
getOptions(opts) {
const options = Object.assign({}, defaultOptions, opts);
const { host, port, protocol, pkgService, username, password } = options;
options.pkgServiceUrl = `${protocol}://${host}:${port}${pkgService}`;
options.auth = `${username}:${password}`;
return options;
},
/**
*
* @param {String} pkgName Name of the package to build without extension
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* await aemPkg.buildRemotePkg('my-awesome-aem-website');
*/
async buildRemotePkg(pkgName, opts) {
const { pkgServiceUrl, auth } = this.getOptions(opts);
const pkgBuildUrl = `${pkgServiceUrl}?name=${pkgName}&cmd=build`;
const buildPkg = await got.post(pkgBuildUrl, {
auth
});
return buildPkg;
},
/**
* @private
* @param {String} packageName Name of the package without extension
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
*/
getRemotePkgStream(packageName, opts) {
const { pkgServiceUrl, auth } = this.getOptions(opts);
const pkgFileUrl = `${pkgServiceUrl}?name=${packageName}`;
const fileStream = got.stream(pkgFileUrl, {
auth
});
return fileStream;
},
/**
* @private
* @param {String} pkgName Name of the package without extension
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
*/
async getRemotePkgBuffer(pkgName, opts) {
await this.buildRemotePkg(pkgName, opts);
const fileStream = this.getRemotePkgStream(pkgName, opts);
return await getStream.buffer(fileStream);
},
/**
* @private
* @param {String} zipFile Path of the zip file to extract
* @param {String} extractPath Location path to extract the file
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
*/
async extractZip(zipFile, extractPath, opts) {
const { extractMetaDir, jcrRootDir } = this.getOptions(opts);
const zipExtractPath = extractPath || './';
const zip = new AdmZip(zipFile);
const zipEntries = zip.getEntries();
const extractFiles = zipEntries.filter(
({ entryName }) =>
extractMetaDir || entryName.split(/\//)[0] === jcrRootDir
);
const createPaths = extractFiles
.filter(({ entryName }) => /\/$/.test(entryName))
.map(({ entryName }) => {
const entryPath = path.join(zipExtractPath, entryName);
return pathExists(entryPath).then(
exists => exists || makeDir(entryPath)
);
});
await Promise.all(createPaths);
const createFiles = extractFiles
.filter(({ entryName }) => !/\/$/.test(entryName))
.map(({ entryName }) => {
return fsAsync.writeFile(
path.resolve(path.join(zipExtractPath, entryName)),
zip.readFile(entryName)
);
});
await Promise.all(createFiles);
},
/**
*
* @param {String} src Path of the package directory where need to pull the package.
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* await aemPkg.pull('./my-aem-pkg-dir/my-aem-website');
*/
async pull(src, opts) {
const { pkgPropFile, cwd } = this.getOptions(opts);
const packageName = await this.getPkgNameFromMeta(pkgPropFile);
const pkgSrc = path.resolve(cwd, src);
const zipBuffer = await this.getRemotePkgBuffer(packageName, opts);
await this.extractZip(zipBuffer, pkgSrc, opts);
},
/**
*
* @param {String} src Path of the package directory which you need to push to the server.
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* await aemPkg.push('./my-aem-pkg-dir/my-aem-website');
*/
async push(src, opts) {
const { pkgPropFile, cwd } = this.getOptions(opts);
const packageName = await this.getPkgNameFromMeta(pkgPropFile);
const pkgSrc = path.resolve(cwd, src);
opts.cwd = pkgSrc;
log('Packaging...');
const zip = new AdmZip();
zip.addLocalFolder(pkgSrc);
const buffer = await zip.toBuffer();
log('Uploading...');
const name = `${packageName}.zip`;
await this.uploadPkg({ buffer, name }, opts);
},
/**
*
* @param {String} pkgName Name of the package without extension
* @param {String} cloneDirPath Path of directory to clone the package
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* await aemPkg.clone('my-aem-website', './my-aem-pkg-dir/');
*/
async clone(pkgName, cloneDirPath, opts) {
const options = this.getOptions(opts);
const { cwd } = options;
options.cwd = path.resolve(cwd, cloneDirPath);
const pkgExtractPath = path.resolve(options.cwd, pkgName);
const dirExist = await pathExists(pkgExtractPath);
if (dirExist) {
return log('Error: Directory already exist');
}
log('Cloning package...');
options.extractMetaDir = true;
await makeDir(pkgExtractPath);
const zipBuffer = await this.getRemotePkgBuffer(pkgName, options);
await this.extractZip(zipBuffer, pkgExtractPath, options);
},
/**
* @private
* @param {String} msg Message before progress
* @param {Object} object Got Progress object
*/
showProgress(msg, { percent, total, transferred }) {
let progressMsg = '';
if (total) {
progressMsg = `${prettyBytes(transferred)}/${prettyBytes(
total
)} | ${Math.round(percent * 100)}%`;
} else if (transferred) {
progressMsg = `${prettyBytes(transferred)}`;
}
logUpdate(`${msg} | ${progressMsg}`);
},
/**
*
* @param {(String|Object)} file path or file url or object with buffer and filename properties
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* await aemPkg.uploadPkg('./my-aem-pkgs/my-website.zip');
* await aemPkg.uploadPkg('https://www.mywebsite.com/my-aem-pkgs/my-website.zip');
* await aemPkg.uploadPkg({buffer:zipFileBuffer, name:'my-website'});
*/
async uploadPkg(file, opts) {
const { pkgServiceUrl, auth, installPkg } = this.getOptions(opts);
const body = new FormData();
let downloadedTempDir;
if (typeof file === 'string') {
let filePath;
if (isUrl(file)) {
const { writePkgFile, tmpDir } = await this.downloadZipFile(file);
downloadedTempDir = tmpDir;
filePath = writePkgFile;
} else {
filePath = path.resolve(opts.cwd, file);
}
body.append('file', fs.createReadStream(filePath));
} else {
let filename = file.name;
body.append('file', file.buffer, { filename });
body.append('name', filename);
}
body.append('force', 'true');
body.append('install', installPkg ? 'true' : 'false');
let uploadFilename = typeof file === 'string' ? file : file.name;
uploadFilename = path.basename(uploadFilename);
await got
.post(pkgServiceUrl, {
auth,
body
})
.on('uploadProgress', progress => {
this.showProgress(`Uploading ${uploadFilename}`, progress);
if (progress.percent === 1) {
logUpdate.done();
logUpdate('Installing...');
logUpdate.done();
}
});
if (downloadedTempDir) {
await downloadedTempDir.cleanup();
}
},
/**
*
* @param {Array} pkgs array of package file paths
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* await aemPkg.uploadPkgs(['./my-aem-pkgs/my-first-website.zip', './my-aem-pkgs/my-second-website.zip', 'https://www.mywebsite.com/my-aem-pkgs/my-second-website.zip']);
*/
async uploadPkgs(pkgs, opts) {
await pkgs.reduce((p, pkg) => {
return p.then(() => this.uploadPkg(pkg, opts));
}, Promise.resolve());
},
/**
*
* @param {String} pkgsDir Directory of all package zip
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* // Upload all packages from this directory
* await aemPkg.uploadPkgsFromDir('./my-aem-pkgs/');
*/
async uploadPkgsFromDir(pkgsDir, opts) {
const options = this.getOptions(opts);
const cwd = path.resolve(options.cwd, pkgsDir);
const pkgs = await globby(options.pkgFilePattern, {
cwd
});
if (!pkgs.length) {
throw new Error(`Nothing found on ${cwd}`);
}
await this.uploadPkgs(pkgs, { ...options, cwd });
},
/**
*
* @param {String} zipFile Path of zip file which contains many packages. All will be uploaded individually.
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* // Upload packages from zip file which contains many AEM packages
* await aemPkg.uploadPkgsFromZip('./aem-pkgs/my-aem-pkgs.zip');
*/
async uploadPkgsFromZip(zipFile, opts) {
const options = this.getOptions(opts);
const { cwd } = options;
const zip = new AdmZip(path.resolve(cwd, zipFile));
const zipEntries = zip.getEntries();
const uploadPkgs = zipEntries
.filter(({ entryName }) => {
// filter directory and path.
return !/\/$/.test(entryName) && !/\//.test(entryName);
})
.map(({ entryName }) => {
return this.uploadPkg(
{
buffer: zip.readFile(entryName),
name: entryName
},
options
);
});
await Promise.all(uploadPkgs);
},
/**
* @private
* @param {String} zipUrl URL of the zip file
* @returns {Promise} Resolve to {writePkgFile, tmpDir} object
*/
async downloadZipFile(zipUrl) {
const tmpDir = await tmpPromise.dir({ unsafeCleanup: true });
const pkgFileName = 'aem-pkg-download-file.zip';
const writePkgFile = path.resolve(path.join(tmpDir.path, pkgFileName));
await new Promise((resolve, reject) => {
got
.stream(zipUrl)
.on('end', resolve)
.on('downloadProgress', progress => {
this.showProgress(`Downloading ${path.basename(zipUrl)}`, progress);
})
.on('error', reject)
.pipe(fs.createWriteStream(writePkgFile));
});
return { writePkgFile, tmpDir };
},
/**
*
* @param {String} zipUrl URL of zip file which contains AEM packages
* @param {Object} [opts=defaultOptions] Options to override default options
* @returns {Promise}
* @example
* // Upload packages from zip file URL which contain many AEM packages
* await aemPkg.uploadPkgsFromZip('https://www.example.com/packages/my-aem-pkgs.zip');
*/
async uploadPkgsFromZipUrl(zipUrl, opts) {
const options = this.getOptions(opts);
const { writePkgFile, tmpDir } = await this.downloadZipFile(zipUrl);
await this.uploadPkgsFromZip(writePkgFile, options);
await tmpDir.cleanup();
}
};
module.exports = exports = aemPkg;