UNPKG

@agatee/cli

Version:

CLI for Agatee App

48 lines (47 loc) 2.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSocketInstances = void 0; var fs_1 = require("fs"); var path_1 = __importDefault(require("path")); var console_1 = require("../../utils/console"); var file_1 = require("../../utils/file"); var text_1 = require("../../utils/text"); var socket_instance_starter_1 = require("../add/socket/starter-code/socket-instance.starter"); var generateSocketInstances = function (params) { var instancesPath = params.params.slice(1); var resolvedInstancesPaths = []; // Resolve path and check if there is some error if (instancesPath.length == 0) { throw new Error('Please give 1 or many instance\'s path !'); } for (var _i = 0, instancesPath_1 = instancesPath; _i < instancesPath_1.length; _i++) { var instancePath = instancesPath_1[_i]; if (instancePath.includes('..')) { throw new Error('Invalide path for ' + instancePath + ', path with .. is not supported'); } instancePath = instancePath[0] === '/' ? "./app/" + instancePath : "./app/modules/socket/" + instancePath; instancePath = path_1.default.resolve(instancePath); if ((0, fs_1.existsSync)(instancePath)) { throw new Error("Directory " + instancePath + " already exist !"); } resolvedInstancesPaths.push(instancePath); } for (var _a = 0, resolvedInstancesPaths_1 = resolvedInstancesPaths; _a < resolvedInstancesPaths_1.length; _a++) { var instancePath = resolvedInstancesPaths_1[_a]; generateSocketInstance(instancePath); } }; exports.generateSocketInstances = generateSocketInstances; var generateSocketInstance = function (instancePath) { var splitedPath = instancePath.split('/'); var pathName = splitedPath.pop(); var name = (0, text_1.camelCase)(pathName); var dirName = splitedPath.joint('/'); (0, fs_1.mkdirSync)(dirName, { recursive: true }); var socketInstanceContent = (0, socket_instance_starter_1.socketInstanceStarterCode)(pathName); (0, file_1.createFile)(dirName + "/" + pathName + ".socket.ts", socketInstanceContent.trim()); (0, console_1.logSuccess)("\u2705 Socket instance " + name + " generated: \u26A0\uFE0F Don't forget to add the new instance to the @GatModule imports array."); };