-
-
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.
- Loading branch information
Showing
8 changed files
with
217 additions
and
28 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,8 @@ | ||
{ | ||
"recommendations": [ | ||
"esbenp.prettier-vscode", | ||
"editorconfig.editorconfig", | ||
"dbaeumer.vscode-eslint", | ||
"streetsidesoftware.code-spell-checker" | ||
] | ||
} |
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,17 @@ | ||
{ | ||
"[javascript]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": [ | ||
"source.fixAll.eslint" | ||
] | ||
}, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": "explicit" | ||
}, | ||
"prettier.requireConfig": true, | ||
"eslint.experimental.useFlatConfig": true, | ||
"files.exclude": { | ||
"dist*": true | ||
} | ||
} |
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,105 @@ | ||
import fs from 'node:fs' | ||
import * as path from 'node:path' | ||
import crypto from 'node:crypto' | ||
import packageJson from './package-json-proxy.cjs' | ||
import temporaryDirectory from 'temp-dir' | ||
|
||
function hash(data) { | ||
return crypto.createHash('sha1').update(data).digest('hex') | ||
} | ||
|
||
class Cache { | ||
#root | ||
|
||
#cacheDirectory | ||
|
||
#metaFile | ||
|
||
#files | ||
|
||
#updated = new Map() | ||
|
||
constructor(root) { | ||
this.#root = root | ||
|
||
let cacheDirectory | ||
if (fs.existsSync(path.join(root, 'node_modules'))) { | ||
cacheDirectory = path.join( | ||
root, | ||
`node_modules/${packageJson.name}-cache/`, | ||
) | ||
} else { | ||
cacheDirectory = path.join( | ||
temporaryDirectory, | ||
`${packageJson.name}-cache/${hash(root)}/`, | ||
) | ||
} | ||
|
||
this.#cacheDirectory = cacheDirectory | ||
|
||
this.#metaFile = path.join(this.#cacheDirectory, 'meta.json') | ||
|
||
this.#files = this.#load() ?? new Set() | ||
} | ||
|
||
#load() { | ||
let data | ||
try { | ||
data = JSON.parse(fs.readFileSync(this.#metaFile)) | ||
} catch {} | ||
|
||
if ( | ||
!data || | ||
data.version !== packageJson.version || | ||
data.root !== this.#root || | ||
!Array.isArray(data.files) | ||
) { | ||
return | ||
} | ||
|
||
return new Set(data.files) | ||
} | ||
|
||
getCachedData(content) { | ||
if (!this.#files) return | ||
|
||
const contentHash = hash(content) | ||
|
||
if (!this.#files.has(contentHash)) { | ||
return | ||
} | ||
|
||
try { | ||
return fs.readFileSync(path.join(this.#cacheDirectory, contentHash)) | ||
} catch {} | ||
} | ||
|
||
updateCache(content, data) { | ||
const contentHash = hash(content) | ||
this.#updated.set(contentHash, data) | ||
} | ||
|
||
writeFile() { | ||
fs.mkdirSync(this.#cacheDirectory, {recursive: true}) | ||
|
||
for (const [contentHash, data] of this.#updated) { | ||
fs.writeFileSync(path.join(this.#cacheDirectory, contentHash), data) | ||
} | ||
|
||
fs.writeFileSync( | ||
this.#metaFile, | ||
JSON.stringify( | ||
{ | ||
version: packageJson.version, | ||
root: this.#root, | ||
files: [...new Set([...this.#files, ...this.#updated.keys()])], | ||
time: new Date(), | ||
}, | ||
undefined, | ||
2, | ||
), | ||
) | ||
} | ||
} | ||
|
||
export default Cache |
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 @@ | ||
module.exports = require('./package.json') |
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