@indiegems/gem-web-sdk 5.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * gem-sdk-stamp — record which gem-web-sdk a game build carries.
4
+ *
5
+ * gem-sdk-stamp <build-directory>
6
+ *
7
+ * Run it after your bundler, on the directory you upload:
8
+ *
9
+ * "build": "vite build && gem-sdk-stamp dist"
10
+ *
11
+ * It writes one file, `gem-web-sdk.json`, at the root of that directory:
12
+ *
13
+ * { "schema_version": 1, "sdk_version": "1.2.3" }
14
+ *
15
+ * WHY YOU NEED IT. A published web build has to say which SDK version it was made
16
+ * against, so that a game left on a version the platform no longer supports can
17
+ * be found and its author told. Your bundle already carries the version inside
18
+ * it, but only your bundler knows which of its output files that ended up in, so
19
+ * finding it there means reading all of them. This file is at the same path for
20
+ * every game, which is the whole reason it exists.
21
+ *
22
+ * THE VERSION IS NOT A VALUE YOU PASS. It is read from the copy of the package
23
+ * this command was installed as — the same copy your bundler compiled into the
24
+ * build — so the two cannot disagree, and upgrading the dependency is the only
25
+ * thing that changes it.
26
+ *
27
+ * SAFE TO RUN TWICE. It replaces whatever it finds, which is what makes it
28
+ * correct in a build directory that is reused rather than cleaned.
29
+ */
30
+
31
+ import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
32
+ import { dirname, join } from 'node:path';
33
+ import { argv, exit, stderr, stdout } from 'node:process';
34
+ import { fileURLToPath } from 'node:url';
35
+
36
+ /** The name a reader looks for. Fixed, because a path nobody can name is no better than a search. */
37
+ const STAMP_FILE = 'gem-web-sdk.json';
38
+
39
+ const USAGE =
40
+ 'usage: gem-sdk-stamp <build-directory>\n' +
41
+ ' The directory your bundler wrote and you upload, e.g. "vite build && gem-sdk-stamp dist".';
42
+
43
+ function fail(message) {
44
+ stderr.write(`gem-sdk-stamp: ${message}\n`);
45
+ exit(1);
46
+ }
47
+
48
+ function main() {
49
+ const target = argv[2];
50
+ if (target === undefined || target === '' || target.startsWith('-')) {
51
+ fail(`no build directory given.\n${USAGE}`);
52
+ return;
53
+ }
54
+
55
+ let stats;
56
+ try {
57
+ stats = statSync(target);
58
+ } catch {
59
+ fail(`${target} does not exist. Run this after your build, on the directory it wrote.`);
60
+ return;
61
+ }
62
+ if (!stats.isDirectory()) {
63
+ fail(`${target} is a file. This takes the build directory you upload, not a file inside it.`);
64
+ return;
65
+ }
66
+
67
+ // An empty directory is the mistake of running this BEFORE the build rather
68
+ // than after it. Left alone it succeeds, and the upload then carries a version
69
+ // for a build that was never there.
70
+ const contents = readdirSync(target).filter((name) => name !== STAMP_FILE);
71
+ if (contents.length === 0) {
72
+ fail(`${target} is empty — nothing has been built into it yet. Run this after your bundler.`);
73
+ return;
74
+ }
75
+
76
+ const own = join(dirname(fileURLToPath(import.meta.url)), '..', 'package.json');
77
+ const version = JSON.parse(readFileSync(own, 'utf8')).version;
78
+ if (typeof version !== 'string' || version === '') {
79
+ fail('the installed gem-web-sdk states no version, so there is nothing to record.');
80
+ return;
81
+ }
82
+
83
+ const stamp = {
84
+ $comment:
85
+ 'Written by gem-sdk-stamp when this build was made. sdk_version is the ' +
86
+ '@indiegems/gem-web-sdk release the build carries. Unknown fields are ignored.',
87
+ schema_version: 1,
88
+ sdk_version: version,
89
+ };
90
+ const path = join(target, STAMP_FILE);
91
+ writeFileSync(path, `${JSON.stringify(stamp, null, 2)}\n`);
92
+ stdout.write(`gem-sdk-stamp: ${path} records gem-web-sdk ${version}\n`);
93
+ }
94
+
95
+ main();