Skip to content

Commit

Permalink
feat(start): version link
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Aug 27, 2024
1 parent 0ecf392 commit 3a04fce
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
32 changes: 27 additions & 5 deletions scripts/build-html.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const { DEV, inputs, outputs, styles, scripts } = require('./const.cjs');
const {
DEV,
github,
inputs,
outputs,
styles,
scripts,
} = require('./const.cjs');
const HTMLParser = require('node-html-parser');
const { minify } = require('html-minifier-terser');
const { html: terserOptions } = require('./terser.config.cjs');
Expand All @@ -14,12 +21,27 @@ const html = HTMLParser.parse(

const i = DEV ? 0 : 1;
const setCss = (id, /** @type {keyof styles} */ type) =>
html.getElementById(id)?.setAttribute('href', styles[type][i]).removeAttribute('id');
html
.getElementById(id)
?.setAttribute('href', styles[type][i])
.removeAttribute('id');
const setScript = (id, /** @type {keyof scripts} */ type) =>
html.getElementById(id)?.setAttribute('src', scripts[type][i]).removeAttribute('id');
const setVersion = (version) => html.getElementById('version')?.set_content(version).removeAttribute('id');
html
.getElementById(id)
?.setAttribute('src', scripts[type][i])
.removeAttribute('id');

setVersion(version(true));
const setVersion = () => {
const v = version();
html.getElementById('github')
?.setAttribute('href', github)
.removeAttribute('id');
html.getElementById('version')
?.set_content(v.text)
.setAttribute('href', github + (v.dirty ? '' : `/tree/${v.commit}`))
.removeAttribute('id');
};
setVersion();

setCss('css-player', 'player');
setCss('css-ccl', 'ccl');
Expand Down
4 changes: 3 additions & 1 deletion scripts/const.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {

const DEV = process.env.NODE_ENV !== 'production';

const github = 'https://github.com/auioc/BilibiliLocalPlayerH5';

const inputs = {
html: src('html/index.html'),
};
Expand Down Expand Up @@ -43,4 +45,4 @@ const scripts = {
],
};

module.exports = { DEV, inputs, outputs, styles, scripts };
module.exports = { DEV, github, inputs, outputs, styles, scripts };
25 changes: 19 additions & 6 deletions scripts/utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ const fs = require('fs');
const _execSync = require('child_process').execSync;

const execSync = (cmd) => _execSync(cmd).toString().trim();
const commitHash = (short = false) => {
const hash = execSync('git rev-parse --verify HEAD');
return short ? hash.slice(0, 8) : hash;
};
const commitHash = () => execSync('git rev-parse --verify HEAD');
const branch = () => execSync('git branch --show-current');
const isDirty = () => execSync('git status --short').length !== 0;
const version = (short = false) => `${branch()}@${commitHash(short)}${isDirty() ? '(dirty)' : ''}`;
const version = () => {
const r = {
branch: branch(),
commit: commitHash(),
dirty: isDirty(),
text: '',
};
r.text = `${r.branch}@${r.commit.slice(0, 8)}${r.dirty ? '(dirty)' : ''}`;
return r;
};

const srcPath = (...p) => path.resolve(__dirname, '../src', ...p);
const buildPath = (...p) => path.resolve(__dirname, '../build', ...p);
Expand All @@ -24,4 +30,11 @@ function readFile(file) {
return fs.readFileSync(file, 'utf-8');
}

module.exports = { version, srcPath, buildPath, publicPath, readFile, writeFile };
module.exports = {
version,
srcPath,
buildPath,
publicPath,
readFile,
writeFile,
};
5 changes: 3 additions & 2 deletions src/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
<th colspan="2">
BilibiliLocalPlayerH5
<div>
<a href="https://github.com/auioc/BilibiliLocalPlayerH5">GitHub</a>&nbsp;&middot;&nbsp;
<span id="version">{version}</span>
<a id="github" target="_blank">GitHub</a>
<span>&nbsp;&middot;&nbsp;</span>
<a id="version" target="_blank">{version}</a>
</div>
</th>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion src/style/_start.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tr > td:nth-child(1) {
th {
padding: 0.5em;

& > div {
& > div * {
font-weight: lighter;
font-size: smaller;
color: var(--border-color);
Expand Down

0 comments on commit 3a04fce

Please sign in to comment.