buzzcasting-storage 4.0.0 → 4.1.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,162 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Prepares a StencilJS tenant build for deploy, and refuses one that cannot be served.
4
+ *
5
+ * Every tenant repository (axa, axagroup, chanel, admo, bayer, cplus, sncf) needs the same two
6
+ * things after `stencil build`, and used to carry its own copy of this script to get them. The
7
+ * copies drift: a pattern retired from the CDN in one repo keeps shipping from another. So the
8
+ * check lives here, in the package they all already depend on, and their `postbuild` is a bin
9
+ * call.
10
+ *
11
+ * 1. Rewrite `www/index.html`. The pages are developed against a second Stencil dev server, so
12
+ * `src/index.html` loads `http://localhost:3333/build/buzzcasting-app.esm.js` - the convention
13
+ * every tenant shares. A built `www/` keeps that URL verbatim, so shipping it as-is means every
14
+ * device asks a host that only exists on a developer's machine. Stencil has no conditional HTML
15
+ * and rewriting the source would break `npm start`, so the rewrite happens on the output: dev
16
+ * keeps working, production gets the per-tenant storage path the presentation blade uses.
17
+ *
18
+ * 2. Refuse a page that still points at a dead app bundle - the dev URL, the retired
19
+ * `cdn.buzzcasting.net/components/...` packages, the dead animate.css path. Scoped to the app
20
+ * bundle on purpose: a page may legitimately point somewhere else on localhost.
21
+ *
22
+ * 3. Refuse a build the deploy cannot publish. touchflows-deploy publishes the project and
23
+ * symlinks `current/dist/<slug>` at `storage/app/public/customers/<slug>`; the presentation
24
+ * blade then requests `/storage/customers/<slug>/<slug>.esm.js`, with <slug> taken from the
25
+ * tenant host (`axa`, `axagroup`). Stencil's `dist` target already names the entry after the
26
+ * namespace, so nothing is renamed on the way out - the one thing the build must get right is
27
+ * that `dist/<slug>/<slug>.esm.js` exists.
28
+ *
29
+ * Usage: `buzzcasting-production-assets` in a tenant repo after `stencil build` (cwd = repo root).
30
+ * Exit: 0 ready to publish, 1 something would 404 once deployed.
31
+ */
32
+ import { readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs'
33
+ import { join } from 'node:path'
34
+ import { fileURLToPath } from 'node:url'
35
+
36
+ const WWW = 'www'
37
+ const DIST = 'dist'
38
+ const DEV = 'http://localhost:3333/build/buzzcasting-app.'
39
+ const PROD = '/storage/buzzcasting/buzzcasting-app.'
40
+
41
+ /** Anything that would ship a dead or local-only reference to the app bundle. */
42
+ export const FORBIDDEN = [
43
+ 'localhost:3333/build/buzzcasting-app',
44
+ 'cdn.buzzcasting.net/components/buzzcasting',
45
+ 'cdn.buzzcasting.net/buzzcasting/app',
46
+ 'cdn.buzzcasting.net/components/app/test/',
47
+ 'cdn.buzzcasting.net/components/common/test/',
48
+ 'cdn.buzzcasting.net/components/presentation/test/',
49
+ 'cdn.buzzcasting.net/components/animate.css',
50
+ ]
51
+
52
+ function htmlFiles(dir, out = []) {
53
+ for (const entry of readdirSync(dir)) {
54
+ const path = join(dir, entry)
55
+ if (statSync(path).isDirectory()) htmlFiles(path, out)
56
+ else if (entry.endsWith('.html')) out.push(path)
57
+ }
58
+ return out
59
+ }
60
+
61
+ /**
62
+ * The directory the deploy publishes: `dist/<slug>/<slug>.esm.js`.
63
+ *
64
+ * Exactly one directory under `dist/` may hold an entry named after itself. Zero means the
65
+ * lazy-loader output target changed shape and every embedded widget would 404; more than one is
66
+ * ambiguous, so neither is published silently.
67
+ */
68
+ export function findPublishableBundle(distDir = DIST) {
69
+ let dirs = []
70
+ try {
71
+ dirs = readdirSync(distDir).filter((entry) => statSync(join(distDir, entry)).isDirectory())
72
+ } catch {
73
+ return { bundles: [], dirs: [] }
74
+ }
75
+
76
+ const bundles = dirs.filter((dir) => {
77
+ try {
78
+ return statSync(join(distDir, dir, `${dir}.esm.js`)).isFile()
79
+ } catch {
80
+ return false
81
+ }
82
+ })
83
+
84
+ return { bundles, dirs }
85
+ }
86
+
87
+ /**
88
+ * Run the postbuild over a repository.
89
+ *
90
+ * Never throws for an ordinary "this build is not publishable" outcome: it reports and returns a
91
+ * non-zero exit code, so a failing build reads as a build failure rather than a crash.
92
+ */
93
+ export function productionAssets({ cwd = process.cwd(), log = console.log, error = console.error } = {}) {
94
+ let failures = 0
95
+
96
+ let files = []
97
+ try {
98
+ files = htmlFiles(join(cwd, WWW))
99
+ } catch {
100
+ files = []
101
+ }
102
+
103
+ if (files.length === 0) {
104
+ error(`production-assets: no HTML under ${WWW}/ - run stencil build first`)
105
+ return { exitCode: 1, rewritten: 0, files: 0 }
106
+ }
107
+
108
+ let rewritten = 0
109
+ for (const file of files) {
110
+ const before = readFileSync(file, 'utf8')
111
+ const after = before.split(DEV).join(PROD)
112
+ if (after !== before) {
113
+ writeFileSync(file, after)
114
+ rewritten++
115
+ }
116
+
117
+ for (const bad of FORBIDDEN) {
118
+ if (after.includes(bad)) {
119
+ error(`production-assets: ${file} still references ${bad}`)
120
+ failures++
121
+ }
122
+ }
123
+ }
124
+
125
+ const { bundles, dirs } = findPublishableBundle(join(cwd, DIST))
126
+ if (bundles.length !== 1) {
127
+ error(
128
+ `production-assets: expected exactly one ${DIST}/<slug>/<slug>.esm.js, found ${bundles.length}` +
129
+ (dirs.length ? ` (directories: ${dirs.join(', ')})` : ` (no ${DIST}/ - run stencil build first)`),
130
+ )
131
+ failures++
132
+ } else {
133
+ log(
134
+ `production-assets: publishable bundle ${DIST}/${bundles[0]}/${bundles[0]}.esm.js -> /storage/customers/${bundles[0]}/`,
135
+ )
136
+ }
137
+
138
+ log(`production-assets: ${rewritten}/${files.length} file(s) rewritten to ${PROD}...`)
139
+
140
+ return { exitCode: failures === 0 ? 0 : 1, rewritten, files: files.length }
141
+ }
142
+
143
+ /**
144
+ * Was this file run, or just imported for its tests?
145
+ *
146
+ * `node_modules/.bin` shims (and pnpm's store links) reach the script through a symlink, so the
147
+ * argv path and `import.meta.url` differ textually while naming the same file. Comparing them
148
+ * directly made the bin a silent no-op when a tenant's `postbuild` called it - a guard that
149
+ * passes without checking anything, which is worse than one that fails.
150
+ */
151
+ function invokedDirectly() {
152
+ if (process.argv[1] === undefined) return false
153
+ try {
154
+ return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))
155
+ } catch {
156
+ return false
157
+ }
158
+ }
159
+
160
+ if (invokedDirectly()) {
161
+ process.exit(productionAssets().exitCode)
162
+ }
@@ -3,7 +3,7 @@ import p from "ndjson-readablestream";
3
3
  import m from "dexie";
