initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

View file

View file

@ -0,0 +1,76 @@
"use strict";
var Dependency = function(options) {
if (!Dependency.configured) {
throw new Error("Unable to create a dependency without static variables configuration, use Dependency.configure first");
}
this._string = options.string;
this._text = options.text;
this._parse();
}
Dependency.configured = false;
Dependency.configure = function(libDir, target) {
this.libDir = libDir;
this.target = target;
var tPath = target.replace(libDir, "lib");
tPath = tPath.replace(".js", "");
tPath = tPath.split("/");
tPath.pop();
this.path = tPath;
this.configured = true;
};
Dependency.prototype.log = function() {
console.log("---------------------------------");
console.log("Found string: " + this._string);
console.log("Captured dependency: " + this._text);
console.log("Output dependency: " + this._name);
console.log("Lib is: " + Dependency.libDir);
console.log("Target is: " + Dependency.target);
console.log("---------------------------------");
}
Dependency.prototype._parse = function() {
var fl = this._text[0];
var dArr = this._text.split("/");
if (fl !== ".") {
this._name = "lib/" + this._text + "/index";
} else {
var summ = Dependency.path.slice();
this._name = "";
for (var i = 0; i < dArr.length; ++i) {
var hop = dArr[i];
switch (hop) {
case ".":
case "":
break;
case "..":
summ.pop();
break
default:
summ.push(hop);
break;
}
}
for (var j = 0; j < summ.length; ++j) {
if (j !== 0) {
this._name += "/"
}
this._name += summ[j];
}
}
}
Dependency.prototype.modify = function(line) {
return line.replace(this._text, this._name);
}
Dependency.prototype.toString = function() {
return this._name;
}
module.exports = Dependency;

View file

@ -0,0 +1,51 @@
"use strict";
var Dependency = require("./dependency");
var DepResolver = function(options) {
Dependency.configure(options.libDir, options.target)
this._reg1 = /(?:require\s*\((?:\"(.*)\"|\'(.*)\')\)[^;,]*(?:[;,]|$))(?!\s*\/\/not\sa\sd)/g;
this._reg2 = /(?:\"(.+)\"|\'(.+)\'),{0,1}(?=\s*\/\/resolve as d)/g;
}
DepResolver.prototype.resolve = function(lines) {
var regres;
var dep;
var header = [];
var dependencies = [];
dependencies.push("var defineArray = [];\n");
for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
var found = false;
while((regres = this._reg1.exec(line)) !== null) {
dep = new Dependency({
string: regres[0],
text: regres[1]
});
lines[i] = dep.modify(line);
dependencies.push("defineArray.push(\"" + dep.toString() + "\");");
found = true;
}
if (!found) {
while((regres = this._reg2.exec(line)) !== null) {
dep = new Dependency({
string: regres[0],
text: regres[1]
});
lines[i] = dep.modify(line);
}
}
}
header.push("define(moduleName, defineArray, function() {");
return {
header: header,
dependencies: dependencies,
bottom: "});"
}
};
module.exports = DepResolver;

75
libjs/polymorph/index.js Normal file
View file

@ -0,0 +1,75 @@
"use strict";
var fs = require("fs");
var DepResolver = require("./depresolver");
var pathCheck = require("./pathCheck");
var path = process.argv[2];
var target = process.argv[3];
var moduleName = process.argv[4];
var env = process.argv[5];
var libDir = process.argv[6];
var isNode = true;
if (env === "browser") {
isNode = false;
}
var dr = new DepResolver({
target: target,
libDir: libDir
});
fs.readFile(path, function(err, buffer) {
if (err) {
throw err;
}
console._stdout.write(String.fromCharCode(27) + "[1;32m");
console._stdout.write("polymorph: ");
console._stdout.write(String.fromCharCode(27) + "[0m");
console._stdout.write("parsing " + moduleName + " for " + env);
console._stdout.write("\n");
var file = buffer.toString();
var output = "";
if (!isNode) {
file = file.replace(/module.exports[\s]*=/g, "return");
file = file.replace(/\"use\sstrict\";/g, "");
var lines = file.split("\n");
output = "\"use strict\";\n" +
"(function(global) {\n" +
" var moduleName = \""+moduleName+"\"\n";
var add = dr.resolve(lines);
for (var i = 0; i < add.dependencies.length; ++i) {
output += " " + add.dependencies[i] + "\n";
}
output += "\n";
for (var i = 0; i < add.header.length; ++i) {
output += " " + add.header[i] + "\n";
}
for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
output += " " + line + "\n";
}
output += " " + add.bottom + "\n";
output += "})(window);";
} else {
output = file;
}
var p = target.split("/");
p[1] = "/" + p[1]
pathCheck(p.slice(1, -1), function(err) {
if (err) throw err;
fs.writeFile(target, output, function(err) {
if (err) throw err;
process.exit(0);
});
})
})

View file

@ -0,0 +1,34 @@
"use strict";
const fs = require("fs");
function pathCheck(/*Array*/path, cb) {
fs.access(path[0], function(err) {
if (err) {
if (err.code === 'ENOENT') {
fs.mkdir(path[0], function() {
if (path.length === 1) {
cb();
} else {
let nPath = path.slice();
let out = nPath.splice(1, 1);
nPath[0] += "/" + out[0];
pathCheck(nPath, cb);
}
})
} else {
cb(err);
}
} else {
if (path.length === 1) {
cb();
} else {
let nPath = path.slice();
let out = nPath.splice(1, 1);
nPath[0] += "/" + out[0];
pathCheck(nPath, cb);
}
}
})
}
module.exports = pathCheck;