UNPKG

ts-to-html

Version:

TS and SASS compiler for a HTML with live preview

86 lines (85 loc) 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const node_child_process_1 = require("node:child_process"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); require("colors"); const log_1 = require("./utils/log"); const parse_args_1 = require("./utils/parse-args"); const args = (0, parse_args_1.parseArgs)(process.argv.slice(2)); const cwd = (0, node_path_1.resolve)(process.cwd(), args.params[0]); // Quick functions const resolve = (...paths) => (0, node_path_1.resolve)(cwd, ...paths); const exec = (cmd) => (0, node_child_process_1.execSync)(cmd, { cwd }); const createFromExample = (file, to) => (0, node_fs_1.writeFileSync)(resolve(to ? to : ".", file), (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(__dirname, "../example", file))); function isInstalled(cmd) { try { (0, node_child_process_1.execSync)(cmd + " --version"); return true; } catch (error) { return false; } } ; // Clear shell (0, log_1.clearAndLog)("Initializing project...".green); // Create directories if (!(0, node_fs_1.existsSync)(cwd)) (0, node_fs_1.mkdirSync)(cwd); if (!(0, node_fs_1.existsSync)(resolve('public'))) (0, node_fs_1.mkdirSync)(resolve('public')); if (!(0, node_fs_1.existsSync)(resolve('src'))) (0, node_fs_1.mkdirSync)(resolve('src')); // Create gitignore file createFromExample("gitignore"); (0, node_fs_1.renameSync)(resolve("gitignore"), resolve(".gitignore")); // Create .env files createFromExample(".env"); createFromExample(".env.example"); // Create files into `public` createFromExample("index.html", "public"); createFromExample("style.scss", "public"); // Create files into `src` (0, node_fs_1.writeFileSync)(resolve('src', 'index.ts'), 'console.log("Hello World!");\r\n// tmp\r\nrequire("./sync.ts")\r\n'); // tmp file createFromExample("sync.ts", "src"); // Update `package.json` execution scripts const pkgJson = resolve('package.json'); const npm = isInstalled("npm") ? "npm" : isInstalled("yarn") ? "yarn" : null; (0, log_1.log)(`Initializing Node.js project using [${npm}]`.cyan); if (!(0, node_fs_1.existsSync)(pkgJson)) { npm ? exec(npm + " init -y") : (0, log_1.log)("No supported package manager found.".red, `${"See:".red} ${"https://github.com/ezequiel-fr/ts-to-html".reset}`); (0, node_fs_1.writeFileSync)(pkgJson, JSON.stringify({ ...JSON.parse((0, node_fs_1.readFileSync)(pkgJson).toString()), scripts: {}, })); } const data = { name: args.params[0], version: "1.0.0", private: true, dependencies: { "ts-to-html": "^" + JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.resolve)(__dirname, "../package.json"), "utf8")).version, }, scripts: { build: "ts-to-html build", start: "ts-to-html start", dev: "ts-to-html dev", }, }; (0, node_fs_1.writeFileSync)(pkgJson, JSON.stringify(data, null, 2).concat("\r\n")); // Init using npm or yarn if (npm) exec(npm + " install"); // Init Git repo if (!args.flags.includes("no-git")) { if (isInstalled("git")) { exec("git init"); (0, log_1.log)("Git repository initialized".cyan); } else { (0, log_1.log)("Git not installed on this machine.".red); } } (0, log_1.log)("Project successfully initialized!".green);