From ef7455fd852d47cacf19cb30af6d466a231a2868 Mon Sep 17 00:00:00 2001 From: patrick-zippenfenig Date: Thu, 30 Mar 2023 18:23:31 +0200 Subject: [PATCH] script to rename to index.html files --- package.json | 1 + scripts/rename_to_index_html.js | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 scripts/rename_to_index_html.js diff --git a/package.json b/package.json index 66d07736..2b69ad66 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "scripts": { "dev": "vite dev", "build": "vite build", + "rename_index_files": "node scripts/rename_to_index_html.js", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch", diff --git a/scripts/rename_to_index_html.js b/scripts/rename_to_index_html.js new file mode 100644 index 00000000..01473ecc --- /dev/null +++ b/scripts/rename_to_index_html.js @@ -0,0 +1,37 @@ +import { lstatSync, mkdirSync, readdirSync, renameSync } from 'fs' +import path from 'path'; +import { fileURLToPath } from 'url'; +import {resolve } from 'path' + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const buildDirPath = resolve(__dirname, '../build/'); + +// Search for abcd.html files and move them to abcd/index.html +function walk(directory) { + const files = readdirSync(directory) + files.forEach(file => { + const absolute = `${directory}/${file}` + if (lstatSync(absolute).isDirectory()) { + walk(absolute); + return + } + + const parts = file.split('.') + if (parts.length != 2) { + return + } + if (parts[0] == "index" || parts[1] != "html") { + return + } + + const newDirectory = `${directory}/${parts[0]}` + const newFile = `${newDirectory}/index.html` + console.log(`Rename ${file} to ${parts[0]}/index.html`) + mkdirSync(newDirectory, {recursive: true}) + renameSync(absolute, newFile) + }); +} + +walk(buildDirPath);