Skip to content

Commit 2c268a4

Browse files
Ross Fenningavengerpenguin
authored andcommitted
Allow hard-coded "sheet" URL to be passed at build time
This PR changes a few small things so I'd happily take some feedback and chat about whether they are all a net good for the project. I've done them for myself, but perhaps it improves things for others too. In summary: 1. The webpack build allows env vars (e.g. from Docker build args) to set the sheet ID (URL) and name at build time 2. The code itself is refactored a little to allow dependency-injection of the sheet URL 3. The Dockerfile does the webpack build etc. at build time 4. The Docker build is now multistage which makes it simpler Rationale --------- I liked the idea of being able to run the project as it is while also being able to run a build, even from my own Dockerfile elsewhere, that spits out an HTML file that is pre-baked with the radar data. This would allow deploying it as part of a wider static site, e.g. on Github pages. It seems a reasonable code architecture to hoist out mentions of `window` to the top-level file and inject that info in such that all JS files from `factory.js` are abstracted away from the fact that info came from a query string. Docker Build Changes -------------------- The Docker container running the bulk of the build as a `CMD` on start seemed odd when -- as far as I can see -- the build output is entirely a function of source files alone. Maybe there's more flexibility intended around being able to swap out env vars without having to rebuild the container? I technically do not need the changes for the sheet URL injection, but I'd have to add something to pass the build args into the environment at container runtime. Small Tweaks ------------ Some stuff I noticed along the way: 1. There's two webpack configs but they're _very_ close so I hoisted up the `entry` part to the common file. There may be a reason it is the way it is. 2. I kept finding it odd to have lots of references to "sheet" and "Google Sheet Input" throughout when it's clearly been extended to allow generic JSON and CSV outside of Google sheets. I suspect this is historic but I changed the name of the top export from factory in the hope this is a positive change in the right direction. I can revert if needed though. 3. For JSON and CSV input, the `h1` title just uses the file name. Since we can now inject I've allowed a SHEET_NAME to be set at build time to override that behaviour. Tests ----- I've checked no tests fail, but not added any since the JS changes are _meant_ to be a refactor but perhaps this change warrants an e2e test to verify the new functionality made available.
1 parent 9e1f13a commit 2c268a4

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

Dockerfile

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
FROM nginx:1.23.0
2-
3-
RUN apt-get update && apt-get upgrade -y
4-
5-
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
6-
RUN apt-get install -y nodejs
1+
FROM node:18 AS build
72

8-
RUN \
9-
apt-get install -y \
10-
libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 \
11-
libxss1 libasound2 libxtst6 xauth xvfb g++ make
3+
ARG SHEET_ID
4+
ARG SHEET_NAME
125

136
WORKDIR /src/build-your-own-radar
147
COPY package.json ./
@@ -17,7 +10,10 @@ RUN npm ci
1710

1811
COPY . ./
1912

20-
# Override parent node image's entrypoint script (/usr/local/bin/docker-entrypoint.sh),
21-
# which tries to run CMD as a node command
22-
ENTRYPOINT []
23-
CMD ["./build_and_start_nginx.sh"]
13+
RUN npm test && npm run build:prod
14+
15+
FROM nginx:1.23.0
16+
17+
COPY --from=build /src/build-your-own-radar/dist /opt/build-your-own-radar
18+
COPY default.template /etc/nginx/conf.d/default.conf
19+
CMD nginx -g 'daemon off;'

src/site.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,14 @@ require('./analytics.js')
66
const Factory = require('./util/factory')
77

88
Factory().build()
9+
10+
if (process.env.SHEET_ID) {
11+
Factory().build(process.env.SHEET_ID, process.env.SHEET_NAME)
12+
} else {
13+
const QueryParams = require('./util/queryParamProcessor')
14+
15+
var queryString = window.location.href.match(/sheetId(.*)/)
16+
var queryParams = queryString ? QueryParams(queryString[0]) : {}
17+
18+
Factory().build(queryParams.sheetId, queryParams.sheetName)
19+
}

src/util/factory.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ const CSVDocument = function (url) {
248248
return self
249249
}
250250

251-
const JSONFile = function (url) {
251+
const JSONFile = function (url, title) {
252252
var self = {}
253253

254254
self.build = function () {
@@ -284,12 +284,6 @@ const JSONFile = function (url) {
284284
return self
285285
}
286286

287-
const DomainName = function (url) {
288-
var search = /.+:\/\/([^\\/]+)/
289-
var match = search.exec(decodeURIComponent(url.replace(/\+/g, ' ')))
290-
return match == null ? null : match[1]
291-
}
292-
293287
const FileName = function (url) {
294288
var search = /([^\\/]+)$/
295289
var match = search.exec(decodeURIComponent(url.replace(/\+/g, ' ')))

webpack.common.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if (env) {
1515
}
1616

1717
const common = ['./src/common.js']
18+
const main = ['./src/site.js']
1819

1920
const ASSET_PATH = process.env.ASSET_PATH || '/'
2021

@@ -33,13 +34,16 @@ const plugins = [
3334
'process.env.RINGS': JSON.stringify(process.env.RINGS),
3435
'process.env.QUADRANTS': JSON.stringify(process.env.QUADRANTS),
3536
'process.env.ADOBE_LAUNCH_SCRIPT_URL': JSON.stringify(process.env.ADOBE_LAUNCH_SCRIPT_URL),
37+
'process.env.SHEET_ID': JSON.stringify(process.env.SHEET_ID),
38+
'process.env.SHEET_NAME': JSON.stringify(process.env.SHEET_NAME)
3639
}),
3740
]
3841

3942
module.exports = {
4043
context: __dirname,
4144
entry: {
42-
common: common,
45+
common,
46+
main,
4347
},
4448
output: {
4549
path: buildPath,

0 commit comments

Comments
 (0)