UNPKG

sardines-compile-time-tools

Version:

sardines.compile-time-tools.js is part of the sardines.io project

255 lines (254 loc) 15.1 kB
"use strict"; 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 }); exports.compile = void 0; var fs = require("fs"); var path = require("path"); var parser_1 = require("./parser"); var transformer_1 = require("./transformer"); exports.compile = function (compilerSettings, targetFiles) { return __awaiter(void 0, void 0, void 0, function () { var processedFiles, sardineExtName, processFile, service_definition_file_content, sardineServices, _i, _a, s, name_1; return __generator(this, function (_b) { processedFiles = {}; sardineExtName = ".sardine.ts"; processFile = function (targetFilePath) { return __awaiter(void 0, void 0, void 0, function () { var services, filePath, error, _i, _a, item, subFilePath, subjobResult, dir, baseName, extName, fileName, sardineFileName, sardineBaseName, sardineFilePath, intermediateFilePath, sourceFilePath, _b, identifiers, referencedTypes, importedIds, proxyIds, appName, err_1; return __generator(this, function (_c) { switch (_c.label) { case 0: services = []; filePath = targetFilePath; error = null; if (processedFiles[filePath]) return [2, { services: services, error: error, filePath: filePath }]; if (!fs.lstatSync(filePath).isDirectory()) return [3, 5]; _i = 0, _a = fs.readdirSync(filePath); _c.label = 1; case 1: if (!(_i < _a.length)) return [3, 4]; item = _a[_i]; subFilePath = path.join(filePath, "./" + item); return [4, processFile(subFilePath)]; case 2: subjobResult = _c.sent(); if (subjobResult.error) { if (compilerSettings.validate || compilerSettings.only_validate) { error = subjobResult.error; filePath = subjobResult.filePath; return [3, 4]; } if (compilerSettings.verbose) { console.error("ERROR for file " + subjobResult.filePath + ":", subjobResult.error, '\n'); } } else { Array.prototype.push.apply(services, subjobResult.services); } _c.label = 3; case 3: _i++; return [3, 1]; case 4: processedFiles[filePath] = true; return [2, { services: services, error: error, filePath: filePath }]; case 5: if (!fs.lstatSync(filePath).isFile()) return [2, { services: services, error: error, filePath: filePath }]; dir = path.dirname(filePath); baseName = path.basename(filePath); extName = path.extname(baseName); if (extName.toLowerCase() !== '.ts') { error = "unsupported file type '" + extName + "' for file " + filePath; return [2, { services: services, error: error, filePath: filePath }]; } fileName = '', sardineFileName = '', sardineBaseName = '', sardineFilePath = '', intermediateFilePath = ''; if (baseName.indexOf(sardineExtName) < 0) { fileName = path.basename(baseName, extName); sardineBaseName = "" + fileName + sardineExtName; sardineFilePath = dir + "/" + sardineBaseName; } else { sardineFilePath = filePath; sardineBaseName = baseName; fileName = path.basename(baseName, sardineExtName); filePath = dir + "/" + fileName + extName; } sardineFileName = path.basename(sardineBaseName, extName); intermediateFilePath = dir + "/" + sardineFileName + ".tmp"; if (!compilerSettings.recompile && !compilerSettings.reverse) { if (fs.existsSync(sardineFilePath)) return [2, { services: services, error: error, filePath: filePath }]; } else if (fs.existsSync(sardineFilePath)) { if (compilerSettings.verbose) { console.log("restoring source file " + filePath + " from sardine file " + sardineFilePath); } fs.renameSync(sardineFilePath, filePath); } if (compilerSettings.reverse) return [2, { services: services, error: error, filePath: filePath }]; if (targetFilePath === sardineFilePath) return [2, { services: services, error: error, filePath: filePath }]; sourceFilePath = filePath; if (processedFiles[filePath]) return [2, { services: services, error: error, filePath: filePath }]; processedFiles[filePath] = true; if (compilerSettings.verbose) { console.log("processing file: " + sourceFilePath); } _c.label = 6; case 6: _c.trys.push([6, 8, 9, 10]); _b = parser_1.gatherExports(sourceFilePath), identifiers = _b[0], referencedTypes = _b[1], importedIds = _b[2], proxyIds = _b[3]; appName = compilerSettings.application; return [4, transformer_1.transform(appName, fileName, sardineFileName, sourceFilePath, identifiers, referencedTypes, importedIds, proxyIds, function (line, lineIndex) { if (lineIndex === 0) { if (!compilerSettings.only_validate) { fs.writeFileSync(intermediateFilePath, line); } if (compilerSettings.print) { console.log(line); } } else if (line && !compilerSettings.only_validate) { if (compilerSettings.print) { console.log(line); } fs.appendFileSync(intermediateFilePath, line); } })]; case 7: services = _c.sent(); if (!compilerSettings.only_validate && fs.existsSync(intermediateFilePath)) { if (compilerSettings.verbose) { console.log("renaming source file " + filePath + " to sardine file " + sardineFilePath); } fs.renameSync(filePath, sardineFilePath); if (compilerSettings.verbose) { console.log("moving intermediate file " + intermediateFilePath + " to replace source file " + filePath); } fs.renameSync(intermediateFilePath, filePath); } if (compilerSettings.verbose || compilerSettings.only_validate) { console.log("successfully processed source file: " + sourceFilePath + "\n"); } return [2, { services: services, error: error, filePath: filePath }]; case 8: err_1 = _c.sent(); error = err_1; return [3, 10]; case 9: if (fs.existsSync(intermediateFilePath)) { fs.unlinkSync(intermediateFilePath); } return [2, { services: services, error: error, filePath: filePath }]; case 10: return [2]; } }); }); }; service_definition_file_content = {}; sardineServices = null; if (compilerSettings.gen_services) { if (fs.existsSync(compilerSettings.gen_services)) { try { service_definition_file_content = JSON.parse(fs.readFileSync(compilerSettings.gen_services).toString()); if (service_definition_file_content.services) { sardineServices = new Map(); for (_i = 0, _a = service_definition_file_content.services; _i < _a.length; _i++) { s = _a[_i]; name_1 = transformer_1.getServiceName(s); if (!sardineServices.has(name_1)) { sardineServices.set(name_1, s); } } service_definition_file_content.services = []; } } catch (e) { if (compilerSettings.verbose) { console.error("error when loading service definition file at [" + compilerSettings.gen_services + "]:", e, '\n'); } } } else { sardineServices = new Map(); service_definition_file_content.services = []; } } Promise.all(targetFiles.map(function (filePath) { return processFile(filePath); })).then(function (results) { var hasError = false; for (var _i = 0, _a = results; _i < _a.length; _i++) { var _b = _a[_i], services = _b.services, error = _b.error, filePath = _b.filePath; if (error) { hasError = true; if (compilerSettings.verbose || compilerSettings.only_validate || compilerSettings.validate) { console.error("ERROR while processing " + filePath + ":", error, '\n'); } } else if (sardineServices) { for (var _c = 0, services_1 = services; _c < services_1.length; _c++) { var s = services_1[_c]; var name_2 = transformer_1.getServiceName(s); sardineServices.set(name_2, s); } } } if (!hasError && !compilerSettings.only_validate && sardineServices) { if (compilerSettings.gen_services && Array.isArray(service_definition_file_content.services)) { service_definition_file_content.services = Array.from(sardineServices.values()); if (compilerSettings.application) { service_definition_file_content.application = compilerSettings.application; } try { fs.writeFileSync(compilerSettings.gen_services, JSON.stringify(service_definition_file_content, null, 4)); if (compilerSettings.verbose) { console.log(service_definition_file_content.services.length + " services stored in the sardine definition file at [" + compilerSettings.gen_services + "]"); } } catch (e) { console.error("ERROR when writing sardine service definition file at [" + compilerSettings.gen_services + "]", e, '\n'); } } } return sardineServices; }).catch(function (e) { console.error('UNKNOW ERROR:', e, '\n'); }); return [2]; }); }); };