Skip to content

Instantly share code, notes, and snippets.

@brillout
Last active February 9, 2024 09:27
Show Gist options
  • Save brillout/ca4d166415704d2545eb5241bca8d76d to your computer and use it in GitHub Desktop.
Save brillout/ca4d166415704d2545eb5241bca8d76d to your computer and use it in GitHub Desktop.
V1 Design Migration Script
// This script was used for migrating:
// - https://github.com/vikejs/vike/tree/main/docs
// - https://github.com/brillout/telefunc/tree/main/docs
import { execSync } from 'child_process'
import fs from 'fs'
import path from 'path'
const mapping = {
'.page.server.mdx': '/+Page.mdx',
'.page.route.ts': '/+route.ts'
}
main()
function main() {
console.log('Migrated:')
const todoManually = []
getFiles().forEach((file) => {
for (const [suffixOld, suffixNew] of Object.entries(mapping)) {
if (file.endsWith(suffixOld)) {
const fileMoved = file.replace(suffixOld, suffixNew)
const fileAbs = addCwd(file)
const fileMovedAbs = addCwd(fileMoved)
console.log('-', file)
console.log('+', fileMoved)
fs.mkdirSync(path.dirname(fileMovedAbs), { recursive: true })
fs.renameSync(fileAbs, fileMovedAbs)
updateImportPaths(fileMovedAbs)
return
}
}
if (file.includes('.page.')) {
todoManually.push(file)
}
})
if (todoManually.length) {
console.log(
[
// prettier-ignore
'',
'To-Do migrate manually:',
...todoManually.map((f) => ` ${f}`)
].join('\n')
)
}
}
function getFiles() {
const stdout = execSync('git ls-files', { encoding: 'utf-8' })
const files = stdout.split('\n').filter(Boolean)
return files
}
function addCwd(file) {
const cwd = process.cwd()
const fileAbs = path.join(cwd, file)
return fileAbs
}
function updateImportPaths(filePath) {
let content = fs.readFileSync(filePath, 'utf-8')
content = content.replaceAll(`from '../`, `from '../../`)
content = content.replaceAll(`from './`, `from '../`)
fs.writeFileSync(filePath, content)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment