-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
and in title of the deployed demo
- Loading branch information
Showing
6 changed files
with
72 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,60 @@ | ||
// pnpm dlx tsx scripts/set-version.ts | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { parseArgs } from 'node:util'; | ||
|
||
import glob from 'fast-glob'; | ||
|
||
import pkg from '../package.json' assert { type: 'json' }; | ||
import { getFileName } from '../src/utils/file.utils.js'; | ||
|
||
// just call `npm run set:version` to use the package version | ||
// or `npm run set:version <version>` to use a custom version | ||
const { positionals } = parseArgs({ allowPositionals: true }); | ||
const version = positionals[0] ?? pkg.version; | ||
|
||
// prepare version banner | ||
const intro = '// set live protocol version globally'; | ||
const banner = `${intro} | ||
let lp = 'live-protocol'; | ||
if (!window.enke) window.enke = {}; | ||
if (!window.enke[lp]) window.enke[lp] = {}; | ||
if (window.enke[lp].version) { | ||
console.warn(\`> \${lp} ${version}: Another version (\${window.enke[lp].version}) has already been loaded.\`); | ||
} else window.enke[lp].version = '${version}'; | ||
`; | ||
|
||
// prepend version banner to given bundle | ||
async function prependVersion(path: string) { | ||
const { name: file } = getFileName(path); | ||
const content = await readFile(path, 'utf8'); | ||
|
||
// update dist file by adding banner if not already present | ||
if (!content.startsWith(intro)) { | ||
console.info(`> Updating ${file} with version banner ${version}`); | ||
await writeFile(path, banner + content); | ||
} else { | ||
console.info(`> Version banner already present in ${file}`); | ||
} | ||
} | ||
|
||
// replace version placeholders in given file | ||
async function replaceVersion(path: string, placeholder = '##VERSION##') { | ||
const { name: file } = getFileName(path); | ||
const content = await readFile(path, 'utf8'); | ||
|
||
// replace all occurrences of search string | ||
const pattern = new RegExp(placeholder, 'g'); | ||
await writeFile(path, content.replace(pattern, version)); | ||
console.info(`> Replaced version placeholders in ${file} with ${version}`); | ||
} | ||
|
||
async function invoke(pattern: string, fn: (path: string) => Promise<void>) { | ||
const cwd = fileURLToPath(new URL('..', import.meta.url)); | ||
for await (const path of await glob(pattern, { onlyFiles: true, absolute: true, cwd })) { | ||
await fn(path); | ||
} | ||
} | ||
|
||
await invoke('dist/assets/index-*.js', prependVersion); | ||
await invoke('dist/index.html', replaceVersion); |
Binary file not shown.