1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var fs = require("fs");
|
4 | var fsx = require("fs-extra");
|
5 | var path = require("path");
|
6 | var cmn = require("@akashic/akashic-cli-commons");
|
7 | var browserify = require("browserify");
|
8 | var readdir = require("fs-readdir-recursive");
|
9 | var gcu = require("./GameConfigurationUtil");
|
10 | var UglifyJS = require("uglify-js");
|
11 | var babel = require("@babel/core");
|
12 | var presetEnv = require("@babel/preset-env");
|
13 | function _completeConvertGameParameterObject(param) {
|
14 | param.bundle = !!param.bundle;
|
15 | param.babel = !!param.babel;
|
16 | param.minify = !!param.minify;
|
17 | param.strip = !!param.strip;
|
18 | param.source = param.source || process.cwd();
|
19 | param.hashLength = param.hashLength || 0;
|
20 | param.logger = param.logger || new cmn.ConsoleLogger();
|
21 | param.omitEmptyJs = !!param.omitEmptyJs;
|
22 | param.exportInfo = param.exportInfo;
|
23 | }
|
24 | exports._completeConvertGameParameterObject = _completeConvertGameParameterObject;
|
25 | function bundleScripts(entryPoint, basedir) {
|
26 | var b = browserify({
|
27 | entries: entryPoint,
|
28 | basedir: basedir,
|
29 | builtins: false,
|
30 | standalone: "aez_bundle_main"
|
31 | });
|
32 | b.external("g");
|
33 | var filePaths = [];
|
34 | b.on("dep", function (row) {
|
35 | filePaths.push(cmn.Util.makeUnixPath(path.relative(basedir, row.file)));
|
36 | });
|
37 | return new Promise(function (resolve, reject) {
|
38 | b.bundle(function (err, buf) {
|
39 | if (err)
|
40 | return reject(err);
|
41 | resolve({ bundle: buf.toString(), filePaths: filePaths });
|
42 | });
|
43 | });
|
44 | }
|
45 | exports.bundleScripts = bundleScripts;
|
46 | function convertGame(param) {
|
47 | _completeConvertGameParameterObject(param);
|
48 | var gamejson;
|
49 | cmn.Util.mkdirpSync(path.dirname(path.resolve(param.dest)));
|
50 | return Promise.resolve()
|
51 | .then(function () { return cmn.ConfigurationFile.read(path.join(param.source, "game.json"), param.logger); })
|
52 | .then(function (result) {
|
53 | gamejson = result;
|
54 |
|
55 | if (param.exportInfo) {
|
56 | gamejson.exportZipInfo = {
|
57 | version: param.exportInfo.version,
|
58 | option: param.exportInfo.option
|
59 | };
|
60 | }
|
61 |
|
62 | var errorMessages = [];
|
63 | gcu.extractScriptAssetFilePaths(gamejson).forEach(function (filePath) {
|
64 | var code = fs.readFileSync(path.resolve(param.source, filePath)).toString();
|
65 | if (!param.babel) {
|
66 | errorMessages = errorMessages.concat(cmn.LintUtil.validateEs5Code(code).map(function (info) { return filePath + "(" + info.line + ":" + info.column + "): " + info.message; }));
|
67 | }
|
68 | });
|
69 | if (errorMessages.length > 0) {
|
70 | param.logger.warn("Non-ES5 syntax found.\n" + errorMessages.join("\n"));
|
71 | }
|
72 | if (!param.bundle)
|
73 | return null;
|
74 | return bundleScripts(gamejson.main || gamejson.assets.mainScene.path, param.source);
|
75 | })
|
76 | .then(function (bundleResult) {
|
77 | var noCopyingFilePaths = new Set();
|
78 | if (bundleResult) {
|
79 | gcu.removeScriptFromFilePaths(gamejson, bundleResult.filePaths);
|
80 | noCopyingFilePaths = new Set(bundleResult.filePaths);
|
81 | }
|
82 | var babelOption = {
|
83 | presets: [
|
84 | babel.createConfigItem([presetEnv, {
|
85 | modules: false,
|
86 | targets: {
|
87 | "ie": 10
|
88 | }
|
89 | }], { type: "preset" })
|
90 | ]
|
91 | };
|
92 | var files = param.strip ? gcu.extractFilePaths(gamejson, param.source) : readdir(param.source).map(function (p) { return p.replace(/\\/g, "/"); });
|
93 | files.forEach(function (p) {
|
94 | if (!noCopyingFilePaths.has(p)) {
|
95 | cmn.Util.mkdirpSync(path.dirname(path.resolve(param.dest, p)));
|
96 | var buff = fs.readFileSync(path.resolve(param.source, p));
|
97 | if (param.omitEmptyJs && gcu.isScriptJsFile(p) && gcu.isEmptyScriptJs(buff.toString().trim())) {
|
98 | Object.keys(gamejson.assets).some(function (key) {
|
99 | if (gamejson.assets[key].type === "script" && gamejson.assets[key].path === p) {
|
100 | gamejson.assets[key].global = false;
|
101 | return true;
|
102 | }
|
103 | return false;
|
104 | });
|
105 | }
|
106 | var value = (param.babel && gcu.isScriptJsFile(p)) ? babel.transform(buff.toString().trim(), babelOption).code : buff;
|
107 | fs.writeFileSync(path.resolve(param.dest, p), value);
|
108 | }
|
109 | });
|
110 | if (bundleResult === null) {
|
111 | return;
|
112 | }
|
113 | var entryPointPath;
|
114 | if (!!gamejson.main) {
|
115 | entryPointPath = gcu.addScriptAsset(gamejson, "aez_bundle_main");
|
116 | gamejson.main = "./" + entryPointPath;
|
117 | }
|
118 | else {
|
119 | entryPointPath = gcu.makeUniqueAssetPath(gamejson, "script/mainScene.js");
|
120 | gamejson.assets["mainScene"] = {
|
121 | type: "script",
|
122 | global: true,
|
123 | path: entryPointPath
|
124 | };
|
125 | }
|
126 | var entryPointAbsPath = path.resolve(param.dest, entryPointPath);
|
127 | cmn.Util.mkdirpSync(path.dirname(entryPointAbsPath));
|
128 | var code = param.babel ? babel.transform(bundleResult.bundle, babelOption).code : bundleResult.bundle;
|
129 | fs.writeFileSync(entryPointAbsPath, code);
|
130 | })
|
131 | .then(function () {
|
132 | if (param.hashLength > 0) {
|
133 | var hashLength = Math.ceil(param.hashLength);
|
134 | try {
|
135 | cmn.Renamer.renameAssetFilenames(gamejson, param.dest, hashLength);
|
136 | }
|
137 | catch (error) {
|
138 |
|
139 | fsx.removeSync(path.resolve(param.dest));
|
140 | if (error.message === cmn.Renamer.ERROR_FILENAME_CONFLICT) {
|
141 | throw new Error("Hashed filename conflict. Use larger hash-filename param on command line.");
|
142 | }
|
143 | throw error;
|
144 | }
|
145 | }
|
146 | return cmn.ConfigurationFile.write(gamejson, path.resolve(param.dest, "game.json"), param.logger);
|
147 | })
|
148 | .then(function () {
|
149 | if (!param.minify)
|
150 | return;
|
151 | var scriptAssetPaths = gcu.extractScriptAssetFilePaths(gamejson).map(function (p) { return path.resolve(param.dest, p); });
|
152 | scriptAssetPaths.forEach(function (p) {
|
153 | fs.writeFileSync(p, UglifyJS.minify(p).code);
|
154 | });
|
155 | });
|
156 | }
|
157 | exports.convertGame = convertGame;
|