Skip to content

Commit 328948b

Browse files
Improve sw versioning, try flags with stage
1 parent d74a44f commit 328948b

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ $(BUILD)/micropython.js: $(OBJ) jshal.js simulator-js
146146
$(Q)emcc $(LDFLAGS) -o $(BUILD)/firmware.js $(OBJ) $(JSFLAGS)
147147

148148
simulator-js:
149-
npx esbuild ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text
150-
npx esbuild --define:process.env.version=$$(cat ../package.json | jq .version) ./sw.ts --bundle --outfile=$(BUILD)/sw.js
149+
npx esbuild '--define:process.env.STAGE="$(STAGE)"' ./simulator.ts --bundle --outfile=$(BUILD)/simulator.js --loader:.svg=text
150+
npx esbuild --define:process.env.VERSION=$$(npm pkg get version) ./sw.ts --bundle --outfile=$(BUILD)/sw.js
151151

152152
include $(TOP)/py/mkrules.mk
153153

src/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Stage = "local" | "REVIEW" | "STAGING" | "PRODUCTION";
2+
3+
export const stage = (process.env.STAGE || "local") as Stage;

src/flags.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Stage, stage as stageFromEnvironment } from "./environment";
2+
3+
/**
4+
* A union of the flag names (alphabetical order).
5+
*/
6+
export type Flag =
7+
/**
8+
* Enables service worker registration.
9+
*
10+
* Registers the service worker and enables offline use.
11+
*/
12+
"sw";
13+
14+
interface FlagMetadata {
15+
defaultOnStages: Stage[];
16+
name: Flag;
17+
}
18+
19+
const allFlags: FlagMetadata[] = [{ name: "sw", defaultOnStages: [] }];
20+
21+
type Flags = Record<Flag, boolean>;
22+
23+
const flagsForParams = (stage: Stage, params: URLSearchParams) => {
24+
const enableFlags = new Set(params.getAll("flag"));
25+
const allFlagsDefault = enableFlags.has("none")
26+
? false
27+
: enableFlags.has("*")
28+
? true
29+
: undefined;
30+
return Object.fromEntries(
31+
allFlags.map((f) => [
32+
f.name,
33+
isEnabled(f, stage, allFlagsDefault, enableFlags.has(f.name)),
34+
])
35+
) as Flags;
36+
};
37+
38+
const isEnabled = (
39+
f: FlagMetadata,
40+
stage: Stage,
41+
allFlagsDefault: boolean | undefined,
42+
thisFlagOn: boolean
43+
): boolean => {
44+
if (thisFlagOn) {
45+
return true;
46+
}
47+
if (allFlagsDefault !== undefined) {
48+
return allFlagsDefault;
49+
}
50+
return f.defaultOnStages.includes(stage);
51+
};
52+
53+
export const flags: Flags = (() => {
54+
const params = new URLSearchParams(window.location.search);
55+
return flagsForParams(stageFromEnvironment, params);
56+
})();

src/simulator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
createMessageListener,
88
Notifications,
99
} from "./board";
10+
import { flags } from "./flags";
1011

1112
declare global {
1213
interface Window {
@@ -48,7 +49,9 @@ function initServiceWorker() {
4849
});
4950
}
5051

51-
initServiceWorker();
52+
if (flags.sw) {
53+
initServiceWorker();
54+
}
5255
const fs = new FileSystem();
5356
const board = createBoard(new Notifications(window.parent), fs);
5457
window.addEventListener("message", createMessageListener(board));

src/sw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare const self: ServiceWorkerGlobalScope;
55
declare const clients: Clients;
66

77
const assets = ["simulator.html", "build/simulator.js", "build/firmware.js"];
8-
const cacheName = `simulator-${process.env.version}`;
8+
const cacheName = `simulator-${process.env.VERSION}`;
99

1010
self.addEventListener("install", (event) => {
1111
console.log("Installing simulator service worker...");

0 commit comments

Comments
 (0)