Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
Expand All @@ -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
4 changes: 2 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -11,7 +11,7 @@
"useTemplate": "off"
},
"correctness": {
"noUnusedVariables": "off"
"noUnusedVariables": "error"
},
"suspicious": {
"noExplicitAny": "off",
Expand Down
122 changes: 2 additions & 120 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
43 changes: 30 additions & 13 deletions scripts/install-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}."
64 changes: 50 additions & 14 deletions test/install-local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
Loading