Skip to content

Instantly share code, notes, and snippets.

@jsjoeio
Last active December 26, 2021 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsjoeio/cbb67da92c82de608735b7bfd1c4a11d to your computer and use it in GitHub Desktop.
Save jsjoeio/cbb67da92c82de608735b7bfd1c4a11d to your computer and use it in GitHub Desktop.
Migrate Content in Gatsby Site to Astro Site
/*
README
I wrote this script to migrate all my blog posts from my Gatsby site to my Astro site.
Sharing in case it helps anyone with their own migration.
Context: my Gatsby site had the blog posts under `/content` and in there, each post had
a directory and inside that it had an `index.md` and images.
I built the new Astro site as a subdirectory in the same repo as my Gatsby site
which let me migrate the posts very easily.
*/
;(async function main() {
console.log('🔨 running migration script')
const pathToContent = `${Deno.cwd()}/content`
for await (const dirEntry of Deno.readDir(pathToContent)) {
const currentDir = dirEntry.name
const pathToDir = `${pathToContent}/${currentDir}`
console.log(`🚧 processing directory: ${currentDir}`)
await processDir(currentDir, pathToDir)
}
console.log(`✅ Done processing /content directory.`)
})()
/**
* Used to process a directory under /content
*
* Expected to have an index.md and maybe .png files
*/
async function processDir(dirName: string, fullPathToDir: string) {
// We need to move the index file first
// otherwise, we'll call updateImageReferenceInMarkdownFile
// on a markdown file that doesn't yet exist.
// This assumes we always have one, but if we didn't that'd be weird
console.log(`...moving index.md`)
await moveIndexFile(dirName, fullPathToDir)
// Loop through files
for await (const dirEntry of Deno.readDir(fullPathToDir)) {
const fileName = dirEntry.name
if (/\.(gif|jpe?g|tiff?|png|webp|bmp)$/i.test(fileName)) {
console.log(`...moving image file: ${fileName}`)
await moveImageFile(fileName, fullPathToDir)
console.log(`...updating image reference`)
await updateImageReferenceInMarkdownFile(fileName, dirName)
}
}
// Clean up - remove dir
await Deno.remove(fullPathToDir, { recursive: true })
console.log(`✔ Success: processed ${dirName}`)
}
async function moveIndexFile(dirName: string, fullPathToDir: string) {
const oldFilePath = `${fullPathToDir}/index.md`
const newFilePath = `${Deno.cwd()}/astro-migration/src/data/posts/${dirName}.md`
await Deno.rename(oldFilePath, newFilePath)
}
async function moveImageFile(fileName: string, fullPathToDir: string) {
const oldFilePath = `${fullPathToDir}/${fileName}`
const newFilePath = `${Deno.cwd()}/astro-migration/public/assets/images/${fileName}`
await Deno.rename(oldFilePath, newFilePath)
}
async function updateImageReferenceInMarkdownFile(
fileName: string,
dirName: string,
) {
// readFile
const pathToMarkdownFile = `${Deno.cwd()}/astro-migration/src/data/posts/${dirName}.md`
const decoder = new TextDecoder('utf-8')
const data = await Deno.readFile(pathToMarkdownFile)
// as string
const fileAsString = decoder.decode(data)
// replace text in string
const updatedString = fileAsString.replace(
`./${fileName}`,
`assets/images/${fileName}`,
)
// writeFile (overwrite?)
const encoder = new TextEncoder()
const newData = encoder.encode(updatedString)
await Deno.writeFile(pathToMarkdownFile, newData)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment