-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(cli): Add check-client command to verify bundle freshness #7517
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
Open
JerryWu1234
wants to merge
13
commits into
QwikDev:main
Choose a base branch
from
JerryWu1234:7479_build_serve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
79d7864
feat(cli): Add check-client command to verify bundle freshness
JerryWu1234 b4c6899
Merge branch 'main' into 7479_build_serve
JerryWu1234 179de2f
take some improvements according to a comment
JerryWu1234 1075962
take some improvements according to a comment
JerryWu1234 df4758b
fix(cli): update check-client command and improve hints; adjust packa…
JerryWu1234 ac2b4ef
refactor(cli): consolidate check-client functionality into a single file
JerryWu1234 970334a
refactor(cli): update check-client command to accept src and dist paths
JerryWu1234 9ae8462
refactor(cli): simplify client check and build logging
JerryWu1234 4859c75
Merge branch 'main' into 7479_build_serve
JerryWu1234 11b4b08
fix test
JerryWu1234 fecbd12
rename argument
JerryWu1234 16a499b
Merge branch 'main' into 7479_build_serve
gioboa 449eb30
refactor(cli): rename isNewerThan to hasNewer for clarity
JerryWu1234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@builder.io/qwik-city': patch | ||
'@builder.io/qwik': patch | ||
--- | ||
|
||
feat(cli): Add check-client command to verify bundle freshness |
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 |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import type { AppCommand } from '../utils/app-command'; | ||
import { intro, log, outro } from '@clack/prompts'; | ||
import { red } from 'kleur/colors'; | ||
import { runInPkg } from '../utils/install-deps'; | ||
import { getPackageManager, panic } from '../utils/utils'; | ||
import fs from 'fs/promises'; | ||
import type { Stats } from 'fs'; | ||
import path from 'path'; | ||
|
||
const getDiskPath = (dist: string) => path.resolve(dist); | ||
const getSrcPath = (src: string) => path.resolve(src); | ||
const getManifestPath = (dist: string) => path.resolve(dist, 'q-manifest.json'); | ||
|
||
export async function runQwikClientCommand(app: AppCommand) { | ||
try { | ||
const src = app.args[1]; | ||
const dist = app.args[2]; | ||
await checkClientCommand(app, src, dist); | ||
} catch (e) { | ||
console.error(`❌ ${red(String(e))}\n`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
/** | ||
* Handles the core logic for the 'check-client' command. Exports this function so other modules can | ||
* import and call it. | ||
* | ||
* @param {AppCommand} app - Application command context (assuming structure). | ||
*/ | ||
async function checkClientCommand(app: AppCommand, src: string, dist: string): Promise<void> { | ||
if (!(await clientDirExists(dist))) { | ||
await goBuild(app); | ||
} else { | ||
const manifest = await getManifestTs(getManifestPath(dist)); | ||
if (manifest === null) { | ||
await goBuild(app); | ||
} else { | ||
if (await hasNewer(getSrcPath(src), manifest)) { | ||
await goBuild(app); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Builds the application using the appropriate package manager. | ||
* | ||
* @param {AppCommand} app - The application command object containing app details.e path to the | ||
* manifest file (though it's not used in the current function). | ||
* @throws {Error} Throws an error if the build process encounters any issues. | ||
*/ | ||
|
||
async function goBuild(app: AppCommand) { | ||
const pkgManager = getPackageManager(); | ||
intro('Building client (manifest missing or outdated)...'); | ||
const { install } = await runInPkg(pkgManager, ['run', 'build.client'], app.rootDir); | ||
if (!(await install)) { | ||
throw new Error('Client build command reported failure.'); | ||
} | ||
outro('Client build complete'); | ||
} | ||
|
||
/** | ||
* Retrieves the last modified timestamp of the manifest file. | ||
* | ||
* @param {string} manifestPath - The path to the manifest file. | ||
* @returns {Promise<number | null>} Returns the last modified timestamp (in milliseconds) of the | ||
* manifest file, or null if an error occurs. | ||
*/ | ||
async function getManifestTs(manifestPath: string) { | ||
try { | ||
// Get stats for the manifest file | ||
const stats: Stats = await fs.stat(manifestPath); | ||
return stats.mtimeMs; | ||
} catch (err: any) { | ||
// Handle errors accessing the manifest file | ||
if (err.code === 'ENOENT') { | ||
log.warn(`q-manifest.json file not found`); | ||
} else { | ||
panic(`Error accessing manifest file ${manifestPath}: ${err.message}`); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the specified disk directory exists and is accessible. | ||
* | ||
* @returns {Promise<boolean>} Returns true if the directory exists and can be accessed, returns | ||
* false if it doesn't exist or an error occurs. | ||
*/ | ||
export async function clientDirExists(path: string): Promise<boolean> { | ||
try { | ||
await fs.access(getDiskPath(path)); | ||
return true; // Directory exists | ||
} catch (err: any) { | ||
if (!(err.code === 'ENOENT')) { | ||
panic(`Error accessing disk directory ${path}: ${err.message}`); | ||
} | ||
return false; // Directory doesn't exist or there was an error | ||
} | ||
} | ||
|
||
/** | ||
* Recursively finds the latest modification time (mtime) of any file in the given directory. | ||
* | ||
* @param {string} srcPath - The directory path to search. | ||
* @returns {Promise<number>} Returns the latest mtime (Unix timestamp in milliseconds), or 0 if the | ||
* directory doesn't exist or is empty. | ||
*/ | ||
export async function hasNewer(srcPath: string, timestamp: number): Promise<boolean> { | ||
let returnValue = false; | ||
JerryWu1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async function traverse(dir: string): Promise<void> { | ||
if (returnValue) { | ||
return; | ||
} | ||
let items: Array<import('fs').Dirent>; | ||
try { | ||
items = await fs.readdir(dir, { withFileTypes: true }); | ||
} catch (err: any) { | ||
if (err.code !== 'ENOENT') { | ||
log.warn(`Cannot read directory ${dir}: ${err.message}`); | ||
} | ||
return; | ||
} | ||
|
||
for (const item of items) { | ||
if (returnValue) { | ||
return; | ||
} | ||
const fullPath = path.join(dir, item.name); | ||
JerryWu1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
if (item.isDirectory()) { | ||
await traverse(fullPath); | ||
} else if (item.isFile()) { | ||
const stats = await fs.stat(fullPath); | ||
if (stats.mtimeMs > timestamp) { | ||
returnValue = true; | ||
return; | ||
} | ||
} | ||
} catch (err: any) { | ||
log.warn(`Cannot access ${fullPath}: ${err.message}`); | ||
} | ||
} | ||
} | ||
|
||
await traverse(srcPath); | ||
return returnValue; | ||
} |
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.