diff --git a/.github/workflows/codra-cli-release.yml b/.github/workflows/codra-cli-release.yml index aae1ff1..0b1ff83 100644 --- a/.github/workflows/codra-cli-release.yml +++ b/.github/workflows/codra-cli-release.yml @@ -143,6 +143,9 @@ jobs: - name: Validate npm pack contents working-directory: packages/codra-npm-cli env: + CODRA_USE_ARTIFACTS: '1' + CODRA_ARTIFACTS_DIR: ${{ github.workspace }}/packages/codra-npm-cli/artifacts + CODRA_ALLOW_PARTIAL_BINARIES: ${{ needs.resolve-matrix.outputs.allow_partial }} CODRA_EXPECT_PLATFORMS: ${{ needs.resolve-matrix.outputs.expect_all_pack == '1' && needs.resolve-matrix.outputs.expected_platforms || '' }} run: npm run pack:dry @@ -152,6 +155,7 @@ jobs: CODRA_USE_ARTIFACTS: '1' CODRA_ARTIFACTS_DIR: ${{ github.workspace }}/packages/codra-npm-cli/artifacts CODRA_ALLOW_PARTIAL_BINARIES: ${{ needs.resolve-matrix.outputs.allow_partial }} + CODRA_EXPECT_PLATFORMS: ${{ needs.resolve-matrix.outputs.expect_all_pack == '1' && needs.resolve-matrix.outputs.expected_platforms || '' }} run: npm pack - name: Upload npm tarball artifact diff --git a/.gitignore b/.gitignore index 1c2fe9a..0303749 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,11 @@ node_modules .DS_Store *.log +# Local secrets (never commit) +.env +.env.* +!.env.example + # Rust target/ **/*.rs.bk diff --git a/packages/codra-npm-cli/scripts/pack-dry-run.js b/packages/codra-npm-cli/scripts/pack-dry-run.js index c3beee2..7fe010a 100644 --- a/packages/codra-npm-cli/scripts/pack-dry-run.js +++ b/packages/codra-npm-cli/scripts/pack-dry-run.js @@ -31,6 +31,26 @@ function expectedNativePaths() { return []; } +function packagingEnv(expectedNative) { + const env = { ...process.env }; + const passthrough = [ + 'CODRA_USE_ARTIFACTS', + 'CODRA_ARTIFACTS_DIR', + 'CODRA_ALLOW_PARTIAL_BINARIES', + 'CODRA_EXPECT_PLATFORMS', + 'CODRA_EXPECT_ALL_PLATFORMS', + ]; + for (const key of passthrough) { + if (process.env[key] !== undefined) { + env[key] = process.env[key]; + } + } + if (!env.CODRA_USE_ARTIFACTS && expectedNative.length > 0) { + env.CODRA_USE_ARTIFACTS = '1'; + } + return env; +} + function main() { const expectedNative = expectedNativePaths(); @@ -39,12 +59,7 @@ function main() { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], shell: true, - env: { - ...process.env, - CODRA_USE_ARTIFACTS: - process.env.CODRA_USE_ARTIFACTS || - (expectedNative.length > 0 ? '1' : process.env.CODRA_USE_ARTIFACTS), - }, + env: packagingEnv(expectedNative), }); const lines = output.split('\n'); diff --git a/packages/codra-npm-cli/scripts/prepack.js b/packages/codra-npm-cli/scripts/prepack.js index 15917a3..06517e1 100644 --- a/packages/codra-npm-cli/scripts/prepack.js +++ b/packages/codra-npm-cli/scripts/prepack.js @@ -33,13 +33,67 @@ function shouldUseArtifacts() { return TARGET_ARTIFACTS.some((name) => fs.existsSync(path.join(dir, name))); } +function installedNativePlatformKeys() { + const nativeRoot = path.join(packageRoot, 'bin', 'native'); + if (!fs.existsSync(nativeRoot)) { + return []; + } + + return fs.readdirSync(nativeRoot).filter((key) => { + const name = key.startsWith('win32') ? 'codra.exe' : 'codra'; + return fs.existsSync(path.join(nativeRoot, key, name)); + }); +} + +function shouldSkipArtifactRebuild() { + if (!shouldUseArtifacts()) { + return false; + } + + const installed = installedNativePlatformKeys(); + if (installed.length === 0) { + return false; + } + + // CI already ran build:from-artifacts; avoid a strict second pass during partial dry runs. + if (process.env.CODRA_ALLOW_PARTIAL_BINARIES === '1') { + return true; + } + + return false; +} + +function packagingEnv() { + const env = { ...process.env }; + const keys = [ + 'CODRA_USE_ARTIFACTS', + 'CODRA_ARTIFACTS_DIR', + 'CODRA_ALLOW_PARTIAL_BINARIES', + 'CODRA_EXPECT_PLATFORMS', + 'CODRA_EXPECT_ALL_PLATFORMS', + ]; + for (const key of keys) { + if (process.env[key] !== undefined) { + env[key] = process.env[key]; + } + } + return env; +} + function main() { + if (shouldSkipArtifactRebuild()) { + console.log( + `[prepack] skip rebuild (${installedNativePlatformKeys().join(', ')} already in bin/native/)`, + ); + return; + } + const script = shouldUseArtifacts() ? path.join(__dirname, 'build-platform-binaries.js') : path.join(__dirname, 'build.js'); console.log(`[prepack] running ${path.basename(script)}`); - execSync(`node "${script}"`, { cwd: packageRoot, stdio: 'inherit' }); + execSync(`node "${script}"`, { cwd: packageRoot, stdio: 'inherit', env: packagingEnv() }); } main(); \ No newline at end of file