UNPKG

@tucmc/hazel

Version:
83 lines (80 loc) 3.67 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initScript = void 0; const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const process_1 = __importDefault(require("process")); const readline_sync_1 = __importDefault(require("readline-sync")); const lib_1 = require("../../lib"); const askFileName = () => { const result = readline_sync_1.default.question('Script name: '); if (!result) { return askFileName(); } return result; }; const DEFAULT_HEADER = `/*------Hazel Typescript Header------- --------------PROPERTIES-------------- env_file = "" skip_yes_no = false version = "any" ------------END-PROPERTIES------------ Do not remove the heading otherwise the transpiler will not transpile the script. -------------Header END--------------*/`; const EXAMPLE = `new hazel.Runtime("DEV").runSnippet(async (debug) => { // Add your script here })`; const initScript = (options) => { let filename = options.name; if (!filename) { filename = askFileName(); } if (filename.includes('.')) { console.log(`${lib_1.ConsoleColour.RED}Error script filename. (Script name must not contain special characters.)\nHint: script name should only contain alphanumeric letter or hyphen, for example my-script2.`); return; } if (fs.existsSync(`${filename}.ts`)) { console.log(`${lib_1.ConsoleColour.RED}Unable to create script ${filename}. File is already existed.\nHint: try creating new script with different name.`); return; } let header = DEFAULT_HEADER; if (options.version) { header = header.replace('version = "any"', `version = "=${lib_1.VERSION}"`); } const nodeExe = process_1.default.argv[0]; if (!nodeExe) throw 'Error: can not find hazel module'; const modulePath = path_1.default.resolve(nodeExe, '../..', 'lib/node_modules/@tucmc/hazel'); const libPath = path_1.default.join(modulePath, 'src/lib/index'); const importText = `import * as hazel from "${libPath}"`; fs.writeFileSync(`${filename}.ts`, `${header}\n\n${importText}\n\n${EXAMPLE}`); console.log(`${lib_1.ConsoleColour.GREEN}Script ${lib_1.ConsoleColour.BOLD}${filename}.ts${lib_1.ConsoleColour.RESET}${lib_1.ConsoleColour.GREEN} created successfully!${lib_1.ConsoleColour.RESET}\nLet's edit the snippet file. For further documentation visit https://github.com/triamudomcmc/hazel`); }; exports.initScript = initScript;