Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk johnsoncodehk/gscc.js
Last active Jun 6, 2019

Embed
What would you like to do?
GameSparks Cloud Code - (CommonJS)TypeScript to Cloud Code
/**
* Obsolete! Use https://github.com/johnsoncodehk/ts2gamesparks
*/
var path = require('path'), fs = require('fs');
var outDir = "./dist/";
function findAllTsFiles(startPath, paths = []) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return;
}
var files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
findAllTsFiles(filename, paths); //recurse
}
else if (filename.indexOf(".ts") >= 0 && filename.indexOf(".d.ts") == -1) {
paths.push(filename);
};
};
return paths;
}
function getModuleFuncHead(moduleFile) {
return "_" + moduleFile + "_";
}
var filePaths = findAllTsFiles("./");
// console.log(filePaths);
filePaths.forEach(function (filePath) {
filePath = outDir + filePath.replace(".ts", ".js");
var filename = filePath.replace(/^.*[\\\/]/, '').replace(".js", "");
var file = fs.readFileSync(filePath, { encoding: "utf8" });
var lines = file.split("\n");
var removeLines = [];
var replaceStrings = {};
// 刪除所有注釋
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var tab = " ";
while (line.indexOf(tab) == 0) {
line = line.replace(tab, "");
}
if (line.indexOf("//") == 0) {
removeLines.push(lines[i]);
lines[i] = "";
}
}
// 找到所有exports
var exports = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf("exports.") == 0) {
exports.push(line.replace(" ", ".").split(".")[1]);
}
}
// console.log(exports);
// 刪除所有exports
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf("exports") >= 0) {
removeLines.push(line);
line = "";
}
lines[i] = line;
}
var requires = {};
// 找到所有require
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf(" = require(") >= 0) {
var moduleFile = line.split('"')[1];
var moduleVar = line.split(" ")[1];
requires[moduleVar] = moduleFile;
line = line.split(" ")[3];
replaceStrings[lines[i]] = line;
}
lines[i] = line;
}
// console.log(requires);
var newFile = lines.join("\n");
// replace所有import Module
for (var moduleVar in requires) {
var moduleFile = requires[moduleVar];
var findStr = moduleVar + ".";
var replaceStr = getModuleFuncHead(moduleFile);
while (newFile.indexOf(findStr) >= 0) {
replaceStrings[findStr] = replaceStr;
newFile = newFile.replace(findStr, replaceStr);
}
}
// replace所有exports
for (var i = 0; i < exports.length; i++) {
var expo = exports[i];
var findStr = "function " + expo;
var replaceStr = "function " + getModuleFuncHead(filename) + expo;
while (newFile.indexOf(findStr) >= 0) {
replaceStrings[findStr] = replaceStr;
newFile = newFile.replace(findStr, replaceStr);
}
}
// replace所有換行
while (newFile.indexOf("\n\n") >= 0) {
newFile = newFile.replace("\n\n", "\n");
}
console.log({
filePath: filePath,
exports: exports,
requires: requires,
removeLines: removeLines,
replaceStrings: replaceStrings,
});
fs.writeFileSync(filePath, newFile);
});
@johnsoncodehk

This comment has been minimized.

Copy link
Owner Author

commented Sep 26, 2017

  1. Download to your Cloud Code TypeScript project.
  2. Configure the "outDir" variable. (same with tsconfig.josn)
  3. Running "tsc; node gscc" to build code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.