mediacentral-feature-pack
Version:
A feature pack tool for mediacentral cloud-ux projects
131 lines (104 loc) • 5.73 kB
JavaScript
let readArgs = (() => {
var _ref = _asyncToGenerator(function* () {
return yargs.usage('Usage: ').alias('m', 'metadata').describe('metadata', 'path to a metadata file based on the feature pack definition').string('metadata').alias('h', 'helm').describe('helm', 'path to a helm chart file').string('helm').alias('d', 'docker').describe('docker', 'a docker image file with the contents of build').string('docker').alias('t', 'tag').describe('tag', 'a version tag').string('tag').alias('o', 'output').describe('output', 'an output directory').string('output').default('output', '.').demandOption(['metadata', 'helm', 'docker', 'tag']).help().argv;
});
return function readArgs() {
return _ref.apply(this, arguments);
};
})();
let validateFile = (() => {
var _ref2 = _asyncToGenerator(function* (options, name) {
logger.trace('validating file with options: ', options, ' with name ', name);
if (!options.hasOwnProperty(name)) throw new Error(`Path to a ${name} file is not set!`);
if (!name.match(/^([a-z0-9\-]{1,253})$/gm)) throw new Error("A name should be up to maximum length of 253 characters and consist of lower case alphanumeric characters and -");
if (typeof options[name] !== 'string') throw new Error(`${name} must be a string!`);
if (!(yield fs.existsPromise(options[name]))) throw new Error(`Path to a ${name} file (${options[name]}) is invalid.`);
if (!(yield fs.lstatPromise(options[name]).then(function (s) {
return s.isFile();
}))) throw new Error(`A (${name}) file (${options[name]}) musn't be a directory.`);
});
return function validateFile(_x, _x2) {
return _ref2.apply(this, arguments);
};
})();
let optionsValidation = (() => {
var _ref3 = _asyncToGenerator(function* (options) {
logger.trace('Options before validation: ', options);
if (!options.hasOwnProperty('tag')) throw new Error(`A version tag is not set!`);
if (typeof options.tag !== 'string') throw new Error(`Tag must be a string!`);
if (!options.hasOwnProperty('output')) throw new Error(`An output directory is not set!`);
if (typeof options.output !== 'string') throw new Error(`An output directory must be a string!`);
if (!(yield fs.existsPromise(options.output))) throw new Error(`Path to an output directory (${options.output}) is invalid.`);
if (!(yield fs.lstatPromise(options.output).then(function (s) {
return s.isDirectory();
}))) throw new Error(`An output musn't be a directory.`);
return Promise.all([validateFile(options, 'metadata'), validateFile(options, 'helm'), validateFile(options, 'docker')]);
});
return function optionsValidation(_x3) {
return _ref3.apply(this, arguments);
};
})();
let createCompressNames = (() => {
var _ref4 = _asyncToGenerator(function* (options) {
logger.info('----- Creating feature-packs -----');
const metadata = require(options.metadata);
const fileBz2 = path.join(options.output, `${metadata.name}.zip`);
if (yield fs.existsPromise(fileBz2)) throw new Error(`File ${fileBz2} already exists`);
do {
var fileTar = path.join(options.output, `${timestamp('YYYY-MM-DD-mm-ss')}.${randomstring.generate(3)}.tar`);
} while (yield fs.existsPromise(fileTar));
return {
fileTar,
fileBz2
};
});
return function createCompressNames(_x4) {
return _ref4.apply(this, arguments);
};
})();
let compress = (() => {
var _ref5 = _asyncToGenerator(function* (files, fileTar, fileBz2) {
const featurePackZip = archiver('zip');
const featurePackOutput = fs.createWriteStream(fileBz2);
return new Promise(function (resolve, reject) {
featurePackOutput.on('close', function () {
resolve();
});
featurePackZip.on('error', function (err) {
reject(err);
});
featurePackZip.pipe(featurePackOutput);
for (file of files) {
let fileName = path.basename(file);
featurePackZip.append(fs.createReadStream(file), { name: fileName });
}
featurePackZip.finalize();
});
});
return function compress(_x5, _x6, _x7) {
return _ref5.apply(this, arguments);
};
})();
let removeFile = (() => {
var _ref6 = _asyncToGenerator(function* (file) {
return (yield fs.existsPromise(file)) && (yield fs.unlinkPromise(file));
});
return function removeFile(_x8) {
return _ref6.apply(this, arguments);
};
})();
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const path = require('path'),
yargs = require('yargs'),
timestamp = require('time-stamp'),
randomstring = require("randomstring"),
archiver = require('archiver'),
logger = require.main.require('log4js').getLogger(`mediacentral-feature-pack ${__filename}`),
fs = require('./fs');
module.exports = {
removeFile,
compress,
createCompressNames,
readArgs,
optionsValidation
};