4
4
  import { get as h, set as g } from "idb-keyval";
5
5
  // package.json
6
- var _ = "4.0.0", v = class {
6
+ var _ = "4.1.0", v = class {
7
7
  options;
8
8
  url;
9
9
  constructor(e) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "buzzcasting-storage",
3
3
  "type": "module",
4
- "version": "4.0.0",
4
+ "version": "4.1.0",
5
5
  "packageManager": "pnpm@11.5.2",
6
6
  "private": false,
7
7
  "repository": {
@@ -23,8 +23,12 @@
23
23
  "types": "./dist/buzzcasting-storage.d.ts",
24
24
  "typings": "./dist/buzzcasting-storage.d.ts",
25
25
  "files": [
26
- "dist"
26
+ "dist",
27
+ "bin"
27
28
  ],
29
+ "bin": {
30
+ "buzzcasting-production-assets": "./bin/production-assets.mjs"
31
+ },
28
32
  "scripts": {
29
33
  "start": "vite",
30
34
  "docs": "typedoc",
@@ -32,6 +36,9 @@
32
36
  "changelog": "changelogen --release",
33
37
  "release": "pnpm test && pnpm run build && changelogen --release && git push --follow-tags",
34
38
  "lint": "eslint . --fix",
39
+ "check:credentials": "bash scripts/check-git-credentials.sh",
40
+ "check:credentials:history": "bash scripts/check-git-credentials.sh --history",
41
+ "install:gitleaks": "bash scripts/install-gitleaks.sh",
35
42
  "test": "vitest --run",
36
43
  "test:watch": "vitest",
37
44
  "test:coverage": "vitest --coverage"