Skip to content

Commit

Permalink
script to rename to index.html files
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-zippenfenig committed Mar 30, 2023
1 parent f50f1f0 commit ef7455f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions scripts/rename_to_index_html.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit ef7455f

Please sign in to comment.