cloudflare-cli icon indicating copy to clipboard operation
cloudflare-cli copied to clipboard

cfcli script doesn't set CWD

Open oising opened this issue 5 months ago • 1 comments

I just installed cfcli (windows 11, arm64, node 24) and it doesn't seem to work out of the box. The script stub is found, but the node implementation looks for supporting files relative to the stub, not the nodejs implementation, i.e.

Image

So I added a try/finally with a pushd/popd pairing to set the write working directory:

#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
  # Fix case when both the Windows and Linux builds of Node
  # are installed in the same directory
  $exe=".exe"
}

$ret=0

try {
# set cwd
pushd (join-path $basedir "node_modules/cloudflare-cli/")

if (Test-Path "$basedir/node$exe") {
  # Support pipeline input
  if ($MyInvocation.ExpectingInput) {
    $input | & "$basedir/node$exe"  "$basedir/node_modules/cloudflare-cli/bin/cfcli" $args
  } else {
    & "$basedir/node$exe"  "$basedir/node_modules/cloudflare-cli/bin/cfcli" $args
  }
  $ret=$LASTEXITCODE
} else {
  # Support pipeline input
  if ($MyInvocation.ExpectingInput) {
    $input | & "node$exe"  "$basedir/node_modules/cloudflare-cli/bin/cfcli" $args
  } else {
    & "node$exe"  "$basedir/node_modules/cloudflare-cli/bin/cfcli" $args
  }
  $ret=$LASTEXITCODE
}

} finally { popd }

exit $ret

oising avatar Oct 03 '25 14:10 oising

  1. Edit the installed module file at:

    • Windows: %APPDATA%\npm\node_modules\cloudflare-cli\index.js
    • macOS/Linux: /usr/local/lib/node_modules/cloudflare-cli/index.js or ~/.nvm/versions/node/v*/lib/node_modules/cloudflare-cli/index.js
  2. Add these imports after line 6 (after import path from 'path';):

import { fileURLToPath } from 'url';
import { dirname } from 'path';
  1. Replace line 365 (the showHelp method) with:
showHelp() {
  return Promise.resolve(new Result([fs.readFileSync(path.join(dirname(fileURLToPath(import.meta.url)), 'doc', 'help.txt'), 'utf8')]));
}

poulpreben avatar Nov 03 '25 12:11 poulpreben