|
| 1 | +import type { AppCommand } from '../utils/app-command'; |
| 2 | +// Removed non-critical logging to keep command output silent unless there are serious issues |
| 3 | +import { red } from 'kleur/colors'; |
| 4 | +import { runInPkg } from '../utils/install-deps'; |
| 5 | +import { getPackageManager, panic } from '../utils/utils'; |
| 6 | +import fs from 'fs/promises'; |
| 7 | +import type { Stats } from 'fs'; |
| 8 | +import path from 'path'; |
| 9 | + |
| 10 | +const getDiskPath = (dist: string) => path.resolve(dist); |
| 11 | +const getSrcPath = (src: string) => path.resolve(src); |
| 12 | +const getManifestPath = (dist: string) => path.resolve(dist, 'q-manifest.json'); |
| 13 | + |
| 14 | +export async function runQwikClientCommand(app: AppCommand) { |
| 15 | + try { |
| 16 | + const src = app.args[1]; |
| 17 | + const dist = app.args[2]; |
| 18 | + await checkClientCommand(app, src, dist); |
| 19 | + } catch (e) { |
| 20 | + console.error(`❌ ${red(String(e))}\n`); |
| 21 | + process.exit(1); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Handles the core logic for the 'check-client' command. Exports this function so other modules can |
| 27 | + * import and call it. |
| 28 | + * |
| 29 | + * @param {AppCommand} app - Application command context (assuming structure). |
| 30 | + */ |
| 31 | +async function checkClientCommand(app: AppCommand, src: string, dist: string): Promise<void> { |
| 32 | + if (!(await clientDirExists(dist))) { |
| 33 | + await goBuild(app); |
| 34 | + } else { |
| 35 | + const manifest = await getManifestTs(getManifestPath(dist)); |
| 36 | + if (manifest === null) { |
| 37 | + await goBuild(app); |
| 38 | + } else { |
| 39 | + if (await hasNewer(getSrcPath(src), manifest)) { |
| 40 | + await goBuild(app); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Builds the application using the appropriate package manager. |
| 48 | + * |
| 49 | + * @param {AppCommand} app - The application command object containing app details.e path to the |
| 50 | + * manifest file (though it's not used in the current function). |
| 51 | + * @throws {Error} Throws an error if the build process encounters any issues. |
| 52 | + */ |
| 53 | + |
| 54 | +async function goBuild(app: AppCommand) { |
| 55 | + const pkgManager = getPackageManager(); |
| 56 | + const { install } = await runInPkg(pkgManager, ['run', 'build.client'], app.rootDir); |
| 57 | + if (!(await install)) { |
| 58 | + throw new Error('Client build command reported failure.'); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Retrieves the last modified timestamp of the manifest file. |
| 64 | + * |
| 65 | + * @param {string} manifestPath - The path to the manifest file. |
| 66 | + * @returns {Promise<number | null>} Returns the last modified timestamp (in milliseconds) of the |
| 67 | + * manifest file, or null if an error occurs. |
| 68 | + */ |
| 69 | +async function getManifestTs(manifestPath: string) { |
| 70 | + try { |
| 71 | + // Get stats for the manifest file |
| 72 | + const stats: Stats = await fs.stat(manifestPath); |
| 73 | + return stats.mtimeMs; |
| 74 | + } catch (err: any) { |
| 75 | + // Handle errors accessing the manifest file |
| 76 | + if (err.code !== 'ENOENT') { |
| 77 | + panic(`Error accessing manifest file ${manifestPath}: ${err.message}`); |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Checks if the specified disk directory exists and is accessible. |
| 85 | + * |
| 86 | + * @returns {Promise<boolean>} Returns true if the directory exists and can be accessed, returns |
| 87 | + * false if it doesn't exist or an error occurs. |
| 88 | + */ |
| 89 | +export async function clientDirExists(path: string): Promise<boolean> { |
| 90 | + try { |
| 91 | + await fs.access(getDiskPath(path)); |
| 92 | + return true; // Directory exists |
| 93 | + } catch (err: any) { |
| 94 | + if (!(err.code === 'ENOENT')) { |
| 95 | + panic(`Error accessing disk directory ${path}: ${err.message}`); |
| 96 | + } |
| 97 | + return false; // Directory doesn't exist or there was an error |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Recursively finds the latest modification time (mtime) of any file in the given directory. |
| 103 | + * |
| 104 | + * @param {string} srcPath - The directory path to search. |
| 105 | + * @returns {Promise<number>} Returns the latest mtime (Unix timestamp in milliseconds), or 0 if the |
| 106 | + * directory doesn't exist or is empty. |
| 107 | + */ |
| 108 | +export async function hasNewer(srcPath: string, timestamp: number): Promise<boolean> { |
| 109 | + let returnValue = false; |
| 110 | + async function traverse(dir: string): Promise<void> { |
| 111 | + if (returnValue) { |
| 112 | + return; |
| 113 | + } |
| 114 | + let items: Array<import('fs').Dirent>; |
| 115 | + try { |
| 116 | + items = await fs.readdir(dir, { withFileTypes: true }); |
| 117 | + } catch (err: any) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + for (const item of items) { |
| 122 | + if (returnValue) { |
| 123 | + return; |
| 124 | + } |
| 125 | + const fullPath = path.join(dir, item.name); |
| 126 | + try { |
| 127 | + if (item.isDirectory()) { |
| 128 | + await traverse(fullPath); |
| 129 | + } else if (item.isFile()) { |
| 130 | + const stats = await fs.stat(fullPath); |
| 131 | + if (stats.mtimeMs > timestamp) { |
| 132 | + returnValue = true; |
| 133 | + return; |
| 134 | + } |
| 135 | + } |
| 136 | + } catch (err: any) { |
| 137 | + // Intentionally silent for non-critical access issues |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + await traverse(srcPath); |
| 143 | + return returnValue; |
| 144 | +} |
0 commit comments