-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from markwylde/implement-hashing
Implement hashing
- Loading branch information
Showing
6 changed files
with
83 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { stat, readdir, readFile, writeFile } from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
export default async function convertFilesToUseHashes(baseDir, lookup) { | ||
for (const file of await readdir(baseDir)) { | ||
const filePath = path.resolve(baseDir, file); | ||
const stats = await stat(filePath); | ||
if (stats.isDirectory()) { | ||
await convertFilesToUseHashes(filePath, lookup); | ||
continue; | ||
} | ||
|
||
let content = await readFile(filePath, 'utf8'); | ||
Object.keys(lookup).forEach((filepath) => { | ||
content = content.replaceAll(`"${filepath}"`, `"./${lookup[filepath]}"`); | ||
content = content.replaceAll(`"./${filepath}"`, `"./${lookup[filepath]}"`); | ||
content = content.replaceAll(`"/${filepath}"`, `"./${lookup[filepath]}"`); | ||
content = content.replaceAll(`'${filepath}'`, `"./${lookup[filepath]}"`); | ||
content = content.replaceAll(`'./${filepath}'`, `"./${lookup[filepath]}"`); | ||
content = content.replaceAll(`'/${filepath}'`, `"./${lookup[filepath]}"`); | ||
}); | ||
await writeFile(filePath, content); | ||
} | ||
} |
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,35 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
|
||
export default async function hashFiles(directory, hashTable = {}, relativePath = '') { | ||
const files = await fs.promises.readdir(directory, { withFileTypes: true }); | ||
const promises = files.map(file => new Promise(async (resolve) => { | ||
const sourceFile = path.resolve(directory, file.name); | ||
const stats = await fs.promises.stat(sourceFile); | ||
if (stats.isDirectory()) { | ||
await hashFiles(sourceFile, hashTable, path.join(relativePath, file.name)); | ||
resolve(); | ||
return; | ||
} | ||
if (sourceFile.endsWith('.html')) { | ||
resolve(); | ||
return; | ||
} | ||
|
||
const hash = crypto.createHash('md5'); | ||
const stream = fs.createReadStream(sourceFile); | ||
stream.on('data', data => hash.update(data)); | ||
stream.on('end', async () => { | ||
const hashString = hash.digest('hex'); | ||
const fileName = path.basename(file.name, path.extname(file.name)); | ||
const fileExt = path.extname(file.name); | ||
const targetFile = path.resolve(directory, `${fileName}-${hashString.slice(0, 8)}${fileExt}`); | ||
await fs.promises.rename(sourceFile, targetFile); | ||
hashTable[path.join(relativePath, file.name)] = path.join(relativePath, `${fileName}-${hashString.slice(0, 8)}${fileExt}`); | ||
resolve(); | ||
}); | ||
})); | ||
await Promise.all(promises); | ||
return hashTable; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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