@hygraph/cli 0.0.9 → 0.0.11
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/README.md +2 -2
- package/dist/cli.js +180 -9
- package/dist/version.js +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -154,10 +154,10 @@ hygraph export write a live environment out as a config
|
|
|
154
154
|
hygraph whoami show which project and environment the token reaches
|
|
155
155
|
hygraph plan show what would change against the environment
|
|
156
156
|
hygraph apply apply the plan as one migration
|
|
157
|
+
hygraph pull show what changed live, as a diff against the config
|
|
157
158
|
```
|
|
158
159
|
|
|
159
|
-
|
|
160
|
-
`npx hygraph <command> --help` for every option.
|
|
160
|
+
Run `npx hygraph <command> --help` for every option.
|
|
161
161
|
|
|
162
162
|
## License
|
|
163
163
|
|
package/dist/cli.js
CHANGED
|
@@ -10,6 +10,7 @@ import { stripVTControlCharacters } from "node:util";
|
|
|
10
10
|
import { createInterface } from "node:readline/promises";
|
|
11
11
|
import { createHash } from "node:crypto";
|
|
12
12
|
import { createJiti } from "jiti";
|
|
13
|
+
import { structuredPatch } from "diff";
|
|
13
14
|
//#region node_modules/.pnpm/commander@15.0.0/node_modules/commander/lib/error.js
|
|
14
15
|
/**
|
|
15
16
|
* CommanderError class
|
|
@@ -16106,7 +16107,7 @@ const fixes = {
|
|
|
16106
16107
|
["change.fieldRenameBothInUse"]: "Delete one of the two fields first, in a plan of its own, or remove renamedFrom.",
|
|
16107
16108
|
["change.fieldRenameFromUnsupported"]: "Rename the field in Studio, then run export again.",
|
|
16108
16109
|
["change.unsupportedFieldMissing"]: "Remove it from the config, or run export again.",
|
|
16109
|
-
["change.unsupportedFieldType"]: "Run
|
|
16110
|
+
["change.unsupportedFieldType"]: "Run pull to see the field as the environment has it, and write it into the config.",
|
|
16110
16111
|
["change.unsupportedPartnerDeleted"]: "Keep both sides in the config, or remove both.",
|
|
16111
16112
|
["change.unsupportedUnionDeleted"]: "Keep the union and its members in the config, or remove all of them.",
|
|
16112
16113
|
["change.unsupportedUnionEmptied"]: "Keep at least one member in the config, or remove the union with them.",
|
|
@@ -22993,22 +22994,192 @@ function count(environment) {
|
|
|
22993
22994
|
return models === 1 ? "1 model" : `${models} models`;
|
|
22994
22995
|
}
|
|
22995
22996
|
//#endregion
|
|
22997
|
+
//#region src/pull/diff.ts
|
|
22998
|
+
/**
|
|
22999
|
+
* The unified diff that takes the config to live, both printed the way
|
|
23000
|
+
* export writes them, or `undefined` when they print the same. Printing
|
|
23001
|
+
* both sides keeps the diff to what differs in the schema, whatever the
|
|
23002
|
+
* config's own layout.
|
|
23003
|
+
*
|
|
23004
|
+
* It describes the change, it is not a patch for the config file: the
|
|
23005
|
+
* config may keep its consts in other files, or hold code of its own, so
|
|
23006
|
+
* the sides are named `config` and `live`, with the target's name, rather
|
|
23007
|
+
* than by a path. By
|
|
23008
|
+
* default it holds the changed lines only, each run under the resource it
|
|
23009
|
+
* is in, such as `@@ component PullQuote`, which is how to find it in any
|
|
23010
|
+
* file. `full` prints the whole config instead, with the changes marked,
|
|
23011
|
+
* so a change shows where it sits among the lines around it.
|
|
23012
|
+
*/
|
|
23013
|
+
async function diffConfig(target, config, live, { full = false } = {}) {
|
|
23014
|
+
const [from, to] = await Promise.all([printConfig(target, config), printConfig(target, live)]);
|
|
23015
|
+
if (from === to) return;
|
|
23016
|
+
const lines = Math.max(from.split("\n").length, to.split("\n").length);
|
|
23017
|
+
const patch = structuredPatch("config", "live", from, to, void 0, void 0, { context: full ? lines : 0 });
|
|
23018
|
+
if (full) return [
|
|
23019
|
+
`--- config (${target.name})`,
|
|
23020
|
+
`+++ live (${target.name})`,
|
|
23021
|
+
...patch.hunks.flatMap((hunk) => hunk.lines)
|
|
23022
|
+
].join("\n");
|
|
23023
|
+
const configLabels = resourceLabels(from);
|
|
23024
|
+
const liveLabels = resourceLabels(to);
|
|
23025
|
+
return [
|
|
23026
|
+
`--- config (${target.name})`,
|
|
23027
|
+
`+++ live (${target.name})`,
|
|
23028
|
+
...patch.hunks.flatMap((hunk) => {
|
|
23029
|
+
const label = hunk.lines.some((line) => line.startsWith("+")) ? liveLabels[hunk.newStart] : configLabels[hunk.oldStart];
|
|
23030
|
+
return [label ? `@@ ${label}` : "@@", ...hunk.lines];
|
|
23031
|
+
})
|
|
23032
|
+
].join("\n");
|
|
23033
|
+
}
|
|
23034
|
+
/** How export starts each resource, and what the diff calls it. */
|
|
23035
|
+
const RESOURCE_STARTS = [
|
|
23036
|
+
[/^const (\w+) = define\.model\(/, (name) => `model ${name}`],
|
|
23037
|
+
[/^const (\w+) = define\.component\(/, (name) => `component ${name}`],
|
|
23038
|
+
[/^const (\w+) = define\.enumeration\(/, (name) => `enumeration ${name}`],
|
|
23039
|
+
[/^const (\w+) = define\.taxonomy\(/, (name) => `taxonomy ${name}`],
|
|
23040
|
+
[/^const (\w+)RemoteSource = define\.(?:graphql|rest)RemoteSource\(/, (prefix) => `remote source ${prefix}`],
|
|
23041
|
+
[/^const \w+ = system\.asset\(/, () => "system.asset"],
|
|
23042
|
+
[/^const \w+ = system\.query\(/, () => "system.query"],
|
|
23043
|
+
[/^const environment = define\.environment\(/, () => "environment"],
|
|
23044
|
+
[/^export default define\.target\(/, () => "target"]
|
|
23045
|
+
];
|
|
23046
|
+
/** Each line's resource, by 1-based line number: the last one that starts at or above it. */
|
|
23047
|
+
function resourceLabels(text) {
|
|
23048
|
+
let current = null;
|
|
23049
|
+
return [null, ...text.split("\n").map((line) => {
|
|
23050
|
+
for (const [start, label] of RESOURCE_STARTS) {
|
|
23051
|
+
const match = start.exec(line);
|
|
23052
|
+
if (match) current = label(match[1] ?? "");
|
|
23053
|
+
}
|
|
23054
|
+
return current;
|
|
23055
|
+
})];
|
|
23056
|
+
}
|
|
23057
|
+
//#endregion
|
|
23058
|
+
//#region src/pull/render.ts
|
|
23059
|
+
/**
|
|
23060
|
+
* What pull prints for one target: a header naming it and the config, for
|
|
23061
|
+
* a reader, and the diff. Pull writes the header to stderr and the diff to
|
|
23062
|
+
* stdout, so `hygraph pull > drift.diff` saves the diff alone. Without a
|
|
23063
|
+
* difference there is a header only.
|
|
23064
|
+
*/
|
|
23065
|
+
function renderPull(diff, { target, config, colors, full }) {
|
|
23066
|
+
if (diff === void 0) return {
|
|
23067
|
+
header: `Pull for ${target.name}: no changes. The config matches the live schema.\n`,
|
|
23068
|
+
diff: ""
|
|
23069
|
+
};
|
|
23070
|
+
return {
|
|
23071
|
+
header: `${[
|
|
23072
|
+
colors.bold(`Pull for ${target.name} (project ${target.project.slice(0, 8)}… / ${target.environment})`),
|
|
23073
|
+
`config: ${config}`,
|
|
23074
|
+
"",
|
|
23075
|
+
"Live differs from the config.",
|
|
23076
|
+
"Both are printed the way export writes a config: find each change by its const.",
|
|
23077
|
+
...full ? [] : ["Run with --full to see the whole config with the changes marked."],
|
|
23078
|
+
""
|
|
23079
|
+
].join("\n")}\n`,
|
|
23080
|
+
diff: `${diff.trimEnd().split("\n").map((line) => colorLine(line, colors)).join("\n")}\n`
|
|
23081
|
+
};
|
|
23082
|
+
}
|
|
23083
|
+
function colorLine(line, colors) {
|
|
23084
|
+
if (line.startsWith("---") || line.startsWith("+++")) return colors.bold(line);
|
|
23085
|
+
if (line.startsWith("@@")) return colors.cyan(line);
|
|
23086
|
+
if (line.startsWith("+")) return colors.green(line);
|
|
23087
|
+
if (line.startsWith("-")) return colors.red(line);
|
|
23088
|
+
return line;
|
|
23089
|
+
}
|
|
23090
|
+
//#endregion
|
|
22996
23091
|
//#region src/commands/pull.ts
|
|
23092
|
+
/** What only pull can find out is wrong. The command prints it and exits 1. */
|
|
23093
|
+
var PullFailure = class extends Failure {};
|
|
22997
23094
|
function registerPull(program) {
|
|
22998
|
-
program.command("pull").description("report what changed live relative to the config").argument("[config]", "path to the config", "hygraph.config.ts").action(function() {
|
|
22999
|
-
|
|
23000
|
-
|
|
23095
|
+
program.command("pull").description("report what changed live relative to the config").argument("[config]", "path to the config", "hygraph.config.ts").option("-t, --target <name>", "pull only this target (repeatable, default: every target)", collect, []).option("--full", "print the whole config with the changes marked, not only the changes").option("--no-color", "print without colours").action(async function(config, options) {
|
|
23096
|
+
const output = this.configureOutput();
|
|
23097
|
+
const write = (text) => output.writeOut?.(text);
|
|
23098
|
+
const writeErr = (text) => output.writeErr?.(text);
|
|
23099
|
+
try {
|
|
23100
|
+
await pull(config, options, write, writeErr);
|
|
23101
|
+
} catch (error) {
|
|
23102
|
+
if (error instanceof Failure) this.error(`error: ${error.message}`, {
|
|
23103
|
+
exitCode: 1,
|
|
23104
|
+
code: "hygraph.pull"
|
|
23105
|
+
});
|
|
23106
|
+
throw error;
|
|
23107
|
+
}
|
|
23108
|
+
}).addHelpText("after", examples([
|
|
23109
|
+
["hygraph pull", "report drift against the default config"],
|
|
23110
|
+
["hygraph pull --target uk", "pull one target of several"],
|
|
23111
|
+
["hygraph pull --full", "see each change among the lines around it"],
|
|
23112
|
+
["hygraph pull brands/kaffa.ts", "report drift against another config"]
|
|
23113
|
+
]));
|
|
23001
23114
|
}
|
|
23002
|
-
|
|
23003
|
-
|
|
23004
|
-
|
|
23005
|
-
|
|
23115
|
+
/**
|
|
23116
|
+
* Pulls every selected target on its own, the way plan plans them: with
|
|
23117
|
+
* several, a failed target is reported and the rest are still pulled, and
|
|
23118
|
+
* the command fails at the end.
|
|
23119
|
+
*/
|
|
23120
|
+
async function pull(config, options, write, writeErr) {
|
|
23121
|
+
const colors = import_picocolors.default.createColors(options.color && import_picocolors.default.isColorSupported && process.stdout.isTTY === true);
|
|
23122
|
+
const targets = await loadTargets(config, options.target, PullFailure);
|
|
23123
|
+
const full = options.full === true;
|
|
23124
|
+
if (targets.length === 1) {
|
|
23125
|
+
await pullTarget(targets[0], {
|
|
23126
|
+
config,
|
|
23127
|
+
colors,
|
|
23128
|
+
full,
|
|
23129
|
+
write,
|
|
23130
|
+
writeErr
|
|
23131
|
+
});
|
|
23132
|
+
return;
|
|
23133
|
+
}
|
|
23134
|
+
const outcomes = [];
|
|
23135
|
+
for (const target of targets) {
|
|
23136
|
+
writeErr(renderTargetHeading(target, colors));
|
|
23137
|
+
try {
|
|
23138
|
+
const diff = await pullTarget(target, {
|
|
23139
|
+
config,
|
|
23140
|
+
colors,
|
|
23141
|
+
full,
|
|
23142
|
+
write,
|
|
23143
|
+
writeErr
|
|
23144
|
+
});
|
|
23145
|
+
outcomes.push({
|
|
23146
|
+
name: target.name,
|
|
23147
|
+
ok: true,
|
|
23148
|
+
detail: diff === void 0 ? "no changes" : "differs from live"
|
|
23149
|
+
});
|
|
23150
|
+
} catch (error) {
|
|
23151
|
+
if (!(error instanceof Failure)) throw error;
|
|
23152
|
+
writeErr(`error: ${target.name}: ${error.message}\n`);
|
|
23153
|
+
outcomes.push({
|
|
23154
|
+
name: target.name,
|
|
23155
|
+
ok: false,
|
|
23156
|
+
detail: firstLine(error)
|
|
23157
|
+
});
|
|
23158
|
+
}
|
|
23159
|
+
}
|
|
23160
|
+
writeErr(renderTargetOutcomes(outcomes, colors));
|
|
23161
|
+
const failed = outcomes.filter(({ ok }) => !ok).length;
|
|
23162
|
+
if (failed > 0) throw new PullFailure(`${failed} of ${targets.length} targets could not be pulled`);
|
|
23163
|
+
}
|
|
23164
|
+
async function pullTarget(target, { config, colors, full, write, writeErr }) {
|
|
23165
|
+
const errors = validateEnvironment(target.apply).filter((validation) => validation.severity === "error");
|
|
23166
|
+
if (errors.length > 0) throw new PullFailure(`the config has ${errors.length === 1 ? "1 error" : `${errors.length} errors`}:\n\n${renderValidations(errors)}`);
|
|
23167
|
+
const { environment: live } = await readLiveEnvironment(target);
|
|
23168
|
+
const diff = await diffConfig(target, convertEnvironmentToIR(target.apply), live, { full });
|
|
23169
|
+
const rendered = renderPull(diff, {
|
|
23170
|
+
target,
|
|
23171
|
+
config,
|
|
23172
|
+
colors,
|
|
23173
|
+
full
|
|
23006
23174
|
});
|
|
23175
|
+
writeErr(rendered.header);
|
|
23176
|
+
write(rendered.diff);
|
|
23177
|
+
return diff;
|
|
23007
23178
|
}
|
|
23008
23179
|
//#endregion
|
|
23009
23180
|
//#region src/commands/validate.ts
|
|
23010
23181
|
function registerValidate(program) {
|
|
23011
|
-
program.command("validate").description("dry-run the migration on the server against real data").requiredOption("--plan <file>", "the plan artifact to validate").action(function() {
|
|
23182
|
+
program.command("validate", { hidden: true }).description("dry-run the migration on the server against real data").requiredOption("--plan <file>", "the plan artifact to validate").action(function() {
|
|
23012
23183
|
validate(this);
|
|
23013
23184
|
}).addHelpText("after", examples([["hygraph validate --plan plan.json", "ask the server to dry-run it"]]));
|
|
23014
23185
|
}
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hygraph/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "Manage your Hygraph schema as code",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Hygraph GmbH",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
+
"diff": "^9.0.0",
|
|
32
33
|
"jiti": "^2.7.0",
|
|
33
34
|
"oxfmt": "0.68.0"
|
|
34
35
|
},
|