import fs from "fs"; import path from "path"; import {projectRoot} from "../config/import.mjs"; const args = process.argv.slice(2); if (args.length < 1) { console.log("Usage: node replace-skypack.js <project-root>"); process.exit(1); } if (!fs.existsSync(projectRoot)) { console.log("Project root " + projectRoot + " does not exist"); process.exit(1); } const absoluteProjectRoot = path.resolve(projectRoot) + "/"; if (!fs.existsSync(absoluteProjectRoot)) { console.log("Project root " + absoluteProjectRoot + " does not exist"); process.exit(1); } // regex const regex = /<form action="https:\/\/codepen.*<\/form>/gm; const packageJson = path.join(absoluteProjectRoot, 'package.json'); const packageJsonContent = JSON.parse( fs.readFileSync(packageJson, 'utf8')); const docPath = path.normalize(absoluteProjectRoot + 'dist/doc/') const dir = fs.opendirSync(docPath); let f; while ((f = dir.readSync()) !== null) { if (!f.isFile()) { continue; } if ((path.extname(f.name) !== ".html")) { continue; } const fn = docPath + f.name; let content = fs.readFileSync(fn, 'utf8'); let m; while ((m = regex.exec(content)) !== null) { m.forEach((match, groupIndex) => { const s=match.replaceAll(";"+packageJsonContent.name, ";https://cdn.skypack.dev/"+packageJsonContent.name+"@latest"); content=content.replace(match+"", s); }); } fs.writeFileSync(fn, content, 'utf8') } dir.closeSync();