@hygraph/cli 0.0.19 → 0.0.20
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/CHANGELOG.md +8 -0
- package/README.md +48 -0
- package/dist/cli.js +47 -5
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @hygraph/cli
|
|
2
2
|
|
|
3
|
+
## 0.0.20
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ecd2855: On a terminal, `export` asks before it replaces a config file that is already there, and writes nothing when you answer no. In CI, or when stdin is not a terminal, `export --out` replaces the file without a question, as before.
|
|
8
|
+
- fea7184: `export` now asks for the target's name as "Name for this environment in the config (used with --target)", and the `--name` help says the same. The new Targets section of the README says what a target is and how `--target` selects one.
|
|
9
|
+
- 2587a4e: When live differs from the config, `pull` now ends with the next steps: give the diff to a coding agent, or run `hygraph export --out <config>` to replace the config with the live schema. `pull --help` and the README show how to save the diff with `hygraph pull > drift.diff`.
|
|
10
|
+
|
|
3
11
|
## 0.0.19
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -86,6 +86,30 @@ npx hygraph plan
|
|
|
86
86
|
|
|
87
87
|
Commit `hygraph.config.ts`. From here on it is the source of your schema.
|
|
88
88
|
|
|
89
|
+
## Targets
|
|
90
|
+
|
|
91
|
+
A target is one environment of one project that the config manages. The
|
|
92
|
+
config exports one target, or a list of targets:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const kaffa = define.target({ name: "kaffa" /* … */ });
|
|
96
|
+
const sencha = define.target({ name: "sencha" /* … */ });
|
|
97
|
+
|
|
98
|
+
export default [kaffa, sencha];
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Each target has a `name`. `--name` sets it on `export`. Without the flag,
|
|
102
|
+
`export` asks for it in a terminal, with the environment as the default, and
|
|
103
|
+
uses the environment elsewhere. Two targets cannot have the same name or the
|
|
104
|
+
same environment of the same project.
|
|
105
|
+
|
|
106
|
+
`plan`, `apply`, `pull` and `whoami` use every target in the config. To use
|
|
107
|
+
only one, give its name with `--target`. Repeat the flag to select more:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
npx hygraph plan --target kaffa
|
|
111
|
+
```
|
|
112
|
+
|
|
89
113
|
## Make a change
|
|
90
114
|
|
|
91
115
|
Edit the config. To add a field to a model you already have, add it to the
|
|
@@ -160,6 +184,30 @@ project, so the plan alone does not let anyone test a guessed value.
|
|
|
160
184
|
`apply` refuses the saved plan when one of these values changed after
|
|
161
185
|
`plan`, or when the project's key changed. Run `plan` again in that case.
|
|
162
186
|
|
|
187
|
+
## Bring live changes into your config
|
|
188
|
+
|
|
189
|
+
When someone changes the schema in Studio, the config no longer matches the
|
|
190
|
+
project. `pull` shows what changed live, as a diff against the config:
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
npx hygraph pull
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
To save the diff to a file, redirect it. The file holds the diff only, without
|
|
197
|
+
colours:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
npx hygraph pull > drift.diff
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Then bring the changes into the config in one of two ways:
|
|
204
|
+
|
|
205
|
+
- **Give the diff to a coding agent.** It edits your config in place. For
|
|
206
|
+
example, ask it: "Bring the live changes in drift.diff into
|
|
207
|
+
hygraph.config.ts, then run `npx hygraph plan` until it shows no changes."
|
|
208
|
+
- **Export again.** `npx hygraph export --out hygraph.config.ts` replaces the
|
|
209
|
+
config with the live schema. Your own edits to the file are lost.
|
|
210
|
+
|
|
163
211
|
## What's supported
|
|
164
212
|
|
|
165
213
|
- **Schema:** models, components, enumerations, taxonomies, locales and
|
package/dist/cli.js
CHANGED
|
@@ -25312,12 +25312,15 @@ function writable(write) {
|
|
|
25312
25312
|
//#region src/commands/export.ts
|
|
25313
25313
|
/** What only export can find out is wrong. The command prints it and exits 1. */
|
|
25314
25314
|
var ExportFailure = class extends Failure {};
|
|
25315
|
+
/** The user answered no. Printed as is, exit 1, and not counted as a failure. */
|
|
25316
|
+
var ExportCancelled = class extends Failure {};
|
|
25315
25317
|
function registerExport(program) {
|
|
25316
|
-
program.command("export").description("write a live environment out as TypeScript").option("--project <id>", "project to export (default: the token's)").option("--endpoint <url>", "the project's management API endpoint (default: the token's)").option("--environment <name>", "environment to export (default: the token's, else master)").option("--name <name>", "
|
|
25318
|
+
program.command("export").description("write a live environment out as TypeScript").option("--project <id>", "project to export (default: the token's)").option("--endpoint <url>", "the project's management API endpoint (default: the token's)").option("--environment <name>", "environment to export (default: the token's, else master)").option("--name <name>", "name for this environment in the config, used with --target (default: the environment)").option("--out <file>", "write to a file instead of stdout").option("--no-color", "print without colours").action(async function(options) {
|
|
25317
25319
|
const write = (text) => this.configureOutput().writeOut?.(text);
|
|
25318
25320
|
const writeErr = (text) => this.configureOutput().writeErr?.(text);
|
|
25319
25321
|
try {
|
|
25320
25322
|
const input = await readInput(options, writeErr);
|
|
25323
|
+
if (input.out !== void 0) await confirmReplace(input.out);
|
|
25321
25324
|
const { config, environment } = await exportEnvironment(input);
|
|
25322
25325
|
const headers = renderHeaderNote(remoteHeaderEnvNames(environment)) + renderSecretNote(oAuthSecretEnvNames(environment));
|
|
25323
25326
|
if (input.out === void 0) {
|
|
@@ -25331,6 +25334,10 @@ function registerExport(program) {
|
|
|
25331
25334
|
write(headers);
|
|
25332
25335
|
write(input.tokenTyped ? `\nSet HYGRAPH_TOKEN to the token, then run \`hygraph plan ${input.out}\`. It should show no changes.\n` : `\nRun \`hygraph plan ${input.out}\` next. It should show no changes.\n`);
|
|
25333
25336
|
} catch (error) {
|
|
25337
|
+
if (error instanceof ExportCancelled) this.error(error.message, {
|
|
25338
|
+
exitCode: 1,
|
|
25339
|
+
code: "hygraph.export"
|
|
25340
|
+
});
|
|
25334
25341
|
recordFailure(error);
|
|
25335
25342
|
if (error instanceof Failure) this.error(`error: ${error.message}`, {
|
|
25336
25343
|
exitCode: 1,
|
|
@@ -25385,7 +25392,7 @@ async function readInput(options, writeErr) {
|
|
|
25385
25392
|
writeErr(`Environment: ${environment} (from the token)\n`);
|
|
25386
25393
|
}
|
|
25387
25394
|
environment ??= terminal === void 0 ? "master" : await askWithDefault("Environment", "master", terminal);
|
|
25388
|
-
const name = options.name ?? (terminal === void 0 ? environment : await askWithDefault("
|
|
25395
|
+
const name = options.name ?? (terminal === void 0 ? environment : await askWithDefault("Name for this environment in the config (used with --target)", environment, terminal));
|
|
25389
25396
|
const out = options.out ?? (terminal !== void 0 && process.stdout.isTTY === true ? await askWithDefault("Write the config to", "hygraph.config.ts", terminal) : void 0);
|
|
25390
25397
|
return {
|
|
25391
25398
|
project,
|
|
@@ -25470,7 +25477,25 @@ function renderSecretNote(names) {
|
|
|
25470
25477
|
if (names.length === 0) return "";
|
|
25471
25478
|
return `\nThe config reads OAuth secrets from ${names.join(", ")}. Set each to its API client's secret before a plan that sends it, such as one that creates the source. Hygraph keeps the secrets and never shows them.\n`;
|
|
25472
25479
|
}
|
|
25473
|
-
/**
|
|
25480
|
+
/**
|
|
25481
|
+
* On a terminal outside CI, asks before an existing file is replaced. Else
|
|
25482
|
+
* the file is replaced without a question.
|
|
25483
|
+
*/
|
|
25484
|
+
async function confirmReplace(path) {
|
|
25485
|
+
if (approvalMode({
|
|
25486
|
+
yes: false,
|
|
25487
|
+
env: process.env,
|
|
25488
|
+
stdinIsTTY: process.stdin.isTTY === true
|
|
25489
|
+
}) !== "ask" || !existsSync(resolve(process.cwd(), path))) return;
|
|
25490
|
+
if (!await confirm(`${path} exists. Replace it? [y/N]: `, {
|
|
25491
|
+
input: process.stdin,
|
|
25492
|
+
output: process.stderr
|
|
25493
|
+
}).catch(() => false)) {
|
|
25494
|
+
recordCancelled();
|
|
25495
|
+
throw new ExportCancelled(`kept ${path}, nothing written`);
|
|
25496
|
+
}
|
|
25497
|
+
}
|
|
25498
|
+
/** Replaces a file that is already there; on a terminal `confirmReplace` asks first. */
|
|
25474
25499
|
function writeConfigFile(config, path) {
|
|
25475
25500
|
try {
|
|
25476
25501
|
writeFileSync(resolve(process.cwd(), path), config);
|
|
@@ -25577,6 +25602,19 @@ function colorLine(line, colors) {
|
|
|
25577
25602
|
if (line.startsWith("-")) return colors.red(line);
|
|
25578
25603
|
return line;
|
|
25579
25604
|
}
|
|
25605
|
+
/**
|
|
25606
|
+
* After the diffs when live differs from the config, on stderr like the
|
|
25607
|
+
* header, so `hygraph pull > drift.diff` saves the diff alone.
|
|
25608
|
+
*/
|
|
25609
|
+
function renderNextSteps(config, colors) {
|
|
25610
|
+
return [
|
|
25611
|
+
"",
|
|
25612
|
+
colors.bold("Next steps"),
|
|
25613
|
+
" - Give the diff to a coding agent to bring the live changes into your config.",
|
|
25614
|
+
` - Run \`hygraph export --out ${config}\` to replace the config with the live schema.`,
|
|
25615
|
+
""
|
|
25616
|
+
].join("\n");
|
|
25617
|
+
}
|
|
25580
25618
|
//#endregion
|
|
25581
25619
|
//#region src/commands/pull.ts
|
|
25582
25620
|
/** What only pull can find out is wrong. The command prints it and exits 1. */
|
|
@@ -25600,6 +25638,7 @@ function registerPull(program) {
|
|
|
25600
25638
|
["hygraph pull", "report drift against the default config"],
|
|
25601
25639
|
["hygraph pull --target uk", "pull one target of several"],
|
|
25602
25640
|
["hygraph pull --full", "see each change among the lines around it"],
|
|
25641
|
+
["hygraph pull > drift.diff", "save the diff to review it or give it to a coding agent"],
|
|
25603
25642
|
["hygraph pull brands/kaffa.ts", "report drift against another config"]
|
|
25604
25643
|
]));
|
|
25605
25644
|
}
|
|
@@ -25613,16 +25652,17 @@ async function pull(config, options, write, writeErr) {
|
|
|
25613
25652
|
const targets = await loadTargets(config, options.target, PullFailure);
|
|
25614
25653
|
const full = options.full === true;
|
|
25615
25654
|
if (targets.length === 1) {
|
|
25616
|
-
await pullTarget(targets[0], {
|
|
25655
|
+
if (await pullTarget(targets[0], {
|
|
25617
25656
|
config,
|
|
25618
25657
|
colors,
|
|
25619
25658
|
full,
|
|
25620
25659
|
write,
|
|
25621
25660
|
writeErr
|
|
25622
|
-
});
|
|
25661
|
+
}) !== void 0) writeErr(renderNextSteps(config, colors));
|
|
25623
25662
|
return;
|
|
25624
25663
|
}
|
|
25625
25664
|
const outcomes = [];
|
|
25665
|
+
let differs = false;
|
|
25626
25666
|
for (const target of targets) {
|
|
25627
25667
|
writeErr(renderTargetHeading(target, colors));
|
|
25628
25668
|
try {
|
|
@@ -25633,6 +25673,7 @@ async function pull(config, options, write, writeErr) {
|
|
|
25633
25673
|
write,
|
|
25634
25674
|
writeErr
|
|
25635
25675
|
});
|
|
25676
|
+
differs ||= diff !== void 0;
|
|
25636
25677
|
outcomes.push({
|
|
25637
25678
|
name: target.name,
|
|
25638
25679
|
ok: true,
|
|
@@ -25650,6 +25691,7 @@ async function pull(config, options, write, writeErr) {
|
|
|
25650
25691
|
}
|
|
25651
25692
|
}
|
|
25652
25693
|
writeErr(renderTargetOutcomes(outcomes, colors));
|
|
25694
|
+
if (differs) writeErr(renderNextSteps(config, colors));
|
|
25653
25695
|
const failed = outcomes.filter(({ ok }) => !ok).length;
|
|
25654
25696
|
if (failed > 0) throw new PullFailure(`${failed} of ${targets.length} targets could not be pulled`);
|
|
25655
25697
|
}
|
package/dist/version.js
CHANGED