diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f4ebbf..d1dea4d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,9 @@ on: push: branches: [main] pull_request: - branches: [main] + +permissions: + contents: read concurrency: group: ci-${{ github.ref }} @@ -26,8 +28,14 @@ jobs: - name: Install dependencies run: npm ci + - name: Lint + run: npm run lint + + - name: Typecheck + run: npm run typecheck + - name: Build run: npm run build - - name: Check - run: npm run check + - name: Test + run: npm test diff --git a/biome.json b/biome.json index 50579aa..cfda237 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/2.3.5/schema.json", + "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", "linter": { "enabled": true, "rules": { @@ -11,7 +11,7 @@ "useTemplate": "off" }, "correctness": { - "noUnusedVariables": "off" + "noUnusedVariables": "error" }, "suspicious": { "noExplicitAny": "off", diff --git a/package-lock.json b/package-lock.json index 5d0604d..5976871 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@willtmc/gmcli", - "version": "0.4.2", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@willtmc/gmcli", - "version": "0.4.2", + "version": "0.5.0", "license": "MIT", "dependencies": { "google-auth-library": "^10.1.0", @@ -18,7 +18,6 @@ "devDependencies": { "@biomejs/biome": "^1.9.4", "@types/node": "^22.10.1", - "@typescript/native-preview": "^7.0.0-dev.20251204.1", "husky": "^9.1.7", "typescript": "^5.7.2" }, @@ -227,123 +226,6 @@ "undici-types": "~6.21.0" } }, - "node_modules/@typescript/native-preview": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview/-/native-preview-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-nyMp0ybgJVZFtDOWmcKDqaRqtj8dOg65+fDxbjIrnZuMWIqlOUGH+imFwofqlW+KndAA7KtAio2YSZMMZB25WA==", - "dev": true, - "license": "Apache-2.0", - "bin": { - "tsgo": "bin/tsgo.js" - }, - "optionalDependencies": { - "@typescript/native-preview-darwin-arm64": "7.0.0-dev.20251204.1", - "@typescript/native-preview-darwin-x64": "7.0.0-dev.20251204.1", - "@typescript/native-preview-linux-arm": "7.0.0-dev.20251204.1", - "@typescript/native-preview-linux-arm64": "7.0.0-dev.20251204.1", - "@typescript/native-preview-linux-x64": "7.0.0-dev.20251204.1", - "@typescript/native-preview-win32-arm64": "7.0.0-dev.20251204.1", - "@typescript/native-preview-win32-x64": "7.0.0-dev.20251204.1" - } - }, - "node_modules/@typescript/native-preview-darwin-arm64": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-darwin-arm64/-/native-preview-darwin-arm64-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-CgIzuO/LFRufdVjJmll6x7jnejYqqLo4kJwrsUxQipJ/dcGeP0q2XMcxNBzT7F9L4Sd5dphRPOZFXES4kS0lig==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "darwin" - ] - }, - "node_modules/@typescript/native-preview-darwin-x64": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-darwin-x64/-/native-preview-darwin-x64-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-X76oQeDMQHJiukkPPbk7STrfu97pfPe5ixwiN6nXzSGXLE+tzrXRecNkYhz4XWeAW2ASNmGwDJJ2RAU5l8MbgQ==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "darwin" - ] - }, - "node_modules/@typescript/native-preview-linux-arm": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-linux-arm/-/native-preview-linux-arm-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-3zl/Jj5rzkK9Oo5KVSIW+6bzRligoI+ZnA1xLpg0BBH2sk27a8Vasj7ZaGPlFvlSegvcaJdIjSt7Z8nBtiF9Ww==", - "cpu": [ - "arm" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@typescript/native-preview-linux-arm64": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-linux-arm64/-/native-preview-linux-arm64-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-+1as+h6ZNpc9TqlHwvDkBP7jg0FoCMUf6Rrc9/Mkllau6etznfVsWMADWT4t76gkGZKUIXOZqsl2Ya3uaBrCBQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@typescript/native-preview-linux-x64": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-linux-x64/-/native-preview-linux-x64-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-YD//l6yv7iPNlKn9OZDzBxrI+QGLN6d4RV3dSucsyq/YNZUulcywGztbZiaQxdUzKPwj70G+LVb9WCgf5ITOIQ==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ] - }, - "node_modules/@typescript/native-preview-win32-arm64": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-win32-arm64/-/native-preview-win32-arm64-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-eDXYR5qfPFA8EfQ0d9SbWGLn02VbAaeTM9jQ5VeLlPLcBP81nGRaGQ9Quta5zeEHev1S9iCdyRj5BqCRtl0ohw==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "win32" - ] - }, - "node_modules/@typescript/native-preview-win32-x64": { - "version": "7.0.0-dev.20251204.1", - "resolved": "https://registry.npmjs.org/@typescript/native-preview-win32-x64/-/native-preview-win32-x64-7.0.0-dev.20251204.1.tgz", - "integrity": "sha512-CRWI2OPdqXbzOU52R2abWMb3Ie2Wp6VPrCFzR3pzP53JabTAe8+XoBWlont9bw/NsqbPKp2aQbdfbLQX5RI44g==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "Apache-2.0", - "optional": true, - "os": [ - "win32" - ] - }, "node_modules/agent-base": { "version": "7.1.4", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz", diff --git a/package.json b/package.json index 46cb21a..2fc24f3 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,10 @@ ], "scripts": { "build": "tsc && chmod +x dist/cli.js", - "check": "biome check src/ --write --diagnostic-level=error && tsgo --noEmit", + "lint": "biome check src/", + "format": "biome check src/ --write", + "typecheck": "tsc --noEmit", + "check": "npm run lint && npm run typecheck", "test": "npm run build && node --test test/**/*.test.js", "install-local": "bash scripts/install-local.sh", "prepublishOnly": "npm run build", @@ -30,7 +33,6 @@ "devDependencies": { "@biomejs/biome": "^1.9.4", "@types/node": "^22.10.1", - "@typescript/native-preview": "^7.0.0-dev.20251204.1", "husky": "^9.1.7", "typescript": "^5.7.2" }, diff --git a/scripts/install-local.sh b/scripts/install-local.sh index 2e4765b..b588814 100755 --- a/scripts/install-local.sh +++ b/scripts/install-local.sh @@ -18,31 +18,48 @@ fi echo "Installing gmcli globally from ${REPO_ROOT}..." npm install -g . -INSTALLED_BIN="$(command -v gmcli || true)" -if [[ -z "${INSTALLED_BIN}" ]]; then - echo "Error: 'gmcli' was not found on PATH after 'npm install -g .'." >&2 +NPM_PREFIX="$(npm prefix -g)" +if [[ -z "${NPM_PREFIX}" ]]; then + echo "Error: could not determine npm global prefix via 'npm prefix -g'." >&2 exit 1 fi -if [[ ! -x "${INSTALLED_BIN}" ]]; then - echo "Error: resolved gmcli binary is not executable: ${INSTALLED_BIN}" >&2 +NPM_BIN="${NPM_PREFIX}/bin/gmcli" +if [[ ! -e "${NPM_BIN}" ]]; then + echo "Error: expected npm global gmcli binary at ${NPM_BIN} after 'npm install -g .', but it does not exist." >&2 exit 1 fi -if ! cmp -s "${BUILT_CLI}" "${INSTALLED_BIN}"; then - RESOLVED_REALPATH="$(node -e 'const fs = require("fs"); console.log(fs.realpathSync(process.argv[1]));' "${INSTALLED_BIN}" 2>/dev/null || echo "${INSTALLED_BIN}")" - echo "Error: installed gmcli binary does not match newly built dist/cli.js." >&2 - echo " PATH binary: ${INSTALLED_BIN} (realpath: ${RESOLVED_REALPATH})" >&2 - echo " Built CLI: ${BUILT_CLI}" >&2 +if [[ ! -x "${NPM_BIN}" ]]; then + echo "Error: npm global gmcli binary is not executable: ${NPM_BIN}" >&2 + exit 1 +fi + +if ! cmp -s "${BUILT_CLI}" "${NPM_BIN}"; then + RESOLVED_REALPATH="$(node -e 'const fs = require("fs"); console.log(fs.realpathSync(process.argv[1]));' "${NPM_BIN}" 2>/dev/null || echo "${NPM_BIN}")" + echo "Error: npm global gmcli binary does not match newly built dist/cli.js." >&2 + echo " npm binary: ${NPM_BIN} (realpath: ${RESOLVED_REALPATH})" >&2 + echo " Built CLI: ${BUILT_CLI}" >&2 exit 1 fi EXPECTED_HELP="$(node "${BUILT_CLI}" --help 2>&1 || true)" -INSTALLED_HELP="$("${INSTALLED_BIN}" --help 2>&1 || true)" +INSTALLED_HELP="$("${NPM_BIN}" --help 2>&1 || true)" if [[ -z "${EXPECTED_HELP}" || "${INSTALLED_HELP}" != "${EXPECTED_HELP}" ]]; then - echo "Error: '${INSTALLED_BIN} --help' output does not match 'node ${BUILT_CLI} --help'." >&2 + echo "Error: '${NPM_BIN} --help' output does not match 'node ${BUILT_CLI} --help'." >&2 exit 1 fi -echo "Verified ${INSTALLED_BIN} matches ${BUILT_CLI}." +PATH_BIN="$(command -v gmcli || true)" +NPM_BIN_REALPATH="$(node -e 'const fs = require("fs"); console.log(fs.realpathSync(process.argv[1]));' "${NPM_BIN}" 2>/dev/null || echo "${NPM_BIN}")" +if [[ -n "${PATH_BIN}" ]]; then + PATH_BIN_REALPATH="$(node -e 'const fs = require("fs"); console.log(fs.realpathSync(process.argv[1]));' "${PATH_BIN}" 2>/dev/null || echo "${PATH_BIN}")" + if [[ "${PATH_BIN}" != "${NPM_BIN}" && "${PATH_BIN_REALPATH}" != "${NPM_BIN_REALPATH}" ]]; then + echo "Note: PATH resolves gmcli to ${PATH_BIN} (wrapper); verified npm global binary at ${NPM_BIN}." + fi +else + echo "Note: 'gmcli' is not on PATH; verified npm global binary at ${NPM_BIN}." +fi + +echo "Verified ${NPM_BIN} matches ${BUILT_CLI}." diff --git a/test/install-local.test.js b/test/install-local.test.js index 8937543..a5b1668 100644 --- a/test/install-local.test.js +++ b/test/install-local.test.js @@ -21,42 +21,78 @@ test("scripts/install-local.sh is executable and passes bash -n syntax check", ( execFileSync("bash", ["-n", scriptPath], { stdio: "pipe" }); }); -test("scripts/install-local.sh runs build + global install and fails when PATH gmcli is stale", (t) => { +test("scripts/install-local.sh verifies npm global binary and tolerates PATH wrapper", (t) => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "gmcli-install-local-")); t.after(() => fs.rmSync(tmpDir, { recursive: true, force: true })); + const npmPrefix = path.join(tmpDir, "npm-prefix"); + const npmBinDir = path.join(npmPrefix, "bin"); + const wrapperDir = path.join(tmpDir, "wrapper-bin"); + const toolsDir = path.join(tmpDir, "tools"); + fs.mkdirSync(npmBinDir, { recursive: true }); + fs.mkdirSync(wrapperDir, { recursive: true }); + fs.mkdirSync(toolsDir, { recursive: true }); + const logFile = path.join(tmpDir, "npm-calls.log"); - const mockNpm = path.join(tmpDir, "npm"); + const mockNpm = path.join(toolsDir, "npm"); fs.writeFileSync( mockNpm, - `#!/usr/bin/env bash\necho "$*" >> "${logFile}"\nexit 0\n`, + `#!/usr/bin/env bash +echo "$*" >> "${logFile}" +if [[ "$*" == "prefix -g" ]]; then + echo "${npmPrefix}" +fi +exit 0 +`, { mode: 0o755 }, ); - // 1. Stale gmcli on PATH should fail verification. - const staleBin = path.join(tmpDir, "gmcli"); - fs.writeFileSync(staleBin, "#!/usr/bin/env bash\necho 'stale help'\nexit 1\n", { mode: 0o755 }); + // 1. Stale npm global gmcli binary should fail verification. + const npmGmcli = path.join(npmBinDir, "gmcli"); + fs.writeFileSync(npmGmcli, "#!/usr/bin/env bash\necho 'stale help'\nexit 1\n", { mode: 0o755 }); const staleRun = spawnSync("bash", [scriptPath], { cwd: repoRoot, - env: { ...process.env, PATH: `${tmpDir}:${process.env.PATH}` }, + env: { ...process.env, PATH: `${toolsDir}:${npmBinDir}:${process.env.PATH}` }, encoding: "utf8", }); - assert.notEqual(staleRun.status, 0, "expected non-zero exit when PATH gmcli is stale"); + assert.notEqual(staleRun.status, 0, "expected non-zero exit when npm global gmcli is stale"); assert.match(staleRun.stderr, /does not match newly built dist\/cli\.js/); const calls = fs.readFileSync(logFile, "utf8").trim().split("\n"); - assert.deepEqual(calls, ["run build", "install -g ."]); + assert.deepEqual(calls, ["run build", "install -g .", "prefix -g"]); - // 2. Fresh gmcli symlink pointing to dist/cli.js should pass verification. - fs.unlinkSync(staleBin); - fs.symlinkSync(path.join(repoRoot, "dist", "cli.js"), staleBin); + // 2. Fresh npm global gmcli symlink pointing to dist/cli.js should pass verification. + fs.unlinkSync(npmGmcli); + fs.symlinkSync(path.join(repoRoot, "dist", "cli.js"), npmGmcli); const freshRun = spawnSync("bash", [scriptPath], { cwd: repoRoot, - env: { ...process.env, PATH: `${tmpDir}:${process.env.PATH}` }, + env: { ...process.env, PATH: `${toolsDir}:${npmBinDir}:${process.env.PATH}` }, encoding: "utf8", }); - assert.equal(freshRun.status, 0, `expected exit 0 when PATH gmcli matches dist/cli.js; stderr: ${freshRun.stderr}`); + assert.equal(freshRun.status, 0, `expected exit 0 when npm global gmcli matches dist/cli.js; stderr: ${freshRun.stderr}`); assert.match(freshRun.stdout, /Verified .* matches .*dist\/cli\.js/); + assert.doesNotMatch(freshRun.stdout, /Note: PATH resolves gmcli to/); + + // 3. PATH wrapper (e.g. Doppler shim) shadowing npm global binary should pass without executing wrapper. + const wrapperBin = path.join(wrapperDir, "gmcli"); + fs.writeFileSync( + wrapperBin, + "#!/usr/bin/env bash\necho 'wrapper should not be invoked' >&2\nexit 99\n", + { mode: 0o755 }, + ); + + const wrapperRun = spawnSync("bash", [scriptPath], { + cwd: repoRoot, + env: { ...process.env, PATH: `${toolsDir}:${wrapperDir}:${npmBinDir}:${process.env.PATH}` }, + encoding: "utf8", + }); + assert.equal( + wrapperRun.status, + 0, + `expected exit 0 when PATH resolves to wrapper and npm global gmcli matches dist/cli.js; stderr: ${wrapperRun.stderr}`, + ); + assert.match(wrapperRun.stdout, /Note: PATH resolves gmcli to .* \(wrapper\); verified npm global binary at /); + assert.match(wrapperRun.stdout, /Verified .* matches .*dist\/cli\.js/); });