UNPKG

easy-cli2

Version:

一个简易的脚手架,提供eslint格式化检查,创建项目模板,自动部署功能,xlsx与json互转,压缩图片等功能

194 lines (193 loc) 8.82 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.minifyCommad = exports.fileDisplay = void 0; var minify_1 = require("../../util/minify/minify"); var try_to_catch_1 = __importDefault(require("try-to-catch")); var tmp_1 = __importDefault(require("tmp")); var fs_extra_1 = __importDefault(require("fs-extra")); var path_1 = __importDefault(require("path")); var util_1 = require("../../util"); var chalk_1 = __importDefault(require("chalk")); var hidefile = require("hidefile"); var minifyOptions = { html: { collapseBooleanAttributes: true, includeAutoGeneratedTags: true, removeAttributeQuotes: true, removeComments: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, sortClassName: true, useShortDoctype: true, collapseWhitespace: true, }, css: { compatibility: "*", }, js: { ecma: 5, }, }; // 遍历文件夹 var fileDisplay = function (filePath, fileProcess, ingores) { return __awaiter(void 0, void 0, void 0, function () { var fileNames, i, filename, fileDir, stats, isFile, isDir; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, fs_extra_1.default.readdir(filePath)]; case 1: fileNames = _a.sent(); i = 0; _a.label = 2; case 2: if (!(i < fileNames.length)) return [3 /*break*/, 7]; filename = fileNames[i]; fileDir = path_1.default.join(filePath, filename); stats = fs_extra_1.default.statSync(fileDir); isFile = stats.isFile(); isDir = stats.isDirectory(); if (!hidefile.isHiddenSync(fileDir)) return [3 /*break*/, 3]; fs_extra_1.default.removeSync(fileDir); return [3 /*break*/, 6]; case 3: if (!isFile) return [3 /*break*/, 5]; if (!(typeof fileProcess === "function")) return [3 /*break*/, 5]; return [4 /*yield*/, fileProcess(fileDir)]; case 4: _a.sent(); _a.label = 5; case 5: if (isDir) { if (!(ingores === null || ingores === void 0 ? void 0 : ingores.includes(filename))) { //是文件夹,继续递归 (0, exports.fileDisplay)(fileDir, fileProcess); } } _a.label = 6; case 6: i++; return [3 /*break*/, 2]; case 7: return [2 /*return*/]; } }); }); }; exports.fileDisplay = fileDisplay; var fileProcess = function (file, output) { return __awaiter(void 0, void 0, void 0, function () { var _a, error, data, error_1; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, (0, try_to_catch_1.default)(minify_1.minify, file, minifyOptions)]; case 1: _a = _b.sent(), error = _a[0], data = _a[1]; if (!error) return [3 /*break*/, 2]; console.log(chalk_1.default.red(error)); return [3 /*break*/, 5]; case 2: _b.trys.push([2, 4, , 5]); return [4 /*yield*/, fs_extra_1.default.writeFile(output ? output : file, data)]; case 3: _b.sent(); return [3 /*break*/, 5]; case 4: error_1 = _b.sent(); console.log(chalk_1.default.red(error_1)); return [3 /*break*/, 5]; case 5: return [2 /*return*/]; } }); }); }; var Minify = function (file, options) { if (file === void 0) { file = ""; } return __awaiter(void 0, void 0, void 0, function () { var all, _a, destination, output, ingores, outputDir, tmpobj, _b, name_1, ext; return __generator(this, function (_c) { all = options.all, _a = options.destination, destination = _a === void 0 ? "dist" : _a, output = options.output; ingores = [ "img", "imgs", "image", "images", "node_modules", "less", "scss", "sass", ]; outputDir = path_1.default.resolve((0, util_1.getCwdPath)(), destination); if (fs_extra_1.default.pathExistsSync(outputDir)) { fs_extra_1.default.removeSync(outputDir); } if (all) { tmpobj = tmp_1.default.dirSync(); (0, util_1.cpSync)((0, util_1.getCwdPath)(), tmpobj.name); fs_extra_1.default.mkdirSync(outputDir); (0, util_1.cpSync)(tmpobj.name, outputDir); fs_extra_1.default.emptyDirSync(tmpobj.name); tmpobj.removeCallback(); (0, exports.fileDisplay)(outputDir, fileProcess, ingores); } else if (file) { _b = path_1.default.parse(file), name_1 = _b.name, ext = _b.ext; fileProcess(path_1.default.resolve((0, util_1.getCwdPath)(), file), path_1.default.resolve((0, util_1.getCwdPath)(), output ? output : "".concat(name_1, ".min").concat(ext))); } return [2 /*return*/]; }); }); }; exports.minifyCommad = { description: "minify js,css and html", command: "minify [file]", options: [ { command: "-a --all", description: "minify all js,css and html", }, { command: "-d --destination <dir>", description: "destination dir", }, { command: "-o --output <outputName>", description: "output name", }, ], action: Minify, };