@nuwah/blueprint 0.0.0-stage → 0.2.0-rc.1
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 +17 -0
- package/README.md +31 -2
- package/dist/arguments.d.ts +11 -0
- package/dist/arguments.js +95 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +40 -0
- package/dist/client.d.ts +98 -0
- package/dist/client.js +91 -0
- package/dist/commands.d.ts +48 -0
- package/dist/commands.js +199 -0
- package/package.json +39 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Copyright (c) 2026 Nuwah. All rights reserved.
|
|
2
|
+
|
|
3
|
+
This software and its source code are proprietary and confidential.
|
|
4
|
+
|
|
5
|
+
No part of this repository may be copied, modified, distributed, published,
|
|
6
|
+
sublicensed, or used to create derivative works, in whole or in part, by any
|
|
7
|
+
means, without the prior written permission of the copyright holder.
|
|
8
|
+
|
|
9
|
+
Access to this repository does not grant any licence, express or implied, to any
|
|
10
|
+
patent, copyright, trademark, or other intellectual property right.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
14
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
16
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
17
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nuwah/blueprint
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Work on a Nuwah blueprint outside the builder, from your own editor or an automated build.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
export NUWAH_API_URL=https://app.nuwah.com
|
|
7
|
+
export NUWAH_BLUEPRINT_TOKEN=nwbp_... # a machine token; keep it in a secret store
|
|
8
|
+
|
|
9
|
+
npx @nuwah/blueprint export --blueprint <id> # writes nuwah.blueprint.json
|
|
10
|
+
npx @nuwah/blueprint validate # the builder's own validator
|
|
11
|
+
npx @nuwah/blueprint diff # sections that differ from the current version
|
|
12
|
+
npx @nuwah/blueprint submit --note "Add a role"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- **One validator.** `validate` calls the same validator the builder uses; this tool judges
|
|
16
|
+
nothing itself.
|
|
17
|
+
- **Nothing applies on submit.** A submission becomes a proposal in the blueprint's "Changes from
|
|
18
|
+
outside the builder" session. The developer confirms it in the builder studio, exactly like a
|
|
19
|
+
change the assistant prepared.
|
|
20
|
+
- **Optimistic on version.** An export file carries the version it was taken from. If the
|
|
21
|
+
blueprint moved on, `submit` exits 3 and tells you the current version: export again, reapply
|
|
22
|
+
your edit, submit again.
|
|
23
|
+
- **Machine tokens** reach one blueprint, can read and propose only, and are revoked in the
|
|
24
|
+
developer console, which also shows when each was last used.
|
|
25
|
+
|
|
26
|
+
Exit codes: `0` done · `1` issues or differences · `2` usage · `3` the blueprint moved on ·
|
|
27
|
+
`4` refused · `5` rate limited · `6` Nuwah unavailable. `--json` answers machine-readable output
|
|
28
|
+
for scripts. `--help` lists every option.
|
|
29
|
+
|
|
30
|
+
The same client is importable: `import { createBlueprintClient } from '@nuwah/blueprint'`.
|
|
31
|
+
|
|
32
|
+
The guide: developer portal → **Builder and AI → Work outside the builder**.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CommandOptions } from './commands.js';
|
|
2
|
+
export declare const DEFAULT_FILE = "nuwah.blueprint.json";
|
|
3
|
+
export declare const USAGE = "Work on a Nuwah blueprint outside the builder.\n\nUsage\n npx @nuwah/blueprint <command> [file] [options]\n\nCommands\n validate [file] check the blueprint with the builder's validator (exit 1 when it has issues)\n diff [file] the sections that differ from the blueprint's current version\n submit [file] send the change for review; confirm it in the builder studio\n export [file] write the current version to file (default nuwah.blueprint.json)\n\nOptions\n --blueprint <id> the blueprint (default NUWAH_BLUEPRINT_ID, or the id in an export file)\n --api-url <url> Nuwah Core (default NUWAH_API_URL, else http://localhost:8090)\n --token <token> a machine token (default NUWAH_BLUEPRINT_TOKEN); prefer the variable\n --expected-version <n> submit: the version you started from (an export file carries it)\n --note <text> submit: what the change is for, shown in the studio\n --version <n> export: an earlier version\n --out <file> export: write here instead of [file]\n --template <file.zip> export: also download the app template\n --oauth-client-id <id> export --template: your OAuth client, written into .env.example\n --console-url <url> where the studio is (default NUWAH_CONSOLE_URL, else http://localhost:8180)\n --strict validate: also fail when the blueprint is not ready for review\n --exit-code diff: exit 1 when anything differs\n --json answer machine-readable JSON\n --language <en|ar> the language of messages (default en)\n --help this text\n\nExit codes: 0 done \u00B7 1 issues or differences \u00B7 2 usage \u00B7 3 the blueprint moved on \u00B7 4 refused \u00B7\n5 rate limited \u00B7 6 Nuwah unavailable";
|
|
4
|
+
export interface ParsedArguments {
|
|
5
|
+
options: CommandOptions;
|
|
6
|
+
apiUrl: string;
|
|
7
|
+
token: string | null;
|
|
8
|
+
help: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Parse `argv` (without node and the script) against the environment. Throws on a malformed option. */
|
|
11
|
+
export declare function parseArguments(argv: string[], environment: Record<string, string | undefined>): ParsedArguments;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export const DEFAULT_FILE = 'nuwah.blueprint.json';
|
|
2
|
+
export const USAGE = `Work on a Nuwah blueprint outside the builder.
|
|
3
|
+
|
|
4
|
+
Usage
|
|
5
|
+
npx @nuwah/blueprint <command> [file] [options]
|
|
6
|
+
|
|
7
|
+
Commands
|
|
8
|
+
validate [file] check the blueprint with the builder's validator (exit 1 when it has issues)
|
|
9
|
+
diff [file] the sections that differ from the blueprint's current version
|
|
10
|
+
submit [file] send the change for review; confirm it in the builder studio
|
|
11
|
+
export [file] write the current version to file (default ${DEFAULT_FILE})
|
|
12
|
+
|
|
13
|
+
Options
|
|
14
|
+
--blueprint <id> the blueprint (default NUWAH_BLUEPRINT_ID, or the id in an export file)
|
|
15
|
+
--api-url <url> Nuwah Core (default NUWAH_API_URL, else http://localhost:8090)
|
|
16
|
+
--token <token> a machine token (default NUWAH_BLUEPRINT_TOKEN); prefer the variable
|
|
17
|
+
--expected-version <n> submit: the version you started from (an export file carries it)
|
|
18
|
+
--note <text> submit: what the change is for, shown in the studio
|
|
19
|
+
--version <n> export: an earlier version
|
|
20
|
+
--out <file> export: write here instead of [file]
|
|
21
|
+
--template <file.zip> export: also download the app template
|
|
22
|
+
--oauth-client-id <id> export --template: your OAuth client, written into .env.example
|
|
23
|
+
--console-url <url> where the studio is (default NUWAH_CONSOLE_URL, else http://localhost:8180)
|
|
24
|
+
--strict validate: also fail when the blueprint is not ready for review
|
|
25
|
+
--exit-code diff: exit 1 when anything differs
|
|
26
|
+
--json answer machine-readable JSON
|
|
27
|
+
--language <en|ar> the language of messages (default en)
|
|
28
|
+
--help this text
|
|
29
|
+
|
|
30
|
+
Exit codes: 0 done · 1 issues or differences · 2 usage · 3 the blueprint moved on · 4 refused ·
|
|
31
|
+
5 rate limited · 6 Nuwah unavailable`;
|
|
32
|
+
const VALUED = new Set(['blueprint', 'api-url', 'token', 'expected-version', 'note', 'version', 'out', 'template', 'oauth-client-id', 'console-url', 'language']);
|
|
33
|
+
const FLAGS = new Set(['json', 'strict', 'exit-code', 'help']);
|
|
34
|
+
/** Parse `argv` (without node and the script) against the environment. Throws on a malformed option. */
|
|
35
|
+
export function parseArguments(argv, environment) {
|
|
36
|
+
const values = {};
|
|
37
|
+
const flags = new Set();
|
|
38
|
+
const positional = [];
|
|
39
|
+
for (let index = 0; index < argv.length; index++) {
|
|
40
|
+
const argument = argv[index];
|
|
41
|
+
if (argument === '-h') {
|
|
42
|
+
flags.add('help');
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!argument.startsWith('--')) {
|
|
46
|
+
positional.push(argument);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const [name, inline] = argument.slice(2).split(/=(.*)/s, 2);
|
|
50
|
+
if (FLAGS.has(name)) {
|
|
51
|
+
flags.add(name);
|
|
52
|
+
}
|
|
53
|
+
else if (VALUED.has(name)) {
|
|
54
|
+
const value = inline ?? argv[++index];
|
|
55
|
+
if (value === undefined || value.startsWith('--'))
|
|
56
|
+
throw new Error(`--${name} needs a value.`);
|
|
57
|
+
values[name] = value;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new Error(`Unknown option --${name}. Run with --help.`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const integer = (name) => {
|
|
64
|
+
if (values[name] === undefined)
|
|
65
|
+
return null;
|
|
66
|
+
const parsed = Number(values[name]);
|
|
67
|
+
if (!Number.isInteger(parsed) || parsed < 1)
|
|
68
|
+
throw new Error(`--${name} must be a whole number from 1.`);
|
|
69
|
+
return parsed;
|
|
70
|
+
};
|
|
71
|
+
const language = values.language ?? 'en';
|
|
72
|
+
if (language !== 'en' && language !== 'ar')
|
|
73
|
+
throw new Error('--language is en or ar.');
|
|
74
|
+
return {
|
|
75
|
+
help: flags.has('help') || positional.length === 0,
|
|
76
|
+
apiUrl: values['api-url'] ?? environment.NUWAH_API_URL ?? 'http://localhost:8090',
|
|
77
|
+
token: values.token ?? environment.NUWAH_BLUEPRINT_TOKEN ?? null,
|
|
78
|
+
options: {
|
|
79
|
+
command: positional[0] ?? '',
|
|
80
|
+
file: positional[1] ?? DEFAULT_FILE,
|
|
81
|
+
blueprintId: values.blueprint ?? environment.NUWAH_BLUEPRINT_ID ?? null,
|
|
82
|
+
expectedVersion: integer('expected-version'),
|
|
83
|
+
version: integer('version'),
|
|
84
|
+
note: values.note ?? null,
|
|
85
|
+
out: values.out ?? null,
|
|
86
|
+
template: values.template ?? null,
|
|
87
|
+
oauthClientId: values['oauth-client-id'] ?? null,
|
|
88
|
+
consoleUrl: values['console-url'] ?? environment.NUWAH_CONSOLE_URL ?? 'http://localhost:8180',
|
|
89
|
+
json: flags.has('json'),
|
|
90
|
+
strict: flags.has('strict'),
|
|
91
|
+
exitCode: flags.has('exit-code'),
|
|
92
|
+
language,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `npx @nuwah/blueprint validate|diff|submit|export` — the thin command-line face of the blueprint
|
|
4
|
+
* API. See `arguments.ts` for the options and `commands.ts` for the exit codes.
|
|
5
|
+
*/
|
|
6
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
7
|
+
import { parseArguments, USAGE } from './arguments.js';
|
|
8
|
+
import { createBlueprintClient } from './client.js';
|
|
9
|
+
import { EXIT, runCommand } from './commands.js';
|
|
10
|
+
async function main() {
|
|
11
|
+
let parsed;
|
|
12
|
+
try {
|
|
13
|
+
parsed = parseArguments(process.argv.slice(2), process.env);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
process.stderr.write(`${error.message}\n`);
|
|
17
|
+
return EXIT.usage;
|
|
18
|
+
}
|
|
19
|
+
if (parsed.help) {
|
|
20
|
+
process.stdout.write(`${USAGE}\n`);
|
|
21
|
+
return parsed.options.command === '' && !process.argv.includes('--help') && !process.argv.includes('-h') ? EXIT.usage : EXIT.done;
|
|
22
|
+
}
|
|
23
|
+
if (!parsed.token) {
|
|
24
|
+
process.stderr.write('No token. Set NUWAH_BLUEPRINT_TOKEN to a machine token (developer console → your blueprint → Take your app with you → Machine tokens).\n');
|
|
25
|
+
return EXIT.usage;
|
|
26
|
+
}
|
|
27
|
+
return runCommand(parsed.options, {
|
|
28
|
+
client: createBlueprintClient({ apiUrl: parsed.apiUrl, token: parsed.token, language: parsed.options.language }),
|
|
29
|
+
readFile: (path) => readFile(path, 'utf8'),
|
|
30
|
+
writeFile: (path, contents) => writeFile(path, contents),
|
|
31
|
+
print: (line) => process.stdout.write(`${line}\n`),
|
|
32
|
+
printError: (line) => process.stderr.write(`${line}\n`),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
main().then((code) => {
|
|
36
|
+
process.exitCode = code;
|
|
37
|
+
}, (error) => {
|
|
38
|
+
process.stderr.write(`${error?.stack ?? String(error)}\n`);
|
|
39
|
+
process.exitCode = EXIT.unavailable;
|
|
40
|
+
});
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The blueprint API client: core's `api/builder/blueprints/{id}/…` routes, the same ones the
|
|
3
|
+
* developer console calls. It judges nothing itself: validation is the builder's one validator,
|
|
4
|
+
* and a submission becomes a proposal the developer confirms in the builder studio.
|
|
5
|
+
*
|
|
6
|
+
* Usable on its own (`import { createBlueprintClient } from '@nuwah/blueprint'`) as well as by
|
|
7
|
+
* the command-line tool.
|
|
8
|
+
*/
|
|
9
|
+
export type Bilingual = {
|
|
10
|
+
en: string;
|
|
11
|
+
ar: string;
|
|
12
|
+
};
|
|
13
|
+
export interface BlueprintIssue {
|
|
14
|
+
path: string;
|
|
15
|
+
message: Bilingual;
|
|
16
|
+
}
|
|
17
|
+
export interface ValidationResult {
|
|
18
|
+
valid: boolean;
|
|
19
|
+
ready: boolean;
|
|
20
|
+
issues: BlueprintIssue[];
|
|
21
|
+
readiness: BlueprintIssue[];
|
|
22
|
+
}
|
|
23
|
+
export interface DiffResult {
|
|
24
|
+
blueprint_id: string;
|
|
25
|
+
current_version: number;
|
|
26
|
+
current_digest: string;
|
|
27
|
+
proposed_digest: string;
|
|
28
|
+
identical: boolean;
|
|
29
|
+
changed: string[];
|
|
30
|
+
sections: Record<string, {
|
|
31
|
+
current: unknown;
|
|
32
|
+
proposed: unknown;
|
|
33
|
+
}>;
|
|
34
|
+
}
|
|
35
|
+
export interface SubmitResult {
|
|
36
|
+
change: {
|
|
37
|
+
id: string;
|
|
38
|
+
status: string;
|
|
39
|
+
summary: Bilingual;
|
|
40
|
+
preview: {
|
|
41
|
+
changed?: string[];
|
|
42
|
+
} | null;
|
|
43
|
+
expires_at: string;
|
|
44
|
+
};
|
|
45
|
+
session_id: string;
|
|
46
|
+
version_from: number;
|
|
47
|
+
review_path: string;
|
|
48
|
+
}
|
|
49
|
+
export interface BlueprintExportDocument {
|
|
50
|
+
$schema?: string;
|
|
51
|
+
schema_version: 'nuwah.blueprint-export.v1';
|
|
52
|
+
exported_at?: string;
|
|
53
|
+
blueprint: {
|
|
54
|
+
id: string;
|
|
55
|
+
key: string;
|
|
56
|
+
name?: Partial<Bilingual>;
|
|
57
|
+
status?: string;
|
|
58
|
+
version: number;
|
|
59
|
+
current_version?: number;
|
|
60
|
+
published_version?: number | null;
|
|
61
|
+
};
|
|
62
|
+
digest?: string;
|
|
63
|
+
document: Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
export declare const EXPORT_SCHEMA_VERSION = "nuwah.blueprint-export.v1";
|
|
66
|
+
/** A refusal from the API: the platform's `{message, reason, details}` shape, plus the status. */
|
|
67
|
+
export declare class BlueprintApiError extends Error {
|
|
68
|
+
readonly status: number;
|
|
69
|
+
readonly reason: string | null;
|
|
70
|
+
readonly details: Record<string, unknown>;
|
|
71
|
+
readonly retryAfterSeconds: number | null;
|
|
72
|
+
constructor(status: number, reason: string | null, message: string, details?: Record<string, unknown>, retryAfterSeconds?: number | null);
|
|
73
|
+
}
|
|
74
|
+
export interface BlueprintClientOptions {
|
|
75
|
+
/** Nuwah Core's base URL, for example https://app.nuwah.com or http://localhost:8090. */
|
|
76
|
+
apiUrl: string;
|
|
77
|
+
/** A machine token (`nwbp_…`) or a developer's IAM access token. */
|
|
78
|
+
token: string;
|
|
79
|
+
/** The language refusals are written in. */
|
|
80
|
+
language?: 'en' | 'ar';
|
|
81
|
+
/** Injected for tests; defaults to the global fetch. */
|
|
82
|
+
fetch?: typeof fetch;
|
|
83
|
+
}
|
|
84
|
+
export interface BlueprintClient {
|
|
85
|
+
export(blueprintId: string, version?: number): Promise<BlueprintExportDocument>;
|
|
86
|
+
appTemplate(blueprintId: string, oauthClientId?: string): Promise<ArrayBuffer>;
|
|
87
|
+
validate(blueprintId: string, document: Record<string, unknown>): Promise<ValidationResult>;
|
|
88
|
+
diff(blueprintId: string, document: Record<string, unknown>): Promise<DiffResult>;
|
|
89
|
+
submit(blueprintId: string, document: Record<string, unknown>, expectedVersion: number, note?: string): Promise<SubmitResult>;
|
|
90
|
+
}
|
|
91
|
+
/** The blueprint document inside an export, or the document itself. */
|
|
92
|
+
export declare function documentOf(input: Record<string, unknown>): Record<string, unknown>;
|
|
93
|
+
/** The blueprint id and version an export carries, if the input is an export. */
|
|
94
|
+
export declare function exportedFrom(input: Record<string, unknown>): {
|
|
95
|
+
id: string;
|
|
96
|
+
version: number;
|
|
97
|
+
} | null;
|
|
98
|
+
export declare function createBlueprintClient(options: BlueprintClientOptions): BlueprintClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The blueprint API client: core's `api/builder/blueprints/{id}/…` routes, the same ones the
|
|
3
|
+
* developer console calls. It judges nothing itself: validation is the builder's one validator,
|
|
4
|
+
* and a submission becomes a proposal the developer confirms in the builder studio.
|
|
5
|
+
*
|
|
6
|
+
* Usable on its own (`import { createBlueprintClient } from '@nuwah/blueprint'`) as well as by
|
|
7
|
+
* the command-line tool.
|
|
8
|
+
*/
|
|
9
|
+
export const EXPORT_SCHEMA_VERSION = 'nuwah.blueprint-export.v1';
|
|
10
|
+
/** A refusal from the API: the platform's `{message, reason, details}` shape, plus the status. */
|
|
11
|
+
export class BlueprintApiError extends Error {
|
|
12
|
+
status;
|
|
13
|
+
reason;
|
|
14
|
+
details;
|
|
15
|
+
retryAfterSeconds;
|
|
16
|
+
constructor(status, reason, message, details = {}, retryAfterSeconds = null) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.status = status;
|
|
19
|
+
this.reason = reason;
|
|
20
|
+
this.details = details;
|
|
21
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
22
|
+
this.name = 'BlueprintApiError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** The blueprint document inside an export, or the document itself. */
|
|
26
|
+
export function documentOf(input) {
|
|
27
|
+
if (input.schema_version === EXPORT_SCHEMA_VERSION && input.document && typeof input.document === 'object') {
|
|
28
|
+
return input.document;
|
|
29
|
+
}
|
|
30
|
+
return input;
|
|
31
|
+
}
|
|
32
|
+
/** The blueprint id and version an export carries, if the input is an export. */
|
|
33
|
+
export function exportedFrom(input) {
|
|
34
|
+
if (input.schema_version !== EXPORT_SCHEMA_VERSION)
|
|
35
|
+
return null;
|
|
36
|
+
const blueprint = input.blueprint;
|
|
37
|
+
if (typeof blueprint?.id !== 'string' || typeof blueprint?.version !== 'number')
|
|
38
|
+
return null;
|
|
39
|
+
return { id: blueprint.id, version: blueprint.version };
|
|
40
|
+
}
|
|
41
|
+
export function createBlueprintClient(options) {
|
|
42
|
+
const base = options.apiUrl.replace(/\/+$/, '');
|
|
43
|
+
const send = options.fetch ?? fetch;
|
|
44
|
+
async function call(method, path, body) {
|
|
45
|
+
let response;
|
|
46
|
+
try {
|
|
47
|
+
response = await send(`${base}/api/builder/blueprints/${path}`, {
|
|
48
|
+
method,
|
|
49
|
+
headers: {
|
|
50
|
+
Accept: 'application/json',
|
|
51
|
+
Authorization: `Bearer ${options.token}`,
|
|
52
|
+
'Accept-Language': options.language ?? 'en',
|
|
53
|
+
...(body === undefined ? {} : { 'Content-Type': 'application/json' }),
|
|
54
|
+
},
|
|
55
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
throw new BlueprintApiError(0, 'unreachable', `Nuwah could not be reached at ${base} (${error.message}).`);
|
|
60
|
+
}
|
|
61
|
+
if (!response.ok) {
|
|
62
|
+
throw await refusal(response);
|
|
63
|
+
}
|
|
64
|
+
return response;
|
|
65
|
+
}
|
|
66
|
+
async function json(method, path, body) {
|
|
67
|
+
return (await (await call(method, path, body)).json());
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
export: (id, version) => json('GET', `${encodeURIComponent(id)}/export${version ? `?version=${version}` : ''}`),
|
|
71
|
+
appTemplate: async (id, oauthClientId) => (await call('GET', `${encodeURIComponent(id)}/app-template${oauthClientId ? `?oauth_client_id=${encodeURIComponent(oauthClientId)}` : ''}`)).arrayBuffer(),
|
|
72
|
+
validate: async (id, document) => (await json('POST', `${encodeURIComponent(id)}/validate`, { document })).data,
|
|
73
|
+
diff: async (id, document) => (await json('POST', `${encodeURIComponent(id)}/diff`, { document })).data,
|
|
74
|
+
submit: async (id, document, expectedVersion, note) => (await json('POST', `${encodeURIComponent(id)}/proposals`, { document, expected_version: expectedVersion, note: note ?? null })).data,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async function refusal(response) {
|
|
78
|
+
let body = {};
|
|
79
|
+
try {
|
|
80
|
+
body = (await response.json());
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// Not JSON (a proxy's error page): the status says enough.
|
|
84
|
+
}
|
|
85
|
+
const retryHeader = response.headers.get('Retry-After');
|
|
86
|
+
const details = body.details && typeof body.details === 'object' && !Array.isArray(body.details) ? body.details : {};
|
|
87
|
+
const retryAfter = typeof details.retry_after_seconds === 'number' ? details.retry_after_seconds : retryHeader ? Number(retryHeader) : null;
|
|
88
|
+
const message = typeof body.message === 'string' && body.message !== '' ? body.message : `Nuwah answered ${response.status}.`;
|
|
89
|
+
const errors = body.errors && typeof body.errors === 'object' ? { errors: body.errors } : {};
|
|
90
|
+
return new BlueprintApiError(response.status, typeof body.reason === 'string' ? body.reason : null, message, { ...details, ...errors }, Number.isFinite(retryAfter) ? retryAfter : null);
|
|
91
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four commands, separated from process handling so they can be tested with an injected
|
|
3
|
+
* client, file system and output. Each answers an exit code:
|
|
4
|
+
*
|
|
5
|
+
* 0 done (valid, identical, submitted, exported; or nothing new to submit)
|
|
6
|
+
* 1 the blueprint has issues (validate), or differs and `--exit-code` was asked (diff)
|
|
7
|
+
* 2 the command was used wrongly: missing token, blueprint, file or version
|
|
8
|
+
* 3 the blueprint moved on since your copy (`version_conflict`): export again
|
|
9
|
+
* 4 refused: the token is not valid, lacks the scope, or the developer agreement is not current
|
|
10
|
+
* 5 rate limited: wait the time it says
|
|
11
|
+
* 6 Nuwah could not be reached, or answered with an error of its own
|
|
12
|
+
*/
|
|
13
|
+
import { type BlueprintClient } from './client.js';
|
|
14
|
+
export declare const EXIT: {
|
|
15
|
+
readonly done: 0;
|
|
16
|
+
readonly issues: 1;
|
|
17
|
+
readonly usage: 2;
|
|
18
|
+
readonly conflict: 3;
|
|
19
|
+
readonly refused: 4;
|
|
20
|
+
readonly rateLimited: 5;
|
|
21
|
+
readonly unavailable: 6;
|
|
22
|
+
};
|
|
23
|
+
export interface CommandOptions {
|
|
24
|
+
command: string;
|
|
25
|
+
file: string;
|
|
26
|
+
blueprintId: string | null;
|
|
27
|
+
expectedVersion: number | null;
|
|
28
|
+
version: number | null;
|
|
29
|
+
note: string | null;
|
|
30
|
+
out: string | null;
|
|
31
|
+
template: string | null;
|
|
32
|
+
oauthClientId: string | null;
|
|
33
|
+
consoleUrl: string;
|
|
34
|
+
json: boolean;
|
|
35
|
+
strict: boolean;
|
|
36
|
+
exitCode: boolean;
|
|
37
|
+
language: 'en' | 'ar';
|
|
38
|
+
}
|
|
39
|
+
export interface CommandEnvironment {
|
|
40
|
+
client: BlueprintClient;
|
|
41
|
+
readFile(path: string): Promise<string>;
|
|
42
|
+
writeFile(path: string, contents: string | Uint8Array): Promise<void>;
|
|
43
|
+
print(line: string): void;
|
|
44
|
+
printError(line: string): void;
|
|
45
|
+
}
|
|
46
|
+
export declare function runCommand(options: CommandOptions, environment: CommandEnvironment): Promise<number>;
|
|
47
|
+
export declare class UsageError extends Error {
|
|
48
|
+
}
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four commands, separated from process handling so they can be tested with an injected
|
|
3
|
+
* client, file system and output. Each answers an exit code:
|
|
4
|
+
*
|
|
5
|
+
* 0 done (valid, identical, submitted, exported; or nothing new to submit)
|
|
6
|
+
* 1 the blueprint has issues (validate), or differs and `--exit-code` was asked (diff)
|
|
7
|
+
* 2 the command was used wrongly: missing token, blueprint, file or version
|
|
8
|
+
* 3 the blueprint moved on since your copy (`version_conflict`): export again
|
|
9
|
+
* 4 refused: the token is not valid, lacks the scope, or the developer agreement is not current
|
|
10
|
+
* 5 rate limited: wait the time it says
|
|
11
|
+
* 6 Nuwah could not be reached, or answered with an error of its own
|
|
12
|
+
*/
|
|
13
|
+
import { BlueprintApiError, documentOf, exportedFrom } from './client.js';
|
|
14
|
+
export const EXIT = { done: 0, issues: 1, usage: 2, conflict: 3, refused: 4, rateLimited: 5, unavailable: 6 };
|
|
15
|
+
export async function runCommand(options, environment) {
|
|
16
|
+
try {
|
|
17
|
+
switch (options.command) {
|
|
18
|
+
case 'validate':
|
|
19
|
+
return await validate(options, environment);
|
|
20
|
+
case 'diff':
|
|
21
|
+
return await diff(options, environment);
|
|
22
|
+
case 'submit':
|
|
23
|
+
return await submit(options, environment);
|
|
24
|
+
case 'export':
|
|
25
|
+
return await exportBlueprint(options, environment);
|
|
26
|
+
default:
|
|
27
|
+
environment.printError(`Unknown command "${options.command}". Use validate, diff, submit or export.`);
|
|
28
|
+
return EXIT.usage;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
return explain(error, options, environment);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function readInput(options, environment) {
|
|
36
|
+
let text;
|
|
37
|
+
try {
|
|
38
|
+
text = await environment.readFile(options.file);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw new UsageError(`Cannot read ${options.file}. Name the blueprint file, or run "export" first.`);
|
|
42
|
+
}
|
|
43
|
+
let input;
|
|
44
|
+
try {
|
|
45
|
+
input = JSON.parse(text);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new UsageError(`${options.file} is not JSON: ${error.message}`);
|
|
49
|
+
}
|
|
50
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
51
|
+
throw new UsageError(`${options.file} must hold a JSON object: a blueprint, or an export of one.`);
|
|
52
|
+
}
|
|
53
|
+
const record = input;
|
|
54
|
+
const exported = exportedFrom(record);
|
|
55
|
+
const blueprintId = options.blueprintId ?? exported?.id ?? null;
|
|
56
|
+
if (!blueprintId) {
|
|
57
|
+
throw new UsageError('Which blueprint? Pass --blueprint <id> or set NUWAH_BLUEPRINT_ID (an export file carries it already).');
|
|
58
|
+
}
|
|
59
|
+
return { input: record, blueprintId, fromVersion: exported?.version ?? null };
|
|
60
|
+
}
|
|
61
|
+
async function validate(options, environment) {
|
|
62
|
+
const { input, blueprintId } = await readInput(options, environment);
|
|
63
|
+
const result = await environment.client.validate(blueprintId, documentOf(input));
|
|
64
|
+
if (options.json) {
|
|
65
|
+
environment.print(JSON.stringify(result, null, 2));
|
|
66
|
+
}
|
|
67
|
+
else if (!result.valid) {
|
|
68
|
+
environment.print(`✗ ${options.file} has ${result.issues.length} issue(s):`);
|
|
69
|
+
printIssues(result.issues, options, environment);
|
|
70
|
+
}
|
|
71
|
+
else if (!result.ready) {
|
|
72
|
+
environment.print(`✓ ${options.file} is valid. Before it can be sent for review it still needs:`);
|
|
73
|
+
printIssues(result.readiness, options, environment);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
environment.print(`✓ ${options.file} is valid and ready for review.`);
|
|
77
|
+
}
|
|
78
|
+
if (!result.valid)
|
|
79
|
+
return EXIT.issues;
|
|
80
|
+
return options.strict && !result.ready ? EXIT.issues : EXIT.done;
|
|
81
|
+
}
|
|
82
|
+
async function diff(options, environment) {
|
|
83
|
+
const { input, blueprintId, fromVersion } = await readInput(options, environment);
|
|
84
|
+
const result = await environment.client.diff(blueprintId, documentOf(input));
|
|
85
|
+
if (options.json) {
|
|
86
|
+
environment.print(JSON.stringify(result, null, 2));
|
|
87
|
+
}
|
|
88
|
+
else if (result.identical) {
|
|
89
|
+
environment.print(`= ${options.file} is the same as version ${result.current_version}.`);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
environment.print(`${result.changed.length} section(s) differ from version ${result.current_version}: ${result.changed.join(', ')}`);
|
|
93
|
+
for (const section of result.changed) {
|
|
94
|
+
environment.print(` ~ ${section}`);
|
|
95
|
+
}
|
|
96
|
+
if (fromVersion !== null && fromVersion !== result.current_version) {
|
|
97
|
+
environment.print(`! Your file was exported from version ${fromVersion}; the blueprint is at ${result.current_version} now. Export again before you submit.`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return !result.identical && options.exitCode ? EXIT.issues : EXIT.done;
|
|
101
|
+
}
|
|
102
|
+
async function submit(options, environment) {
|
|
103
|
+
const { input, blueprintId, fromVersion } = await readInput(options, environment);
|
|
104
|
+
const expectedVersion = options.expectedVersion ?? fromVersion;
|
|
105
|
+
if (expectedVersion === null) {
|
|
106
|
+
throw new UsageError('Which version did you start from? Pass --expected-version <n>, or submit an export file (it carries the version).');
|
|
107
|
+
}
|
|
108
|
+
const result = await environment.client.submit(blueprintId, documentOf(input), expectedVersion, options.note ?? undefined);
|
|
109
|
+
const review = `${options.consoleUrl.replace(/\/+$/, '')}${result.review_path}`;
|
|
110
|
+
if (options.json) {
|
|
111
|
+
environment.print(JSON.stringify({ ...result, review_url: review }, null, 2));
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
environment.print(`✓ Sent for review as change ${result.change.id} (from version ${result.version_from}).`);
|
|
115
|
+
environment.print(` ${options.language === 'ar' ? result.change.summary.ar : result.change.summary.en}`);
|
|
116
|
+
environment.print(` Nothing is applied until you confirm it in the builder: ${review}`);
|
|
117
|
+
}
|
|
118
|
+
return EXIT.done;
|
|
119
|
+
}
|
|
120
|
+
async function exportBlueprint(options, environment) {
|
|
121
|
+
let blueprintId = options.blueprintId;
|
|
122
|
+
if (!blueprintId) {
|
|
123
|
+
try {
|
|
124
|
+
blueprintId = exportedFrom(JSON.parse(await environment.readFile(options.file)))?.id ?? null;
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
blueprintId = null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (!blueprintId) {
|
|
131
|
+
throw new UsageError('Which blueprint? Pass --blueprint <id> or set NUWAH_BLUEPRINT_ID.');
|
|
132
|
+
}
|
|
133
|
+
const exported = await environment.client.export(blueprintId, options.version ?? undefined);
|
|
134
|
+
const target = options.out ?? options.file;
|
|
135
|
+
await environment.writeFile(target, `${JSON.stringify(exported, null, 4)}\n`);
|
|
136
|
+
if (options.template) {
|
|
137
|
+
const archive = await environment.client.appTemplate(blueprintId, options.oauthClientId ?? undefined);
|
|
138
|
+
await environment.writeFile(options.template, new Uint8Array(archive));
|
|
139
|
+
}
|
|
140
|
+
if (options.json) {
|
|
141
|
+
environment.print(JSON.stringify({ file: target, template: options.template, blueprint: exported.blueprint, digest: exported.digest }, null, 2));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
environment.print(`✓ ${exported.blueprint.key} version ${exported.blueprint.version} written to ${target}.`);
|
|
145
|
+
if (options.template)
|
|
146
|
+
environment.print(`✓ App template written to ${options.template}.`);
|
|
147
|
+
}
|
|
148
|
+
return EXIT.done;
|
|
149
|
+
}
|
|
150
|
+
function printIssues(issues, options, environment) {
|
|
151
|
+
for (const issue of issues) {
|
|
152
|
+
environment.print(` - ${issue.path}: ${options.language === 'ar' ? issue.message.ar : issue.message.en}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export class UsageError extends Error {
|
|
156
|
+
}
|
|
157
|
+
function explain(error, options, environment) {
|
|
158
|
+
if (error instanceof UsageError) {
|
|
159
|
+
environment.printError(error.message);
|
|
160
|
+
return EXIT.usage;
|
|
161
|
+
}
|
|
162
|
+
if (!(error instanceof BlueprintApiError)) {
|
|
163
|
+
environment.printError(`Unexpected failure: ${error?.message ?? String(error)}`);
|
|
164
|
+
return EXIT.unavailable;
|
|
165
|
+
}
|
|
166
|
+
if (options.json) {
|
|
167
|
+
environment.printError(JSON.stringify({ status: error.status, reason: error.reason, message: error.message, details: error.details }));
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
environment.printError(`✗ ${error.message}`);
|
|
171
|
+
}
|
|
172
|
+
if (error.reason === 'blueprint_invalid' && Array.isArray(error.details.issues) && !options.json) {
|
|
173
|
+
printIssues(error.details.issues, options, environment);
|
|
174
|
+
return EXIT.issues;
|
|
175
|
+
}
|
|
176
|
+
switch (true) {
|
|
177
|
+
// Idempotent for automated builds: nothing new to send is not a failure.
|
|
178
|
+
case error.reason === 'nothing_to_change' || error.reason === 'change_already_waiting':
|
|
179
|
+
return EXIT.done;
|
|
180
|
+
case error.status === 409 && error.reason === 'version_conflict':
|
|
181
|
+
if (!options.json)
|
|
182
|
+
environment.printError(` The blueprint is at version ${String(error.details.current_version ?? '?')} now. Run "export", reapply your edit, and submit again.`);
|
|
183
|
+
return EXIT.conflict;
|
|
184
|
+
case error.status === 429:
|
|
185
|
+
if (!options.json)
|
|
186
|
+
environment.printError(` Try again in ${error.retryAfterSeconds ?? 60} seconds.`);
|
|
187
|
+
return EXIT.rateLimited;
|
|
188
|
+
case error.status === 401 || error.status === 403:
|
|
189
|
+
return EXIT.refused;
|
|
190
|
+
case error.status === 404:
|
|
191
|
+
if (!options.json)
|
|
192
|
+
environment.printError(' No such blueprint for this token. Check the blueprint id.');
|
|
193
|
+
return EXIT.refused;
|
|
194
|
+
case error.status === 422:
|
|
195
|
+
return EXIT.issues;
|
|
196
|
+
default:
|
|
197
|
+
return EXIT.unavailable;
|
|
198
|
+
}
|
|
199
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@nuwah/blueprint",
|
|
3
|
+
"version": "0.2.0-rc.1",
|
|
4
|
+
"description": "Work on a Nuwah blueprint outside the builder: validate, diff, submit for review and export, from your editor or an automated build.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"nuwah-blueprint": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/client.js",
|
|
11
|
+
"types": "./dist/client.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/client.d.ts",
|
|
15
|
+
"import": "./dist/client.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18.17"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -p tsconfig.json",
|
|
26
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
27
|
+
"test": "npm run build && node --test test/commands.test.mjs",
|
|
28
|
+
"prepack": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^22.10.0",
|
|
32
|
+
"typescript": "^5.7.2"
|
|
33
|
+
},
|
|
34
|
+
"author": "Nuwah",
|
|
35
|
+
"homepage": "https://developers.nuwah.com",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"registry": "https://registry.npmjs.org",
|
|
38
|
+
"access": "public"
|
|
39
|
+
}
|
|
40
|
+
}
|