-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: report correct package version in esm build (#2569)
- Loading branch information
1 parent
cafed1f
commit 521775c
Showing
5 changed files
with
66 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env node | ||
|
||
import { exec } from 'node:child_process'; | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import glob from 'glob'; | ||
import { promisify } from 'node:util'; | ||
import getPackageVersion from "./getPackageVersion.mjs"; | ||
|
||
const execAsync = promisify(exec); | ||
|
||
const version = getPackageVersion(); | ||
|
||
const bundleEsm = async () => { | ||
// Run TypeScript compiler | ||
console.log('Running TypeScript compiler...'); | ||
await execAsync('tsc'); | ||
|
||
// Replace version string in generated files | ||
console.log('Replacing version strings...'); | ||
const files = glob.glob.sync('dist/**/*.js'); | ||
await Promise.all( | ||
files.map(async (file) => { | ||
const content = await readFile(file, 'utf8'); | ||
const newContent = content.replace(/__STREAM_CHAT_REACT_VERSION__/g, version); | ||
await writeFile(file, newContent); | ||
}) | ||
); | ||
|
||
console.log('ESM build complete'); | ||
}; | ||
|
||
bundleEsm().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { dirname, resolve } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
// import.meta.dirname is not available before Node 20 | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
const packageJson = await import(resolve(__dirname, '../package.json'), { | ||
assert: { type: 'json' }, | ||
}); | ||
|
||
// Get the latest version so that magic string __STREAM_CHAT_REACT_VERSION__ can be replaced with it in the source code (used for reporting purposes) | ||
export default function getPackageVersion() { | ||
let version; | ||
// During release, use the version being released | ||
// see .releaserc.json where the `NEXT_VERSION` env variable is set | ||
if (process.env.NEXT_VERSION) { | ||
version = process.env.NEXT_VERSION; | ||
} else { | ||
// Otherwise use the latest git tag | ||
try { | ||
version = execSync('git describe --tags --abbrev=0').toString().trim(); | ||
} catch (error) { | ||
console.error(error); | ||
console.warn('Could not get latest version from git tags, falling back to package.json'); | ||
version = packageJson.default.version; | ||
} | ||
} | ||
console.log(`Determined the build package version to be ${version}`); | ||
return version; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters