UNPKG

next-dev

Version:

Tentu, berikut adalah markdown yang telah diperbaiki:

108 lines (107 loc) 5.08 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 }); const path_1 = __importDefault(require("path")); const readdirp_1 = __importDefault(require("readdirp")); const parser = __importStar(require("@babel/parser")); const traverse_1 = __importDefault(require("@babel/traverse")); const fs = __importStar(require("fs")); const prettier_1 = __importDefault(require("prettier")); async function clearDevBox(argv) { const log = argv.log; console.log(log ? "✨✨✨ START WITH LOG ✨✨✨" : "✨✨✨ START WITHOUT LOG ✨✨✨"); for await (const entry of (0, readdirp_1.default)(path_1.default.join(process.cwd(), '/src/ui'), { fileFilter: ['*.tsx'] })) { // Format the initial code with prettier const fileContent = (await fs.promises.readFile(entry.fullPath, 'utf8')).toString(); if (fileContent.includes("use dev")) { log && console.log(`Clearing ${entry.fullPath}...`); await processClearBox({ stringCode: fileContent, fullPath: entry.fullPath, log }); } } console.log(`✨✨✨ DONE ✨✨✨`); } exports.default = clearDevBox; async function processClearBox({ stringCode, fullPath, log }) { // Format the initial code with prettier log && console.log(`Formating ${fullPath}...`); let code = await prettier_1.default.format(stringCode, { parser: "typescript" }); let ast; try { log && console.log("Parsing code..."); // Parse code into AST ast = parser.parse(code, { sourceType: "module", plugins: ["jsx", "typescript"] }); } catch (error) { console.error("Parsing error:", error.message); return; } try { const newCodeSegments = []; let lastIndex = 0; log && console.log("Traversing code..."); (0, traverse_1.default)(ast, { JSXElement(path) { log && console.log("Traversing JSXElement..."); const openingElement = path.node.openingElement.name.name; if (openingElement === "DevBox") { log && console.log("Found DevBox, removing it..."); const start = path.node.loc?.start.index; const end = path.node.loc?.end.index; if (start !== undefined && end !== undefined) { log && console.log(`Removing DevBox from ${path.node.loc?.start.line}:${path.node.loc?.start.column} to ${path.node.loc?.end.line}:${path.node.loc?.end.column}`); const innerJSX = path.node.children.map(child => code.substring(child.loc.start.index, child.loc.end.index)).join(''); // Add previous segment and the inner JSX log && console.log("Adding previous segment and inner JSX..."); newCodeSegments.push(code.substring(lastIndex, start)); newCodeSegments.push(innerJSX); lastIndex = end; } } } }); // Add the remaining part of the code newCodeSegments.push(code.substring(lastIndex)); let newCode = newCodeSegments.join(''); // Remove DevBox import if no longer needed log && console.log("Removing DevBox import if no longer needed..."); newCode = newCode.replace(/import { DevBox } from "next-dev";\s*/, ''); // Format the new code with prettier log && console.log("Formatting code..."); const formattedCode = await prettier_1.default.format(newCode, { parser: "typescript" }); // Write the new code to the file log && console.log("Writing new code to file..."); await fs.promises.writeFile(fullPath, formattedCode, "utf8"); } catch (error) { console.error("Traversal error:", error.message); return; } }