diff --git a/cli/src/commands/server.ts b/cli/src/commands/server.ts index b22d100..06073b6 100644 --- a/cli/src/commands/server.ts +++ b/cli/src/commands/server.ts @@ -7,6 +7,7 @@ import {buildContext} from '../services/context.services'; import {setController} from '../services/controller.services'; import {collectIdentities} from '../services/identity.services'; import {transfer} from '../services/ledger.services'; +import {touchWatchedFile} from '../services/touch.services'; import type {CliContext} from '../types/context'; import {nextArg} from '../utils/args.utils'; @@ -92,6 +93,11 @@ const buildServer = ({context}: {context: CliContext}): Server => res.end(JSON.stringify(identities)); return; } + case 'touch': { + await touchWatchedFile({searchParams}); + done(); + return; + } } error404(); diff --git a/cli/src/services/touch.services.ts b/cli/src/services/touch.services.ts new file mode 100644 index 0000000..b2a2800 --- /dev/null +++ b/cli/src/services/touch.services.ts @@ -0,0 +1,41 @@ +import {isEmptyString} from '@dfinity/utils'; +import kleur from 'kleur'; +import {existsSync} from 'node:fs'; +import {utimes} from 'node:fs/promises'; +import {join} from 'node:path'; +import {DEV_DEPLOY_FOLDER} from '../constants/dev.constants'; + +const {yellow, red} = kleur; + +/** + * Workaround Podman and Apple container issues on macOS: + * - https://github.com/containers/podman/issues/22343 + * - https://github.com/apple/container/issues/141 + */ +export const touchWatchedFile = async ({searchParams}: {searchParams: URLSearchParams}) => { + const file = searchParams.get('file'); + + if (isEmptyString(file)) { + console.log(`ℹ️ Skipped touching file: no file provided.`); + return; + } + + const filename = decodeURIComponent(file); + const filepath = join(DEV_DEPLOY_FOLDER, filename); + + if (!existsSync(filepath)) { + console.log(`ℹ️ Skipped touching file: ${yellow(filepath)} does not exist.`); + return; + } + + try { + await touch(filepath); + } catch (err: unknown) { + console.log(red(`️‼️ Unexpected error while touching ${filepath}`), err); + } +}; + +const touch = async (filePath: string) => { + const now = new Date(); + await utimes(filePath, now, now); +};