@criticalmanufacturing/node-package-bundler
Version:
Connect IoT Package Bundler
131 lines • 6.98 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transpiler = void 0;
const log_1 = require("./log");
const io = require("fs-extra");
const path = require("path");
const inversify_1 = require("inversify");
const types_1 = require("../types");
const core_1 = require("@swc/core");
let Transpiler = class Transpiler {
preProcessTaskScripts(templateDirectory, value) {
return __awaiter(this, void 0, void 0, function* () {
if (value == null) {
return value;
}
const isMatchScriptFunc = (valueToCheck) => {
const regex = /\${script\((.*)\)}/i;
const matches = valueToCheck.match(regex);
if (matches != null && matches.length === 2) {
return { isMatch: true, matchedValue: matches[1] };
}
return { isMatch: false, matchedValue: "" };
};
const isMatchScriptArray = (valueToCheck) => {
const regex = /\${script\[\]\((.*)\)}/i;
const matches = valueToCheck.match(regex);
if (matches != null && matches.length === 2) {
return { isMatch: true, matchedValue: matches[1] };
}
return { isMatch: false, matchedValue: "" };
};
const readAndTranspile = (scriptType, pathMatch) => __awaiter(this, void 0, void 0, function* () {
this._logger.debug(` [Templates] Processing Script as ${scriptType} '${pathMatch}'`);
const scriptFile = path.resolve(templateDirectory, pathMatch);
const scriptContent = io.readFileSync(scriptFile).toString();
const regexTokenMatcherForPackagePacker = /\/\/\s+PackagePacker:\s+Start\s+of\s+Script([\s\S]*?)\/\/\s+PackagePacker:\s+End\s+of\s+Script/i;
const regexTokenMatcherForAsyncPackagePacker = /\/\/\s+PackagePacker:\s+Start\s+of\s+Async\s+Script([\s\S]*?)\/\/\s+PackagePacker:\s+End\s+of\s+Async\s+Script/i;
const matches = scriptContent.match(regexTokenMatcherForPackagePacker);
const matchesAsync = scriptContent.match(regexTokenMatcherForAsyncPackagePacker);
if (matches != null && matches.length === 2) {
// Check for hooks
// Start Hook -> // PackagePacker: Start of Script
// End Hook -> // PackagePacker: End of Script
return yield this.transpile(matches[1].trim(), false);
}
else if (matchesAsync != null && matchesAsync.length === 2) {
// Check for hooks and Add start '(async () => {' declaration and end '})();'
// Start Hook -> // PackagePacker: Start of Async Script
// End Hook -> // PackagePacker: End of Async Script
const script = "(async () => {\r\n" + matchesAsync[1].trim() + "\r\n})();";
return yield this.transpile(script, false);
}
else {
return yield this.transpile(scriptContent, false);
}
});
const isArray = (valueToCheck) => typeof (valueToCheck) === "object" && Array.isArray(valueToCheck);
const isObject = (valueToCheck) => typeof (valueToCheck) === "object" && !Array.isArray(valueToCheck);
const isString = (valueToCheck) => typeof (valueToCheck) === "string";
if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = yield this.preProcessTaskScripts(templateDirectory, value[i]);
}
}
else if (isObject(value)) {
const keys = Object.keys(value);
for (const key of keys) {
value[key] = yield this.preProcessTaskScripts(templateDirectory, value[key]);
}
}
else if (isString(value)) {
const isMatchFunc = isMatchScriptFunc(value);
const isMatchArray = isMatchScriptArray(value);
if (isMatchFunc.isMatch) {
const transpiled = yield readAndTranspile("()", isMatchFunc.matchedValue);
value = Buffer.from(transpiled).toString("base64");
}
else if (isMatchArray.isMatch) {
const transpiled = yield readAndTranspile("[]", isMatchArray.matchedValue);
value = transpiled.split("\n");
}
}
return value;
});
}
transpile(code, compress) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield (0, core_1.transform)(code, {
jsc: {
parser: {
syntax: "typescript",
},
transform: {},
target: "es2016",
minify: {
compress: compress,
}
},
minify: compress,
});
return (res.code);
});
}
};
exports.Transpiler = Transpiler;
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Logger),
__metadata("design:type", log_1.Log)
], Transpiler.prototype, "_logger", void 0);
exports.Transpiler = Transpiler = __decorate([
(0, inversify_1.injectable)()
], Transpiler);
//# sourceMappingURL=transpiler.js.map