Skip to content

Commit 1c700aa

Browse files
authored
feat(config-json): Only either bundle or load from remote (#291)
1 parent 4b54be6 commit 1c700aa

15 files changed

+127
-74
lines changed

.github/workflows/next-deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232
echo "{\"latestTag\": \"$(git rev-parse --short $GITHUB_SHA)\", \"isCommit\": true}" > assets/release.json
3333
- name: Build Project Artifacts
3434
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
35+
env:
36+
CONFIG_JSON_SOURCE: BUNDLED
3537
- run: pnpm build-storybook
3638
- name: Copy playground files
3739
run: |

.github/workflows/preview.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ jobs:
6161
echo "{\"latestTag\": \"$(git rev-parse --short ${{ github.event.pull_request.head.sha }})\", \"isCommit\": true}" > assets/release.json
6262
- name: Build Project Artifacts
6363
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
64+
env:
65+
CONFIG_JSON_SOURCE: BUNDLED
6466
- run: pnpm build-storybook
6567
- name: Copy playground files
6668
run: |

.github/workflows/publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ jobs:
3030
env:
3131
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3232
- run: vercel build --token=${{ secrets.VERCEL_TOKEN }} --prod
33+
env:
34+
CONFIG_JSON_SOURCE: BUNDLED
3335
- run: pnpm build-storybook
3436
- name: Copy playground files
3537
run: |

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ RUN npm i -g pnpm@9.0.4
99
# Build arguments
1010
ARG DOWNLOAD_SOUNDS=false
1111
ARG DISABLE_SERVICE_WORKER=false
12+
ARG CONFIG_JSON_SOURCE=REMOTE
1213
# TODO need flat --no-root-optional
1314
RUN node ./scripts/dockerPrepare.mjs
1415
RUN pnpm i
@@ -22,8 +23,8 @@ RUN if [ "$DOWNLOAD_SOUNDS" = "true" ] ; then node scripts/downloadSoundsMap.mjs
2223
# ENTRYPOINT ["pnpm", "run", "run-all"]
2324

2425
# only for prod
25-
RUN GITHUB_REPOSITORY=zardoy/minecraft-web-client \
26-
DISABLE_SERVICE_WORKER=$DISABLE_SERVICE_WORKER \
26+
RUN DISABLE_SERVICE_WORKER=$DISABLE_SERVICE_WORKER \
27+
CONFIG_JSON_SOURCE=$CONFIG_JSON_SOURCE \
2728
pnpm run build
2829

2930
# ---- Run Stage ----

rsbuild.config.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ const disableServiceWorker = process.env.DISABLE_SERVICE_WORKER === 'true'
2525
let releaseTag
2626
let releaseLink
2727
let releaseChangelog
28+
let githubRepositoryFallback
2829

2930
if (fs.existsSync('./assets/release.json')) {
3031
const releaseJson = JSON.parse(fs.readFileSync('./assets/release.json', 'utf8'))
3132
releaseTag = releaseJson.latestTag
3233
releaseLink = releaseJson.isCommit ? `/commit/${releaseJson.latestTag}` : `/releases/${releaseJson.latestTag}`
3334
releaseChangelog = releaseJson.changelog?.replace(/<!-- bump-type:[\w]+ -->/, '')
35+
githubRepositoryFallback = releaseJson.repository
3436
}
3537

3638
const configJson = JSON.parse(fs.readFileSync('./config.json', 'utf8'))
@@ -41,6 +43,8 @@ if (dev) {
4143
configJson.defaultProxy = ':8080'
4244
}
4345

46+
const configSource = process.env.CONFIG_JSON_SOURCE || 'REMOTE'
47+
4448
// base options are in ./renderer/rsbuildSharedConfig.ts
4549
const appConfig = defineConfig({
4650
html: {
@@ -66,13 +70,13 @@ const appConfig = defineConfig({
6670
'process.env.BUILD_VERSION': JSON.stringify(!dev ? buildingVersion : 'undefined'),
6771
'process.env.MAIN_MENU_LINKS': JSON.stringify(process.env.MAIN_MENU_LINKS),
6872
'process.env.GITHUB_URL':
69-
JSON.stringify(`https://github.com/${process.env.GITHUB_REPOSITORY || `${process.env.VERCEL_GIT_REPO_OWNER}/${process.env.VERCEL_GIT_REPO_SLUG}`}`),
73+
JSON.stringify(`https://github.com/${process.env.GITHUB_REPOSITORY || `${process.env.VERCEL_GIT_REPO_OWNER}/${process.env.VERCEL_GIT_REPO_SLUG}` || githubRepositoryFallback}`),
7074
'process.env.DEPS_VERSIONS': JSON.stringify({}),
7175
'process.env.RELEASE_TAG': JSON.stringify(releaseTag),
7276
'process.env.RELEASE_LINK': JSON.stringify(releaseLink),
7377
'process.env.RELEASE_CHANGELOG': JSON.stringify(releaseChangelog),
7478
'process.env.DISABLE_SERVICE_WORKER': JSON.stringify(disableServiceWorker),
75-
'process.env.INLINED_APP_CONFIG': JSON.stringify(configJson),
79+
'process.env.INLINED_APP_CONFIG': JSON.stringify(configSource === 'BUNDLED' ? configJson : null),
7680
},
7781
},
7882
server: {
@@ -109,7 +113,9 @@ const appConfig = defineConfig({
109113
fs.copyFileSync('./assets/release.json', './dist/release.json')
110114
}
111115

112-
fs.writeFileSync('./dist/config.json', JSON.stringify(configJson), 'utf8')
116+
if (configSource === 'REMOTE') {
117+
fs.writeFileSync('./dist/config.json', JSON.stringify(configJson), 'utf8')
118+
}
113119
if (fs.existsSync('./generated/sounds.js')) {
114120
fs.copyFileSync('./generated/sounds.js', './dist/sounds.js')
115121
}

scripts/dockerPrepare.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@ import path from 'path'
44
import { fileURLToPath } from 'url'
55
import { execSync } from 'child_process'
66

7-
// write release tag
7+
// Get repository from git config
8+
const getGitRepository = () => {
9+
try {
10+
const gitConfig = fs.readFileSync('.git/config', 'utf8')
11+
const originUrlMatch = gitConfig.match(/\[remote "origin"\][\s\S]*?url = .*?github\.com[:/](.*?)(\.git)?\n/m)
12+
if (originUrlMatch) {
13+
return originUrlMatch[1]
14+
}
15+
} catch (err) {
16+
console.warn('Failed to read git repository from config:', err)
17+
}
18+
return null
19+
}
20+
21+
// write release tag and repository info
822
const commitShort = execSync('git rev-parse --short HEAD').toString().trim()
9-
fs.writeFileSync('./assets/release.json', JSON.stringify({ latestTag: `${commitShort} (docker)` }), 'utf8')
23+
const repository = getGitRepository()
24+
fs.writeFileSync('./assets/release.json', JSON.stringify({
25+
latestTag: `${commitShort} (docker)`,
26+
repository
27+
}), 'utf8')
1028

1129
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
1230
delete packageJson.optionalDependencies

src/appConfig.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { disabledSettings, options, qsOptions } from './optionsStorage'
2+
import { miscUiState } from './globalState'
3+
import { setLoadingScreenStatus } from './appStatus'
4+
5+
export type AppConfig = {
6+
// defaultHost?: string
7+
// defaultHostSave?: string
8+
defaultProxy?: string
9+
// defaultProxySave?: string
10+
// defaultVersion?: string
11+
peerJsServer?: string
12+
peerJsServerFallback?: string
13+
promoteServers?: Array<{ ip, description, version? }>
14+
mapsProvider?: string
15+
16+
appParams?: Record<string, any> // query string params
17+
18+
defaultSettings?: Record<string, any>
19+
forceSettings?: Record<string, boolean>
20+
// hideSettings?: Record<string, boolean>
21+
allowAutoConnect?: boolean
22+
pauseLinks?: Array<Array<Record<string, any>>>
23+
}
24+
25+
export const loadAppConfig = (appConfig: AppConfig) => {
26+
if (miscUiState.appConfig) {
27+
Object.assign(miscUiState.appConfig, appConfig)
28+
} else {
29+
miscUiState.appConfig = appConfig
30+
}
31+
32+
if (appConfig.forceSettings) {
33+
for (const [key, value] of Object.entries(appConfig.forceSettings)) {
34+
if (value) {
35+
disabledSettings.value.add(key)
36+
// since the setting is forced, we need to set it to that value
37+
if (appConfig.defaultSettings?.[key] && !qsOptions[key]) {
38+
options[key] = appConfig.defaultSettings[key]
39+
}
40+
} else {
41+
disabledSettings.value.delete(key)
42+
}
43+
}
44+
}
45+
}
46+
47+
export const isBundledConfigUsed = !!process.env.INLINED_APP_CONFIG
48+
49+
if (isBundledConfigUsed) {
50+
loadAppConfig(process.env.INLINED_APP_CONFIG as AppConfig ?? {})
51+
} else {
52+
void window.fetch('config.json').then(async res => res.json()).then(c => c, (error) => {
53+
// console.warn('Failed to load optional app config.json', error)
54+
// return {}
55+
setLoadingScreenStatus('Failed to load app config.json', true)
56+
}).then((config: AppConfig) => {
57+
loadAppConfig(config)
58+
})
59+
}

src/appParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AppConfig } from './globalState'
1+
import type { AppConfig } from './appConfig'
22

33
const qsParams = new URLSearchParams(window.location?.search ?? '')
44

src/browserfs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ browserfs.configure({
4141
throw e2
4242
}
4343
showNotification('Failed to access device storage', `Check you have free space. ${e.message}`, true)
44-
miscUiState.appLoaded = true
44+
miscUiState.fsReady = true
4545
miscUiState.singleplayerAvailable = false
4646
})
4747
return
4848
}
4949
await updateTexturePackInstalledState()
50-
miscUiState.appLoaded = true
50+
miscUiState.fsReady = true
5151
miscUiState.singleplayerAvailable = true
5252
})
5353

src/customChannels.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ const registeredJeiChannel = () => {
8585
[
8686
{
8787
name: 'id',
88-
type: 'pstring',
88+
type: ['pstring', { countType: 'i16' }]
8989
},
9090
{
9191
name: 'categoryTitle',
92-
type: 'pstring',
92+
type: ['pstring', { countType: 'i16' }]
9393
},
9494
{
9595
name: 'items',
96-
type: 'pstring',
96+
type: ['pstring', { countType: 'i16' }]
9797
},
9898
]
9999
]

0 commit comments

Comments
 (0)