@sssb/tools 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/bin/sssb.js +102 -0
- package/package.json +89 -3
- package/src/app-port.js +77 -0
- package/src/app.js +142 -0
- package/src/assembly-checks.d.ts +57 -0
- package/src/assembly-checks.js +259 -0
- package/src/commands/check-demo-size.js +80 -0
- package/src/commands/check-doc-paths.js +48 -0
- package/src/commands/check-e2e.js +611 -0
- package/src/commands/check-fences.js +51 -0
- package/src/commands/check-real-env.js +165 -0
- package/src/commands/check-tmpdir-leak.js +78 -0
- package/src/commands/ci.js +350 -0
- package/src/commands/dev-tick.js +25 -0
- package/src/commands/doctor-checks.js +1276 -0
- package/src/commands/doctor-vitest.js +262 -0
- package/src/commands/doctor.js +954 -0
- package/src/commands/e2e-profile.js +94 -0
- package/src/commands/guide.js +108 -0
- package/src/commands/launch-json.js +55 -0
- package/src/commands/port.js +22 -0
- package/src/commands/serve-standalone.js +78 -0
- package/src/commands/tick.js +27 -0
- package/src/doc-paths.d.ts +106 -0
- package/src/doc-paths.js +328 -0
- package/src/framework-guard.js +35 -0
- package/src/kit.d.ts +43 -0
- package/src/kit.js +205 -0
- package/src/next.d.ts +16 -0
- package/src/next.js +55 -0
- package/src/ports.d.ts +72 -0
- package/src/ports.js +306 -0
- package/src/prune-mantine-css.js +171 -0
- package/src/testing/tmp-dir-marker.js +9 -0
- package/src/testing/tmp-dir-setup.d.ts +2 -0
- package/src/testing/tmp-dir-setup.js +9 -0
- package/src/testing/tmp-dir.d.ts +20 -0
- package/src/testing/tmp-dir.js +174 -0
- package/src/tick/dev-tick.js +128 -0
- package/src/tick/tick.js +315 -0
- package/src/tick/ticker-marker.d.ts +17 -0
- package/src/tick/ticker-marker.js +83 -0
- package/src/tick/ticker.d.ts +36 -0
- package/src/tick/ticker.js +142 -0
- package/src/vite.d.ts +38 -0
- package/src/vite.js +64 -0
- package/src/vitest.d.ts +21 -0
- package/src/vitest.js +55 -0
- package/tsconfig.base.json +33 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rice University
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/bin/sssb.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// THE sssb CLI (ADR-0014, step P4). Every app-level command acts on the app in the working directory, or the one `--app
|
|
4
|
+
// <dir>` names, so each package's own scripts work as they are (part 9). Run it as `pnpm exec sssb`, never
|
|
5
|
+
// `npx sssb`: outside a package that installs @sssb/tools, npx would fetch an unscoped `sssb` from the registry.
|
|
6
|
+
//
|
|
7
|
+
// Each command is its own module, loaded only when named, so a command that needs nothing installed (`ci
|
|
8
|
+
// e2e-shards`, `port`) runs before `pnpm install` has, and none pays for another's dependencies.
|
|
9
|
+
import { UsageError } from '../src/app.js'
|
|
10
|
+
|
|
11
|
+
/** @type {Record<string, { load: () => Promise<{ run: (args: string[]) => Promise<number> }>, summary: string }>} */
|
|
12
|
+
const COMMANDS = {
|
|
13
|
+
port: {
|
|
14
|
+
load: () => import('../src/commands/port.js'),
|
|
15
|
+
summary: "print a port service's port for this checkout (the app's own, with no argument)",
|
|
16
|
+
},
|
|
17
|
+
'launch-json': {
|
|
18
|
+
load: () => import('../src/commands/launch-json.js'),
|
|
19
|
+
summary: "write .claude/launch.json for this checkout's apps and ports",
|
|
20
|
+
},
|
|
21
|
+
'check-demo-size': {
|
|
22
|
+
load: () => import('../src/commands/check-demo-size.js'),
|
|
23
|
+
summary: "fail when the app's static demo (dist-demo/index.html) is over its byte budget",
|
|
24
|
+
},
|
|
25
|
+
'serve-standalone': {
|
|
26
|
+
load: () => import('../src/commands/serve-standalone.js'),
|
|
27
|
+
summary: "serve the app's standalone production build, as the Lambda would",
|
|
28
|
+
},
|
|
29
|
+
'e2e-profile': {
|
|
30
|
+
load: () => import('../src/commands/e2e-profile.js'),
|
|
31
|
+
summary: 'run playwright test and rank where the wall clock went',
|
|
32
|
+
},
|
|
33
|
+
'check-tmpdir-leak': {
|
|
34
|
+
load: () => import('../src/commands/check-tmpdir-leak.js'),
|
|
35
|
+
summary: 'run a test command with a private TMPDIR and fail if a test scratch directory survives',
|
|
36
|
+
},
|
|
37
|
+
'check-real-env': {
|
|
38
|
+
load: () => import('../src/commands/check-real-env.js'),
|
|
39
|
+
summary: "the dev:real preflight: name every cutover item the real adapters' environment still lacks",
|
|
40
|
+
},
|
|
41
|
+
'check-fences': {
|
|
42
|
+
load: () => import('../src/commands/check-fences.js'),
|
|
43
|
+
summary: "fail when the app's lint config drops a fence",
|
|
44
|
+
},
|
|
45
|
+
'check-doc-paths': {
|
|
46
|
+
load: () => import('../src/commands/check-doc-paths.js'),
|
|
47
|
+
summary: "fail when a doc in the workspace's doc set cites a path that does not exist",
|
|
48
|
+
},
|
|
49
|
+
guide: {
|
|
50
|
+
load: () => import('../src/commands/guide.js'),
|
|
51
|
+
summary: "copy @sssb/kit's app guide into the repository, stamped with its hash (--check compares)",
|
|
52
|
+
},
|
|
53
|
+
tick: {
|
|
54
|
+
load: () => import('../src/commands/tick.js'),
|
|
55
|
+
summary: "call the running dev server's scheduled-tick route once and say what ran",
|
|
56
|
+
},
|
|
57
|
+
'dev-tick': {
|
|
58
|
+
load: () => import('../src/commands/dev-tick.js'),
|
|
59
|
+
summary: "run the app's dev server, ticking the scheduler on a timer only when DEV_TICK_SECONDS asks",
|
|
60
|
+
},
|
|
61
|
+
doctor: {
|
|
62
|
+
load: () => import('../src/commands/doctor.js'),
|
|
63
|
+
summary:
|
|
64
|
+
"fail unless the app's SSSB is one copy, on its own seam, in step, and its conformance checks would run",
|
|
65
|
+
},
|
|
66
|
+
'check-e2e': {
|
|
67
|
+
load: () => import('../src/commands/check-e2e.js'),
|
|
68
|
+
summary: 'fail when a Playwright spec reaches @sssb/kit other than through @sssb/kit/testing/e2e',
|
|
69
|
+
},
|
|
70
|
+
ci: {
|
|
71
|
+
load: () => import('../src/commands/ci.js'),
|
|
72
|
+
summary: "a CI job's logic: lint-typecheck-unit, contract, matrix, e2e-shards, e2e, demo-static, synth",
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function usage() {
|
|
77
|
+
const width = Math.max(...Object.keys(COMMANDS).map((name) => name.length))
|
|
78
|
+
return [
|
|
79
|
+
'usage: sssb <command> [--app <dir>] [args...]',
|
|
80
|
+
'',
|
|
81
|
+
...Object.entries(COMMANDS).map(([name, { summary }]) => ` ${name.padEnd(width)} ${summary}`),
|
|
82
|
+
].join('\n')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const [name, ...args] = process.argv.slice(2)
|
|
86
|
+
if (name === undefined || name === '--help' || name === '-h' || name === 'help') {
|
|
87
|
+
console.log(usage())
|
|
88
|
+
process.exit(name === undefined ? 1 : 0)
|
|
89
|
+
}
|
|
90
|
+
const command = Object.hasOwn(COMMANDS, name) ? COMMANDS[name] : undefined
|
|
91
|
+
if (!command) {
|
|
92
|
+
console.error(`sssb: unknown command "${name}"\n\n${usage()}`)
|
|
93
|
+
process.exit(1)
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
process.exitCode = await (await command.load()).run(args)
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (error instanceof UsageError) {
|
|
99
|
+
console.error(error.message)
|
|
100
|
+
process.exitCode = 1
|
|
101
|
+
} else throw error
|
|
102
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,92 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sssb/tools",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/safeinsights/sssb.git",
|
|
8
|
+
"directory": "packages/tools"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"description": "SSSB's tooling: the sssb CLI, the Next, Vite and Vitest presets, the tsconfig base, and helpers any workspace package may import. Plain JavaScript that Node loads directly; it never depends on @sssb/kit, and reaches it only from the app's own directory (ADR-0014, rule 4).",
|
|
12
|
+
"bin": {
|
|
13
|
+
"sssb": "./bin/sssb.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"bin",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.base.json",
|
|
19
|
+
"!src/**/*.test.*"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
"./tsconfig.base.json": "./tsconfig.base.json",
|
|
24
|
+
"./assembly-checks": {
|
|
25
|
+
"types": "./src/assembly-checks.d.ts",
|
|
26
|
+
"default": "./src/assembly-checks.js"
|
|
27
|
+
},
|
|
28
|
+
"./doc-paths": {
|
|
29
|
+
"types": "./src/doc-paths.d.ts",
|
|
30
|
+
"default": "./src/doc-paths.js"
|
|
31
|
+
},
|
|
32
|
+
"./kit": {
|
|
33
|
+
"types": "./src/kit.d.ts",
|
|
34
|
+
"default": "./src/kit.js"
|
|
35
|
+
},
|
|
36
|
+
"./next": {
|
|
37
|
+
"types": "./src/next.d.ts",
|
|
38
|
+
"default": "./src/next.js"
|
|
39
|
+
},
|
|
40
|
+
"./ports": {
|
|
41
|
+
"types": "./src/ports.d.ts",
|
|
42
|
+
"default": "./src/ports.js"
|
|
43
|
+
},
|
|
44
|
+
"./testing/tmp-dir": {
|
|
45
|
+
"types": "./src/testing/tmp-dir.d.ts",
|
|
46
|
+
"default": "./src/testing/tmp-dir.js"
|
|
47
|
+
},
|
|
48
|
+
"./testing/tmp-dir-setup": {
|
|
49
|
+
"types": "./src/testing/tmp-dir-setup.d.ts",
|
|
50
|
+
"default": "./src/testing/tmp-dir-setup.js"
|
|
51
|
+
},
|
|
52
|
+
"./ticker": {
|
|
53
|
+
"types": "./src/tick/ticker.d.ts",
|
|
54
|
+
"default": "./src/tick/ticker.js"
|
|
55
|
+
},
|
|
56
|
+
"./ticker-marker": {
|
|
57
|
+
"types": "./src/tick/ticker-marker.d.ts",
|
|
58
|
+
"default": "./src/tick/ticker-marker.js"
|
|
59
|
+
},
|
|
60
|
+
"./vite": {
|
|
61
|
+
"types": "./src/vite.d.ts",
|
|
62
|
+
"default": "./src/vite.js"
|
|
63
|
+
},
|
|
64
|
+
"./vitest": {
|
|
65
|
+
"types": "./src/vitest.d.ts",
|
|
66
|
+
"default": "./src/vitest.js"
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": ">=24"
|
|
71
|
+
},
|
|
72
|
+
"dependencies": {
|
|
73
|
+
"esbuild": "^0.28.2",
|
|
74
|
+
"postcss": "^8.5.31"
|
|
75
|
+
},
|
|
76
|
+
"peerDependencies": {
|
|
77
|
+
"next": "^16.4.6",
|
|
78
|
+
"vite": "^8.3.5",
|
|
79
|
+
"vitest": "^4.1.12"
|
|
80
|
+
},
|
|
81
|
+
"peerDependenciesMeta": {
|
|
82
|
+
"next": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"vite": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"vitest": {
|
|
89
|
+
"optional": true
|
|
90
|
+
}
|
|
91
|
+
}
|
|
6
92
|
}
|
package/src/app-port.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Which port service an app's dev server binds: its package.json says, as `"sssb": { "port": "<service>" }` (one of
|
|
3
|
+
// SSSB's built-in services) or `"sssb": { "port": { "name": "<name>", "default": <port> } }` (a slot of the app's own, for
|
|
4
|
+
// a workspace whose apps are not SSSB's). One declaration, read by `sssb port` (with no argument, in the app's dev and
|
|
5
|
+
// start scripts), `sssb serve-standalone`, `sssb launch-json`, `sssb tick` and `sssb dev-tick`, so the server, the
|
|
6
|
+
// standalone server, the browser pane and the ticker cannot disagree.
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { UsageError } from './app.js'
|
|
9
|
+
import { foreignAppSlotProblem, readPortDeclaration, SERVICE_NAMES } from './ports.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The name a declaration goes by on a command line: a built-in service's name, or a custom slot's.
|
|
13
|
+
* @param {import('./ports.d.ts').PortDeclaration} declaration
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export const portNameOf = (declaration) => (typeof declaration === 'string' ? declaration : declaration.name)
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The app's declaration, or undefined when it makes none (launch-json skips such an app). An invalid one is a
|
|
20
|
+
* UsageError that says what is wrong, never a silent skip.
|
|
21
|
+
* @param {string} appDir
|
|
22
|
+
* @returns {import('./ports.d.ts').PortDeclaration | undefined}
|
|
23
|
+
*/
|
|
24
|
+
export function declaredPortService(appDir) {
|
|
25
|
+
try {
|
|
26
|
+
return readPortDeclaration(appDir)
|
|
27
|
+
} catch (error) {
|
|
28
|
+
throw new UsageError(`sssb: ${error instanceof Error ? error.message.replace(/^ports: /, '') : String(error)}`)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The app's declaration; a UsageError when it makes none.
|
|
34
|
+
* @param {string} appDir
|
|
35
|
+
* @returns {import('./ports.d.ts').PortDeclaration}
|
|
36
|
+
*/
|
|
37
|
+
export function appPortService(appDir) {
|
|
38
|
+
const declared = declaredPortService(appDir)
|
|
39
|
+
if (declared === undefined) {
|
|
40
|
+
throw new UsageError(
|
|
41
|
+
`sssb: ${path.join(appDir, 'package.json')} declares no port service. Add "sssb": { "port": "<service>" } ` +
|
|
42
|
+
`(one of ${SERVICE_NAMES.join(', ')}) or { "port": { "name": "<name>", "default": <port> } } for a slot ` +
|
|
43
|
+
'of your own, or name a built-in one: sssb port <service>',
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
return declared
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* What a service name typed on a command line means for the app in `appDir`: a built-in service, or the slot the
|
|
51
|
+
* app declares under that name. Undefined when it is neither (a custom slot is the declaring app's alone).
|
|
52
|
+
* @param {string} appDir
|
|
53
|
+
* @param {string} name
|
|
54
|
+
* @returns {import('./ports.d.ts').PortDeclaration | undefined}
|
|
55
|
+
*/
|
|
56
|
+
export function serviceNamed(appDir, name) {
|
|
57
|
+
const declared = declaredPortService(appDir)
|
|
58
|
+
if (SERVICE_NAMES.includes(/** @type {import('./ports.d.ts').PortService} */ (name))) {
|
|
59
|
+
// Another app's slot, to an app that declares one of its own: refused, never quietly that app's port.
|
|
60
|
+
const problem = foreignAppSlotProblem(name, declared)
|
|
61
|
+
if (problem !== null) throw new UsageError(`sssb: ${problem}`)
|
|
62
|
+
return /** @type {import('./ports.d.ts').PortService} */ (name)
|
|
63
|
+
}
|
|
64
|
+
return declared !== undefined && portNameOf(declared) === name ? declared : undefined
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The names `serviceNamed` accepts for the app in `appDir`, for a usage message.
|
|
69
|
+
* @param {string} appDir
|
|
70
|
+
* @returns {string[]}
|
|
71
|
+
*/
|
|
72
|
+
export function servicesKnownTo(appDir) {
|
|
73
|
+
const declared = declaredPortService(appDir)
|
|
74
|
+
return declared === undefined || typeof declared === 'string'
|
|
75
|
+
? [...SERVICE_NAMES]
|
|
76
|
+
: [...SERVICE_NAMES.filter((name) => foreignAppSlotProblem(name, declared) === null), declared.name]
|
|
77
|
+
}
|
package/src/app.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Where a command acts: the app in the working directory, or the one `--app <dir>` names (ADR-0014 part 9), and
|
|
3
|
+
// the workspace around it. Nothing here assumes this repository's layout, so a workspace whose apps are not
|
|
4
|
+
// under apps/ (or a single-app repository) is first-class.
|
|
5
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
|
|
8
|
+
/** A CLI failure: printed without a stack, exit code 1. */
|
|
9
|
+
export class UsageError extends Error {}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The checkout's root: the nearest directory, walking up from `start`, that holds `pnpm-workspace.yaml`.
|
|
13
|
+
* @param {string} start
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export function workspaceRootOf(start) {
|
|
17
|
+
let dir = path.resolve(start)
|
|
18
|
+
for (;;) {
|
|
19
|
+
if (existsSync(path.join(dir, 'pnpm-workspace.yaml'))) return dir
|
|
20
|
+
const parent = path.dirname(dir)
|
|
21
|
+
if (parent === dir)
|
|
22
|
+
throw new UsageError(`sssb: no pnpm-workspace.yaml above ${start}: run this inside a workspace`)
|
|
23
|
+
dir = parent
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Takes `--app <dir>` out of `args` (in place) and returns the app's absolute directory: that one, or the working
|
|
29
|
+
* directory. It must hold a package.json.
|
|
30
|
+
* @param {string[]} args
|
|
31
|
+
* @param {string} [cwd]
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
export function takeAppDir(args, cwd = process.cwd()) {
|
|
35
|
+
const value = takeOption(args, '--app')
|
|
36
|
+
const dir = path.resolve(cwd, value ?? '.')
|
|
37
|
+
if (!existsSync(path.join(dir, 'package.json'))) {
|
|
38
|
+
throw new UsageError(
|
|
39
|
+
value === undefined
|
|
40
|
+
? `sssb: ${dir} is not a package (no package.json): run this from an app's directory or pass --app <dir>`
|
|
41
|
+
: `sssb: --app ${value} is not a package (no package.json at ${dir})`,
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
return dir
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes `name <value>` from `args` and returns the value, or undefined when absent.
|
|
49
|
+
* @param {string[]} args
|
|
50
|
+
* @param {string} name
|
|
51
|
+
* @returns {string | undefined}
|
|
52
|
+
*/
|
|
53
|
+
export function takeOption(args, name) {
|
|
54
|
+
const at = args.indexOf(name)
|
|
55
|
+
if (at < 0) return undefined
|
|
56
|
+
const value = args[at + 1]
|
|
57
|
+
if (value === undefined || value.startsWith('--')) throw new UsageError(`sssb: ${name} needs a value`)
|
|
58
|
+
args.splice(at, 2)
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Removes a boolean flag from `args` and says whether it was there.
|
|
64
|
+
* @param {string[]} args
|
|
65
|
+
* @param {string} name
|
|
66
|
+
*/
|
|
67
|
+
export function takeFlag(args, name) {
|
|
68
|
+
const at = args.indexOf(name)
|
|
69
|
+
if (at < 0) return false
|
|
70
|
+
args.splice(at, 1)
|
|
71
|
+
return true
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @param {string} file */
|
|
75
|
+
export const readJson = (file) => JSON.parse(readFileSync(file, 'utf8'))
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The apps a workspace-wide command fans out over, as directories relative to the workspace root: the root
|
|
79
|
+
* package.json's `sssb.apps` list when it has one, otherwise every `apps/<name>` that is a package.
|
|
80
|
+
* @param {string} root
|
|
81
|
+
* @returns {string[]}
|
|
82
|
+
*/
|
|
83
|
+
export function workspaceApps(root) {
|
|
84
|
+
const manifest = readJson(path.join(root, 'package.json'))
|
|
85
|
+
const listed = manifest?.sssb?.apps
|
|
86
|
+
if (listed !== undefined) {
|
|
87
|
+
if (!Array.isArray(listed) || !listed.every((entry) => typeof entry === 'string'))
|
|
88
|
+
throw new UsageError('sssb: `sssb.apps` in the root package.json must be a list of directories')
|
|
89
|
+
for (const dir of listed) {
|
|
90
|
+
if (path.isAbsolute(dir) || dir.split(/[\\/]/).includes('..'))
|
|
91
|
+
throw new UsageError(`sssb: \`sssb.apps\` names ${dir}: each app is a directory inside the workspace`)
|
|
92
|
+
if (!existsSync(path.join(root, dir, 'package.json')))
|
|
93
|
+
throw new UsageError(`sssb: \`sssb.apps\` names ${dir}, which is not a package`)
|
|
94
|
+
}
|
|
95
|
+
// Normalised (`./apps/a/` is `apps/a`) and each named once, so two spellings of one app are one app.
|
|
96
|
+
return [...new Set(listed.map(normalAppDir))]
|
|
97
|
+
}
|
|
98
|
+
const appsDir = path.join(root, 'apps')
|
|
99
|
+
if (!existsSync(appsDir)) return []
|
|
100
|
+
return readdirSync(appsDir, { withFileTypes: true })
|
|
101
|
+
.filter((entry) => entry.isDirectory() && existsSync(path.join(appsDir, entry.name, 'package.json')))
|
|
102
|
+
.map((entry) => `apps/${entry.name}`)
|
|
103
|
+
.sort()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* An app directory as the workspace names it: `/`-separated, normalised, with no leading `./` or trailing `/`.
|
|
108
|
+
* @param {string} dir
|
|
109
|
+
*/
|
|
110
|
+
export function normalAppDir(dir) {
|
|
111
|
+
return path.posix.normalize(dir.split(path.sep).join('/')).replace(/\/+$/, '')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A short, unique label for each app directory (relative to `root`), for names a person reads: CI shard and job
|
|
116
|
+
* names, the browser pane's configurations. The directory's own name (`showcase`), or, when two apps share one, the
|
|
117
|
+
* whole relative path with `/` as `-` (`services-web`). An app at the root is named by the root's directory. Two
|
|
118
|
+
* spellings that still meet (`a/x/web`, `a-x/web`) are told apart by a number on the later one (`a-x-web-2`).
|
|
119
|
+
* @param {string} root
|
|
120
|
+
* @param {string[]} apps
|
|
121
|
+
* @returns {Map<string, string>}
|
|
122
|
+
*/
|
|
123
|
+
export function appLabels(root, apps) {
|
|
124
|
+
const base = (/** @type {string} */ app) => path.basename(path.resolve(root, app))
|
|
125
|
+
const rel = (/** @type {string} */ app) => path.relative(root, path.resolve(root, app)).split(path.sep).join('-')
|
|
126
|
+
/** @type {Map<string, number>} */
|
|
127
|
+
const counts = new Map()
|
|
128
|
+
for (const app of apps) counts.set(base(app), (counts.get(base(app)) ?? 0) + 1)
|
|
129
|
+
/** @type {Map<string, string>} */
|
|
130
|
+
const labels = new Map()
|
|
131
|
+
/** @type {Set<string>} */
|
|
132
|
+
const taken = new Set()
|
|
133
|
+
for (const app of apps) {
|
|
134
|
+
const wanted = (counts.get(base(app)) ?? 0) > 1 && rel(app) !== '' ? rel(app) : base(app)
|
|
135
|
+
// Two spellings can still meet (`a/x/web` and `a-x/web`): the later one gets a number, so a label is unique.
|
|
136
|
+
let label = wanted
|
|
137
|
+
for (let n = 2; taken.has(label); n++) label = `${wanted}-${n}`
|
|
138
|
+
taken.add(label)
|
|
139
|
+
labels.set(app, label)
|
|
140
|
+
}
|
|
141
|
+
return labels
|
|
142
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** One resource of a synthesized CloudFormation template. */
|
|
2
|
+
export type Resource = { Type: string; Properties?: Record<string, unknown> }
|
|
3
|
+
|
|
4
|
+
/** A synthesized template's `Resources`, keyed by logical id. */
|
|
5
|
+
export type Resources = Record<string, Resource>
|
|
6
|
+
|
|
7
|
+
/** A Lambda function as the checks read it: its logical id, whether it runs in a VPC, and its environment. */
|
|
8
|
+
export type LambdaFunction = { id: string; inVpc: boolean; env: Record<string, unknown> }
|
|
9
|
+
|
|
10
|
+
/** What a migrator bundle must carry, see `migratorBundleProblems`. */
|
|
11
|
+
export type MigratorBundleExpectation = {
|
|
12
|
+
/** The bundle's text, or `undefined` when no bundle was staged for the migrator. */
|
|
13
|
+
code: string | undefined
|
|
14
|
+
/** Module paths the bundle must name as esbuild path comments (`// <path>` on a line of its own). */
|
|
15
|
+
sources: readonly string[]
|
|
16
|
+
/** Migration names the bundle must carry, each as a quoted string (the keys Kysely records). */
|
|
17
|
+
migrations: readonly string[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The staged code (`index.mjs` or `index.js`) of each Lambda whose asset has one, keyed by logical id, read
|
|
22
|
+
* from the synthesized assembly directory. A Lambda whose asset has neither is left out.
|
|
23
|
+
*/
|
|
24
|
+
export declare function lambdaBundles(resources: Resources, assemblyDir: string): Map<string, string>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The first Lambda function whose logical id starts with `namePrefix`, ignoring CDK's own provider
|
|
28
|
+
* functions. Throws when there is none, so a renamed function fails loudly rather than skipping its checks.
|
|
29
|
+
*/
|
|
30
|
+
export declare function lambdaFunction(resources: Resources, namePrefix: string): LambdaFunction
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Problems with a migrator bundle: missing, minified (names none of the `sources`), lacking one of the
|
|
34
|
+
* app's `migrations`, or holding code from a `test-fixture` directory. Empty means it passes.
|
|
35
|
+
*/
|
|
36
|
+
export declare function migratorBundleProblems(expected: MigratorBundleExpectation): string[]
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Problems with a function's final environment that break the production fail-closed contract:
|
|
40
|
+
* `APP_MODE` must be exactly `'real'`, and neither `DEMO_MODE` nor `E2E_BUILD` may be present at all,
|
|
41
|
+
* whatever their value.
|
|
42
|
+
*/
|
|
43
|
+
export declare function failClosedEnvironmentProblems(env: Record<string, unknown>): string[]
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Problems with Secrets Manager and managed-policy grants: every Secrets Manager grant names only the
|
|
47
|
+
* cluster's own secret, and no role attaches an AWS-managed policy the check cannot read. Needs the one
|
|
48
|
+
* `AWS::RDS::DBCluster` in the template; runs on every database shape.
|
|
49
|
+
*/
|
|
50
|
+
export declare function secretGrantProblems(resources: Resources): string[]
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Problems with the Data API shape's access paths: a role is granted `rds-data:ExecuteStatement`, every
|
|
54
|
+
* statement that can grant an `rds-data` action is scoped to the cluster alone, the Secrets Manager checks
|
|
55
|
+
* of `secretGrantProblems`, and the database security group admits no inbound traffic.
|
|
56
|
+
*/
|
|
57
|
+
export declare function dataApiShapeProblems(resources: Resources): string[]
|