buzzcasting-storage 4.0.0 → 4.2.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,215 @@
|
|
|
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
|
+
* Tenants built with only a `www` output target (ksa-moh, aramco) have no `dist/<slug>` to check,
|
|
30
|
+
* so they pass `--www <slug>`: the same page rewrite and dead-reference guard, then the bundle
|
|
31
|
+
* check asserts `www/build/<slug>.esm.js` exists and that no page loads another tenant's
|
|
32
|
+
* `/build/<other>.esm.js`. That one is not hypothetical: ksa-moh's page named
|
|
33
|
+
* `/build/axagroup.esm.js` while its own build wrote `/build/ksa-moh.esm.js`, so the widget 404s
|
|
34
|
+
* on a correctly built site.
|
|
35
|
+
*
|
|
36
|
+
* Usage: `buzzcasting-production-assets` in a tenant repo after `stencil build` (cwd = repo root),
|
|
37
|
+
* or `buzzcasting-production-assets --www <slug>` in a www-only tenant.
|
|
38
|
+
* Exit: 0 ready to publish, 1 something would 404 once deployed.
|
|
39
|
+
*/
|
|
40
|
+
import { readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs'
|
|
41
|
+
import { join } from 'node:path'
|
|
42
|
+
import { fileURLToPath } from 'node:url'
|
|
43
|
+
|
|
44
|
+
const WWW = 'www'
|
|
45
|
+
const DIST = 'dist'
|
|
46
|
+
const DEV = 'http://localhost:3333/build/buzzcasting-app.'
|
|
47
|
+
const PROD = '/storage/buzzcasting/buzzcasting-app.'
|
|
48
|
+
|
|
49
|
+
/** Anything that would ship a dead or local-only reference to the app bundle. */
|
|
50
|
+
export const FORBIDDEN = [
|
|
51
|
+
'localhost:3333/build/buzzcasting-app',
|
|
52
|
+
'cdn.buzzcasting.net/components/buzzcasting',
|
|
53
|
+
'cdn.buzzcasting.net/buzzcasting/app',
|
|
54
|
+
'cdn.buzzcasting.net/components/app/test/',
|
|
55
|
+
'cdn.buzzcasting.net/components/common/test/',
|
|
56
|
+
'cdn.buzzcasting.net/components/presentation/test/',
|
|
57
|
+
'cdn.buzzcasting.net/components/animate.css',
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
function isFile(path) {
|
|
61
|
+
try {
|
|
62
|
+
return statSync(path).isFile()
|
|
63
|
+
} catch {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function htmlFiles(dir, out = []) {
|
|
69
|
+
for (const entry of readdirSync(dir)) {
|
|
70
|
+
const path = join(dir, entry)
|
|
71
|
+
if (statSync(path).isDirectory()) htmlFiles(path, out)
|
|
72
|
+
else if (entry.endsWith('.html')) out.push(path)
|
|
73
|
+
}
|
|
74
|
+
return out
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The directory the deploy publishes: `dist/<slug>/<slug>.esm.js`.
|
|
79
|
+
*
|
|
80
|
+
* Exactly one directory under `dist/` may hold an entry named after itself. Zero means the
|
|
81
|
+
* lazy-loader output target changed shape and every embedded widget would 404; more than one is
|
|
82
|
+
* ambiguous, so neither is published silently.
|
|
83
|
+
*/
|
|
84
|
+
export function findPublishableBundle(distDir = DIST) {
|
|
85
|
+
let dirs = []
|
|
86
|
+
try {
|
|
87
|
+
dirs = readdirSync(distDir).filter((entry) => statSync(join(distDir, entry)).isDirectory())
|
|
88
|
+
} catch {
|
|
89
|
+
return { bundles: [], dirs: [] }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const bundles = dirs.filter((dir) => {
|
|
93
|
+
try {
|
|
94
|
+
return statSync(join(distDir, dir, `${dir}.esm.js`)).isFile()
|
|
95
|
+
} catch {
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
return { bundles, dirs }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Run the postbuild over a repository.
|
|
105
|
+
*
|
|
106
|
+
* Never throws for an ordinary "this build is not publishable" outcome: it reports and returns a
|
|
107
|
+
* non-zero exit code, so a failing build reads as a build failure rather than a crash.
|
|
108
|
+
*/
|
|
109
|
+
export function productionAssets({
|
|
110
|
+
cwd = process.cwd(),
|
|
111
|
+
log = console.log,
|
|
112
|
+
error = console.error,
|
|
113
|
+
bundle = 'dist',
|
|
114
|
+
slug,
|
|
115
|
+
} = {}) {
|
|
116
|
+
let failures = 0
|
|
117
|
+
|
|
118
|
+
if (bundle === 'www' && !slug) {
|
|
119
|
+
error('production-assets: --www needs the tenant slug, e.g. `buzzcasting-production-assets --www ksa-moh`')
|
|
120
|
+
return { exitCode: 1, rewritten: 0, files: 0 }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let files = []
|
|
124
|
+
try {
|
|
125
|
+
files = htmlFiles(join(cwd, WWW))
|
|
126
|
+
} catch {
|
|
127
|
+
files = []
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (files.length === 0) {
|
|
131
|
+
error(`production-assets: no HTML under ${WWW}/ - run stencil build first`)
|
|
132
|
+
return { exitCode: 1, rewritten: 0, files: 0 }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let rewritten = 0
|
|
136
|
+
for (const file of files) {
|
|
137
|
+
const before = readFileSync(file, 'utf8')
|
|
138
|
+
const after = before.split(DEV).join(PROD)
|
|
139
|
+
if (after !== before) {
|
|
140
|
+
writeFileSync(file, after)
|
|
141
|
+
rewritten++
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const bad of FORBIDDEN) {
|
|
145
|
+
if (after.includes(bad)) {
|
|
146
|
+
error(`production-assets: ${file} still references ${bad}`)
|
|
147
|
+
failures++
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (bundle === 'www') {
|
|
153
|
+
const entry = `${slug}.esm.js`
|
|
154
|
+
const expected = join(cwd, WWW, 'build', entry)
|
|
155
|
+
|
|
156
|
+
if (!isFile(expected)) {
|
|
157
|
+
error(`production-assets: expected ${WWW}/build/${entry} - run stencil build first`)
|
|
158
|
+
failures++
|
|
159
|
+
} else {
|
|
160
|
+
log(`production-assets: publishable bundle ${WWW}/build/${entry}`)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Stencil injects its own loader for the tenant entry, but a page may also name the
|
|
164
|
+
// bundle by hand. A page that loads another tenant's file finds nothing under this
|
|
165
|
+
// www/build/ and the widget never boots, so any other name is a deploy failure.
|
|
166
|
+
for (const file of files) {
|
|
167
|
+
for (const [, ref] of readFileSync(file, 'utf8').matchAll(/\/build\/([\w.-]+\.esm\.js)/g)) {
|
|
168
|
+
if (ref !== entry) {
|
|
169
|
+
error(`production-assets: ${file} loads /build/${ref}, not this tenant's /build/${entry}`)
|
|
170
|
+
failures++
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
const { bundles, dirs } = findPublishableBundle(join(cwd, DIST))
|
|
176
|
+
if (bundles.length !== 1) {
|
|
177
|
+
error(
|
|
178
|
+
`production-assets: expected exactly one ${DIST}/<slug>/<slug>.esm.js, found ${bundles.length}` +
|
|
179
|
+
(dirs.length ? ` (directories: ${dirs.join(', ')})` : ` (no ${DIST}/ - run stencil build first)`),
|
|
180
|
+
)
|
|
181
|
+
failures++
|
|
182
|
+
} else {
|
|
183
|
+
log(
|
|
184
|
+
`production-assets: publishable bundle ${DIST}/${bundles[0]}/${bundles[0]}.esm.js -> /storage/customers/${bundles[0]}/`,
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
log(`production-assets: ${rewritten}/${files.length} file(s) rewritten to ${PROD}...`)
|
|
190
|
+
|
|
191
|
+
return { exitCode: failures === 0 ? 0 : 1, rewritten, files: files.length }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Was this file run, or just imported for its tests?
|
|
196
|
+
*
|
|
197
|
+
* `node_modules/.bin` shims (and pnpm's store links) reach the script through a symlink, so the
|
|
198
|
+
* argv path and `import.meta.url` differ textually while naming the same file. Comparing them
|
|
199
|
+
* directly made the bin a silent no-op when a tenant's `postbuild` called it - a guard that
|
|
200
|
+
* passes without checking anything, which is worse than one that fails.
|
|
201
|
+
*/
|
|
202
|
+
function invokedDirectly() {
|
|
203
|
+
if (process.argv[1] === undefined) return false
|
|
204
|
+
try {
|
|
205
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))
|
|
206
|
+
} catch {
|
|
207
|
+
return false
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (invokedDirectly()) {
|
|
212
|
+
const args = process.argv.slice(2)
|
|
213
|
+
const bundle = args[0] === '--www' ? 'www' : 'dist'
|
|
214
|
+
process.exit(productionAssets({ bundle, slug: bundle === 'www' ? args[1] : undefined }).exitCode)
|
|
215
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "buzzcasting-storage",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.2.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"
|