@codingtools/cdt
Version:
CLI for Developers
106 lines • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const MinifyJs = require('minify');
const logger_1 = tslib_1.__importDefault(require("../utilities/logger"));
const utilities_1 = tslib_1.__importDefault(require("../utilities/utilities"));
class Minify extends command_1.Command {
// required FILE
async run() {
const { args, flags } = this.parse(Minify);
args.file = this.getFilePath(flags, args);
args.string = this.getFileString(args);
flags.type = this.getFileType(flags, args);
logger_1.default.info(this, `reading file: ${args.file}`);
logger_1.default.info(this, `file type: ${flags.type}`);
this.checkParameters(flags, args);
await this.minifyString(flags, args);
}
async evalJs(args) {
let output = '';
await MinifyJs.js(args)
.then((val) => output = val); // cdt-168, Minify upgrade not return Promise Object for JS
return output;
}
// this will get file type either from flags.type or file extension ( if flag is not given)
getFileType(flags, args) {
if (flags.type) //if type is given
return this.getFileTypeFromExtension(flags.type);
let fileExtensionRegex = /\w+\.([a-z]+)/;
let extension = args.file.match(fileExtensionRegex)[1];
return this.getFileTypeFromExtension(extension);
}
getFileTypeFromExtension(extension) {
switch (extension.toUpperCase()) {
case 'JS':
case 'JAVASCRIPT':
return Minify.JS;
case 'CSS':
return Minify.CSS;
case 'HTML':
case 'HTM':
return Minify.HTML;
default:
logger_1.default.error(this, 'Invalid File Type');
}
}
// get filepath from args or flags ( if args not have file name )
// this will get file path into args.file either from args or flags
getFileString(args) {
return utilities_1.default.getStringFromFile(this, args.file);
}
getFilePath(flags, args) {
if (args.file)
return args.file;
if (flags.file)
return flags.file;
logger_1.default.error(this, 'File path not passed');
}
// tslint:disable-next-line:no-unused
checkParameters(flags, args) {
if (args.file === undefined || args.file === '')
logger_1.default.error(this, 'File path is empty');
// others already checked
}
//TODO: add image compression also
//TODO: add error handling also
async minifyString(flags, args) {
logger_1.default.progressStart(this, 'minifying...');
let output = '';
// setTimeout(() => { //TODO: can add spinner for bigger files using promise
switch (flags.type) {
case Minify.JS:
output = await this.evalJs(args.string);
break;
case Minify.CSS:
output = MinifyJs.css(args.string);
break;
case Minify.HTML:
output = MinifyJs.html(args.string);
break;
default:
logger_1.default.error(this, 'Invalid Minifier Type');
}
logger_1.default.progressStop(this, `file: ${args.file} minified`);
// }, 1000)
if (flags.output) { // if output path is provided then write to file also
utilities_1.default.writeStringToFile(this, flags.output, output);
}
else
logger_1.default.success(this, `minified output: \n${output}`);
}
}
exports.default = Minify;
Minify.description = 'File Minifier';
Minify.flags = {
help: command_1.flags.help({ char: 'h' }),
type: command_1.flags.string({ char: 't', description: 'type of file to be minified, it will try to find type with extension supported: JS, HTML/HTM, CSS' }),
file: command_1.flags.string({ char: 'f', description: 'file to be minified' }),
output: command_1.flags.string({ char: 'o', description: 'output file path' }),
};
Minify.args = [{ name: 'file' }];
Minify.JS = 'JS';
Minify.HTML = 'HTML';
Minify.CSS = 'CSS';
//# sourceMappingURL=minify.js.map