-
-
Notifications
You must be signed in to change notification settings - Fork 159
fix: use commit time for cache #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
383bf12
fix: use commit time for cache
dummdidumm 50cab39
test: modify unrelated file
dummdidumm 00c52b6
test: modify md file
dummdidumm 878392b
test: modify code snippet
dummdidumm 66917c2
test: modify file related to snippet generation
dummdidumm 702c8aa
another test
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -9,6 +9,7 @@ import { transformerTwoslash } from '@shikijs/twoslash'; | |||||
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, smart_quotes, transform } from './utils'; | ||||||
import type { Modules } from './index'; | ||||||
import { fileURLToPath } from 'node:url'; | ||||||
import { execSync } from 'node:child_process'; | ||||||
|
||||||
interface SnippetOptions { | ||||||
file: string | null; | ||||||
|
@@ -174,7 +175,7 @@ export async function render_content_markdown( | |||||
} | ||||||
|
||||||
if (options.copy) { | ||||||
html += `<button class="copy-to-clipboard raised" title="Copy to clipboard" aria-label="Copy to clipboard"></button>`; | ||||||
html += `<button class="copy-to-clipboard raised" title="Copyyyy to clipboard" aria-label="Copy to clipboard"></button>`; | ||||||
} | ||||||
|
||||||
html += '</div>'; | ||||||
|
@@ -510,15 +511,15 @@ function find_nearest_node_modules(file: string): string | null { | |||||
} | ||||||
|
||||||
/** | ||||||
* Get the `mtime` of the most recently modified file in a dependency graph, | ||||||
* Get the commit time of the most recently modified file in a dependency graph, | ||||||
* excluding imports from `node_modules` | ||||||
*/ | ||||||
function get_mtime(file: string, seen = new Set<string>()) { | ||||||
if (seen.has(file)) return -1; | ||||||
function get_commit_time(file: string, seen = new Set<string>()) { | ||||||
if (seen.has(file)) return 0; | ||||||
seen.add(file); | ||||||
|
||||||
let mtime = fs.statSync(file).mtimeMs; | ||||||
const content = fs.readFileSync(file, 'utf-8'); | ||||||
let latest_commit_time = 0; | ||||||
|
||||||
for (const [_, source] of content.matchAll(/^import(?:.+?\s+from\s+)?['"](.+)['"];?$/gm)) { | ||||||
if (source[0] !== '.') continue; | ||||||
|
@@ -528,16 +529,22 @@ function get_mtime(file: string, seen = new Set<string>()) { | |||||
if (!fs.existsSync(resolved)) | ||||||
throw new Error(`Could not resolve ${source} relative to ${file}`); | ||||||
|
||||||
mtime = Math.max(mtime, get_mtime(resolved, seen)); | ||||||
const child_commit_time = get_commit_time(resolved, seen); | ||||||
if (child_commit_time > latest_commit_time) { | ||||||
latest_commit_time = child_commit_time; | ||||||
} | ||||||
} | ||||||
|
||||||
return mtime; | ||||||
const file_commit_time = parseInt( | ||||||
execSync(`git log -n 1 --pretty=format:%ct -- ${file}`).toString().trim(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Untested.)
Suggested change
|
||||||
10 | ||||||
); | ||||||
return latest_commit_time > file_commit_time ? latest_commit_time : file_commit_time; | ||||||
} | ||||||
|
||||||
const mtime = Math.max( | ||||||
get_mtime(fileURLToPath(import.meta.url)), | ||||||
fs.statSync('node_modules').mtimeMs, | ||||||
fs.statSync('../../pnpm-lock.yaml').mtimeMs | ||||||
const commit_time = Math.max( | ||||||
get_commit_time(fileURLToPath(import.meta.url)), | ||||||
get_commit_time('../../pnpm-lock.yaml') | ||||||
); | ||||||
|
||||||
/** | ||||||
|
@@ -560,7 +567,7 @@ async function create_snippet_cache(should: boolean) { | |||||
|
||||||
if (fs.existsSync(directory)) { | ||||||
for (const dir of fs.readdirSync(directory)) { | ||||||
if (!fs.statSync(`${directory}/${dir}`).isDirectory() || +dir < mtime) { | ||||||
if (!fs.statSync(`${directory}/${dir}`).isDirectory() || +dir < commit_time) { | ||||||
fs.rmSync(`${directory}/${dir}`, { force: true, recursive: true }); | ||||||
} | ||||||
} | ||||||
|
@@ -569,15 +576,15 @@ async function create_snippet_cache(should: boolean) { | |||||
} | ||||||
|
||||||
try { | ||||||
fs.mkdirSync(`${directory}/${mtime}`); | ||||||
fs.mkdirSync(`${directory}/${commit_time}`); | ||||||
} catch {} | ||||||
|
||||||
function get_file(source: string) { | ||||||
const hash = createHash('sha256'); | ||||||
hash.update(source); | ||||||
const digest = hash.digest().toString('base64').replace(/\//g, '-'); | ||||||
|
||||||
return `${directory}/${mtime}/${digest}.html`; | ||||||
return `${directory}/${commit_time}/${digest}.html`; | ||||||
} | ||||||
|
||||||
return { | ||||||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ const escapeReplacements = { | |
'"': '"', | ||
"'": ''' | ||
}; | ||
|
||
//test | ||
/** | ||
* @param {string} ch | ||
*/ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.