Skip to content

Commit

Permalink
build: provide version at runtime
Browse files Browse the repository at this point in the history
and in title of the deployed demo
  • Loading branch information
davidenke committed Nov 7, 2024
1 parent ee397db commit b55eb6b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:

- name: Install dependencies
run: |
pnpm install --global release-it @release-it/conventional-changelog
pnpm install --global tsx release-it @release-it/conventional-changelog
cargo install cargo-bump
- name: Prepare release
Expand All @@ -92,6 +92,7 @@ jobs:
--hooks.before:release='cargo bump --manifest-path src-tauri/Cargo.toml ${version}' \
--hooks.before:release='cargo update --manifest-path src-tauri/Cargo.toml --precise ${version} live-protocol' \
--hooks.before:release='git add src-tauri/tauri.conf.json src-tauri/Cargo.toml src-tauri/Cargo.lock'
--hooks.before:release='tsx scripts/set-version.ts ${version}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down Expand Up @@ -123,6 +124,7 @@ jobs:
cache: pnpm
- run: pnpm install
- run: ./node_modules/.bin/vite build --mode detached
- run: pnpm dlx tsx scripts/set-version.ts ${{ needs.prepare.outputs.version }}
- uses: actions/upload-pages-artifact@v3
with:
path: demo
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Live Protocol Demo</title>
<title>Live Protocol ##VERSION## Demo</title>
<link rel="stylesheet" href="src/index.css" />
<script type="module" src="/src/index.ts" defer></script>
</head>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"eslint-plugin-simple-import-sort": "12.1.1",
"eslint-plugin-unused-imports": "4.1.4",
"eslint-plugin-wc": "2.2.0",
"fast-glob": "3.3.2",
"jiti": "2.3.3",
"postcss": "8.4.47",
"postject": "1.0.0-alpha.6",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

60 changes: 60 additions & 0 deletions scripts/set-version.ts
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 modified src-tauri/icons/icon.icns
Binary file not shown.

0 comments on commit b55eb6b

Please sign in to comment.