UNPKG

cocos-html-pack-3d

Version:

Single html web mobile template for Cocos verions 2.x

203 lines (202 loc) 11.8 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var fs = __importStar(require("fs")); var path = __importStar(require("path")); var process_1 = require("process"); var commander_1 = require("commander"); var Packer_1 = require("./Packer"); var Reader_1 = require("./Reader"); var Util_1 = require("./Util"); var kOverrideTemplatesDir = 'pack-templates'; var _adSize = [{ 'width': 300, 'height': 250 }, { 'width': 480, 'height': 320 }, { 'width': 320, 'height': 480 }]; var _vendor = ['ironsource', 'vungle', 'adword', 'unity', 'facebook', 'applovin']; function main(templates, input, output, title, orientation, minify, tinify, compress) { var _a, _b, _d, _e; return __awaiter(this, void 0, void 0, function () { var platform, useOverrideTemplate, templateDir, filenames, _i, filenames_1, name_1, filename; return __generator(this, function (_f) { switch (_f.label) { case 0: if (templates) { console.log("Copy templates to '" + kOverrideTemplatesDir + "'"); if (fs.existsSync(kOverrideTemplatesDir)) { fs.rmdirSync(kOverrideTemplatesDir, { recursive: true }); } fs.mkdirSync(kOverrideTemplatesDir); Util_1.Util.copyDirSync(path.join((_b = (_a = require.main) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : '', '..', kOverrideTemplatesDir), kOverrideTemplatesDir); process_1.exit(0); } if (!input) { console.error('missing input dir'); process_1.exit(1); } if (!output) { console.error('missing output dir'); process_1.exit(1); } if (input === output) { console.error('output dir must be different input dir'); process_1.exit(1); } // Copy all from input console.log('Pack', input, '->', output); if (fs.existsSync(output)) { fs.rmdirSync(output, { recursive: true }); } fs.mkdirSync(output, { recursive: true }); Util_1.Util.copyDirSync(input, output); // Check override templates console.log('Copy templates'); platform = path.basename(input); useOverrideTemplate = fs.existsSync(path.join(kOverrideTemplatesDir, platform)); if (useOverrideTemplate) { console.log(' use override templates'); } else { console.log(' use built-in template'); } templateDir = useOverrideTemplate ? path.join(kOverrideTemplatesDir, platform) : path.join((_e = (_d = require.main) === null || _d === void 0 ? void 0 : _d.path) !== null && _e !== void 0 ? _e : '', '..', kOverrideTemplatesDir, platform); Util_1.Util.copyFilesSync(templateDir, output); if (!tinify) return [3 /*break*/, 2]; console.log('Compress images with tinypng'); return [4 /*yield*/, Util_1.Util.useTinify(tinify, output)]; case 1: _f.sent(); _f.label = 2; case 2: console.log('Pack'); _vendor.map(function (val, index) { var reader = new Reader_1.Reader(platform); var data = reader.readAll(output, val); var packer = new Packer_1.Packer(data); if (val == 'ironsource') { var htmlIronsource = packer.patch(title, val, '', orientation, compress); Util_1.Util.write(path.join(output, 'ironsource.html'), htmlIronsource); } else if (val == 'vungle') { var htmlVungle = packer.patch(title, val, '', orientation, compress); Util_1.Util.write(path.join(output, 'vungle.html'), htmlVungle); } else if (val == 'adword') { _adSize.map(function (item, ind) { var htmlAdword = packer.patch(title, val, JSON.stringify(item), orientation, compress); Util_1.Util.write(path.join(output, 'index.html'), htmlAdword); fs.readFile(path.join(output, 'index.html'), function (err, data) { if (err) throw err; var content = data; var _c = ''; _c += content; Util_1.Util.zipFile('index.html', _c, 'adword_' + item.width + '_' + item.height + '.zip', output); }); }); } else { var htmlNormal = packer.patch(title, val, '', orientation, compress); Util_1.Util.write(path.join(output, val + '.html'), htmlNormal); } }); if (!minify) return [3 /*break*/, 4]; console.log('Compress html with minify'); return [4 /*yield*/, Util_1.Util.useMinify(path.join(output, 'index.html'))]; case 3: _f.sent(); _f.label = 4; case 4: // Remove unused files console.log('Remove unused files'); filenames = fs.readdirSync(output); for (_i = 0, filenames_1 = filenames; _i < filenames_1.length; _i++) { name_1 = filenames_1[_i]; if (name_1 === 'index.html' || name_1 == 'ironsource.html' || name_1 == 'vungle.html' || name_1 == 'applovin.html' || name_1 == 'unity.html' || name_1 == 'facebook.html' || name_1 == 'adword_300_250.zip' || name_1 == 'adword_320_480.zip' || name_1 == 'adword_480_320.zip') { continue; } filename = path.join(output, name_1); if (fs.statSync(filename).isFile()) { fs.unlinkSync(filename); } else { fs.rmdirSync(filename, { recursive: true }); } } console.log('File size:', fs.statSync(path.join(output, 'index.html')).size, 'bytes'); return [2 /*return*/]; } }); }); } var program = new commander_1.Command(); program .name('cocos-html-pack-3d ') .description('Single html web mobile template for Cocos') .version('1.1.12') .option('--templates', "override template dir in '" + kOverrideTemplatesDir + "'") .option('-i, --input <path>', 'input dir, build/web-mobile for example') .option('-o, --output <path>', 'output dir') .option('-t, --title <value>', 'page title if use the default template') .option('--orientation <value>', 'portrait or landscape', 'portrait') .option('--minify', 'compress js, css and html') .option('--tinify <key>', 'compress and optimize JPEG and PNG images') .option('--compress', 'compress assets and js'); program.parse(process.argv); var _a = program.opts(), templates = _a.templates, input = _a.input, output = _a.output, title = _a.title, orientation = _a.orientation, minify = _a.minify, tinify = _a.tinify, compress = _a.compress; main(templates, input, output, title, orientation, minify, tinify, compress);