-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: astro-purgecss is modifying files keeping old hash (#564)
* fix: astro-purgecss is modifying files keeping the hash fix #450 * x * x * Address the-dijkstra comments * missing utils on build command
- Loading branch information
Showing
3 changed files
with
79 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { readdir, readFile, writeFile } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
export function handleWindowsPath(outputPath: string): string { | ||
if (process.platform !== 'win32') return outputPath; | ||
|
||
if (outputPath.endsWith('\\')) { | ||
outputPath = outputPath.substring(0, outputPath.length - 1); | ||
} | ||
outputPath = outputPath.replaceAll('\\', '/'); | ||
|
||
return outputPath; | ||
} | ||
|
||
export async function replaceStringInFile( | ||
filePath: string, | ||
searchValue: string, | ||
replaceValue: string | ||
) { | ||
try { | ||
const fileContent = await readFile(filePath, 'utf8'); | ||
if (fileContent.includes(searchValue)) { | ||
const re = new RegExp(searchValue, 'g'); | ||
const newContent = fileContent.replace(re, replaceValue); | ||
await writeFile(filePath, newContent, 'utf8'); | ||
} | ||
} catch (err) { | ||
console.error(`Error processing file ${filePath}: ${err}`); | ||
} | ||
} | ||
|
||
export async function replaceStringInDirectory( | ||
directory: string, | ||
searchValue: string, | ||
replaceValue: string | ||
) { | ||
try { | ||
const files = await readdir(directory, { withFileTypes: true }); | ||
for (const file of files) { | ||
const fullPath = path.join(directory, file.name); | ||
if (file.isDirectory()) { | ||
await replaceStringInDirectory(fullPath, searchValue, replaceValue); | ||
} else if (file.isFile()) { | ||
await replaceStringInFile(fullPath, searchValue, replaceValue); | ||
} | ||
} | ||
} catch (err) { | ||
console.error(`Error processing directory ${directory}: ${err}`); | ||
} | ||
} |