create-graphql-markdown-docs 0.1.1 → 1.1.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/README.md +107 -26
- package/lib/create.mjs +525 -670
- package/lib/frameworks/docusaurus.mjs +66 -0
- package/lib/frameworks/index.mjs +66 -0
- package/lib/frameworks/links.mjs +102 -0
- package/lib/frameworks/markdown.mjs +75 -0
- package/lib/frameworks/nuxt.mjs +266 -0
- package/lib/frameworks/presets.mjs +133 -0
- package/lib/helpers.mjs +83 -0
- package/lib/project.mjs +137 -0
- package/lib/rewrites/graphqlrc.mjs +61 -0
- package/lib/rewrites/lines.mjs +18 -0
- package/lib/schema.mjs +102 -0
- package/lib/target.mjs +95 -0
- package/lib/tasks.mjs +146 -0
- package/lib/telemetry.mjs +46 -0
- package/lib/wire/deps.mjs +38 -0
- package/lib/wire/plan.mjs +259 -0
- package/lib/wire/steps.mjs +359 -0
- package/lib/wire/yaml.mjs +93 -0
- package/package.json +5 -4
- package/templates/docusaurus/gitignore +20 -0
- package/templates/docusaurus/package.json +1 -1
- package/templates/markdown/.graphqlrc +9 -0
- package/templates/markdown/README.md +13 -0
- package/templates/markdown/docs/.gitkeep +0 -0
- package/templates/markdown/gitignore +1 -0
- package/templates/markdown/package.json +13 -0
- package/templates/markdown/schema/example.graphql +55 -0
- package/templates/nuxt/gitignore +30 -0
- package/templates/nuxt/package.json +2 -2
package/lib/tasks.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
import * as prompts from "@clack/prompts";
|
|
4
|
+
import { resolveCommand } from "package-manager-detector/commands";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Package managers supported by the scaffolder.
|
|
8
|
+
*/
|
|
9
|
+
export const SUPPORTED_PACKAGE_MANAGERS = ["npm", "pnpm", "yarn", "bun"];
|
|
10
|
+
|
|
11
|
+
function resolveOrNpm(packageManager, intent, args) {
|
|
12
|
+
const agent = SUPPORTED_PACKAGE_MANAGERS.includes(packageManager)
|
|
13
|
+
? packageManager
|
|
14
|
+
: "npm";
|
|
15
|
+
const resolved = resolveCommand(agent, intent, args);
|
|
16
|
+
if (!resolved) {
|
|
17
|
+
throw new Error(`Unsupported ${intent} command for ${agent}`);
|
|
18
|
+
}
|
|
19
|
+
return [resolved.command, resolved.args];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function addDevArgs(packageManager, packages, { workspaceRoot = false } = {}) {
|
|
23
|
+
const rootFlag = workspaceRoot
|
|
24
|
+
? ({ pnpm: ["-w"], yarn: ["-W"] }[packageManager] ?? [])
|
|
25
|
+
: [];
|
|
26
|
+
return resolveOrNpm(packageManager, "add", ["-D", ...rootFlag, ...packages]);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Run a command in a shell.
|
|
31
|
+
*/
|
|
32
|
+
export function runCommand(command, args, options = {}) {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const proc = spawn(command, args, {
|
|
35
|
+
stdio: "inherit",
|
|
36
|
+
...options,
|
|
37
|
+
});
|
|
38
|
+
proc.on("close", (code) => {
|
|
39
|
+
if (code === 0) {
|
|
40
|
+
resolve();
|
|
41
|
+
} else {
|
|
42
|
+
reject(new Error(`Command failed with exit code ${code}`));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
proc.on("error", (err) => {
|
|
46
|
+
reject(
|
|
47
|
+
err?.code === "ENOENT"
|
|
48
|
+
? new Error(`${command} is not installed or not on your PATH`)
|
|
49
|
+
: err,
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Whether `git` is installed and runnable. */
|
|
56
|
+
export async function isGitAvailable() {
|
|
57
|
+
return runCommand("git", ["--version"], { stdio: "ignore" }).then(
|
|
58
|
+
() => true,
|
|
59
|
+
() => false,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Install dependencies for a project using the specified package manager.
|
|
65
|
+
* Logs info, runs the install command and logs success; throws on failure.
|
|
66
|
+
*/
|
|
67
|
+
export async function installDependencies(packageManager, projectDir) {
|
|
68
|
+
prompts.log.info(`Installing dependencies with ${packageManager}...`);
|
|
69
|
+
const [command, args] = resolveOrNpm(packageManager, "install", []);
|
|
70
|
+
await runCommand(command, args, { cwd: projectDir });
|
|
71
|
+
prompts.log.success("Dependencies installed!");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Command line that adds packages as dev dependencies, e.g. "bun add -D a b".
|
|
76
|
+
* @param {string} packageManager
|
|
77
|
+
* @param {string[]} packages
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
export function addDevCommand(packageManager, packages, options = {}) {
|
|
81
|
+
const [command, args] = addDevArgs(packageManager, packages, options);
|
|
82
|
+
return [command, ...args].join(" ");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Add packages as dev dependencies using the specified package manager.
|
|
87
|
+
* Logs info, runs the add command and logs success; throws on failure.
|
|
88
|
+
*/
|
|
89
|
+
export async function addDevDependencies(
|
|
90
|
+
packageManager,
|
|
91
|
+
projectDir,
|
|
92
|
+
packages,
|
|
93
|
+
options = {},
|
|
94
|
+
) {
|
|
95
|
+
prompts.log.info(`Adding ${packages.join(", ")} with ${packageManager}...`);
|
|
96
|
+
const [command, args] = addDevArgs(packageManager, packages, options);
|
|
97
|
+
await runCommand(command, args, { cwd: projectDir });
|
|
98
|
+
prompts.log.success("Dev dependencies added!");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Initialize a git repository in the project directory.
|
|
103
|
+
* Skips when already inside a git work tree (no nested repos) and resolves to
|
|
104
|
+
* `false`; otherwise runs git init, git add, and git commit with an initial
|
|
105
|
+
* commit message and resolves to `true`. Throws on failure (after stopping the
|
|
106
|
+
* spinner).
|
|
107
|
+
*/
|
|
108
|
+
export async function initGitRepo(projectDir) {
|
|
109
|
+
// Git output is silenced (stdio "ignore") so it doesn't garble the prompt UI.
|
|
110
|
+
const options = { cwd: projectDir, stdio: "ignore" };
|
|
111
|
+
|
|
112
|
+
// A nested repo inside an existing one is almost never wanted.
|
|
113
|
+
const insideRepo = await runCommand(
|
|
114
|
+
"git",
|
|
115
|
+
["rev-parse", "--is-inside-work-tree"],
|
|
116
|
+
options,
|
|
117
|
+
).then(
|
|
118
|
+
() => true,
|
|
119
|
+
() => false,
|
|
120
|
+
);
|
|
121
|
+
if (insideRepo) {
|
|
122
|
+
prompts.log.info("Already inside a git repository — skipping git init.");
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const spinner = prompts.spinner();
|
|
127
|
+
spinner.start("Initializing git repository...");
|
|
128
|
+
try {
|
|
129
|
+
await runCommand("git", ["init"], options);
|
|
130
|
+
await runCommand("git", ["add", "."], options);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
spinner.stop("Git initialization incomplete.");
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
await runCommand("git", ["commit", "-m", "Initial commit"], options);
|
|
137
|
+
spinner.stop("Git repository initialized!");
|
|
138
|
+
} catch (error) {
|
|
139
|
+
spinner.stop("Git initialization incomplete.");
|
|
140
|
+
throw new Error(
|
|
141
|
+
`${error.message} (the commit can fail when git user.name / user.email are not configured).`,
|
|
142
|
+
{ cause: error },
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { note } from "@clack/prompts";
|
|
5
|
+
import { track } from "@graphql-markdown/telemetry";
|
|
6
|
+
|
|
7
|
+
import { packageRoot } from "./project.mjs";
|
|
8
|
+
import { EXAMPLE_SCHEMA_REF } from "./schema.mjs";
|
|
9
|
+
|
|
10
|
+
const PKG_NAME = "create-graphql-markdown-docs";
|
|
11
|
+
|
|
12
|
+
/** The prompt answers worth reporting; the telemetry package masks custom values. */
|
|
13
|
+
function configFor(ctx) {
|
|
14
|
+
if (ctx.mode === "wire") {
|
|
15
|
+
return { schema: ctx.schemaRef, linkRoot: ctx.linkRoot };
|
|
16
|
+
}
|
|
17
|
+
return { schema: ctx.schemaPath ?? EXAMPLE_SCHEMA_REF };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Reports one anonymous create event (`create_scaffold` or `create_wire`).
|
|
22
|
+
* Never throws: telemetry must not affect a run. The full notice is shown
|
|
23
|
+
* as a clack note written to stderr, so stdout stays clean. Nothing is sent on `--dry-run`.
|
|
24
|
+
*/
|
|
25
|
+
export async function reportTelemetry(ctx) {
|
|
26
|
+
if (ctx.args?.["dry-run"]) return;
|
|
27
|
+
try {
|
|
28
|
+
const { version } = JSON.parse(
|
|
29
|
+
fs.readFileSync(path.join(packageRoot, "package.json"), "utf-8"),
|
|
30
|
+
);
|
|
31
|
+
await track({
|
|
32
|
+
event: ctx.mode === "wire" ? "create_wire" : "create_scaffold",
|
|
33
|
+
trigger: "create",
|
|
34
|
+
pkg: PKG_NAME,
|
|
35
|
+
pkgVersion: version,
|
|
36
|
+
target: ctx.framework,
|
|
37
|
+
mode: ctx.mode,
|
|
38
|
+
config: configFor(ctx),
|
|
39
|
+
option: ctx.args["no-telemetry"] ? false : undefined,
|
|
40
|
+
onFullNotice: (notice) =>
|
|
41
|
+
note(notice, "Telemetry", { output: process.stderr }),
|
|
42
|
+
});
|
|
43
|
+
} catch {
|
|
44
|
+
// telemetry must never affect a run
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package name behind a module specifier: "@scope/name/sub" gives "@scope/name",
|
|
3
|
+
* "name/sub" gives "name". Local paths ("." or "/" prefixed) have no package and
|
|
4
|
+
* give `undefined`.
|
|
5
|
+
* @param {string} specifier
|
|
6
|
+
* @returns {string | undefined}
|
|
7
|
+
*/
|
|
8
|
+
export function packageNameOf(specifier) {
|
|
9
|
+
if (specifier.startsWith(".") || specifier.startsWith("/")) return undefined;
|
|
10
|
+
const [first, second] = specifier.split("/");
|
|
11
|
+
if (first.startsWith("@")) return second ? `${first}/${second}` : undefined;
|
|
12
|
+
return first;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Packages wire mode needs in the project: the CLI, the formatter (the
|
|
17
|
+
* --formatter value, else the framework preset's) and the schema loader.
|
|
18
|
+
* Duplicates and packages already listed in package.json are left out.
|
|
19
|
+
* @param {{ descriptor: { formatter?: string }, loader: { package: string }, formatter?: string, packageJson?: object | null }} inputs
|
|
20
|
+
* @returns {string[]}
|
|
21
|
+
*/
|
|
22
|
+
export function requiredPackages({
|
|
23
|
+
descriptor,
|
|
24
|
+
loader,
|
|
25
|
+
formatter,
|
|
26
|
+
packageJson,
|
|
27
|
+
}) {
|
|
28
|
+
const present = new Set([
|
|
29
|
+
...Object.keys(packageJson?.dependencies ?? {}),
|
|
30
|
+
...Object.keys(packageJson?.devDependencies ?? {}),
|
|
31
|
+
]);
|
|
32
|
+
const formatterSpecifier = formatter || descriptor.formatter;
|
|
33
|
+
const names = ["@graphql-markdown/cli"];
|
|
34
|
+
if (formatterSpecifier) names.push(packageNameOf(formatterSpecifier));
|
|
35
|
+
names.push(loader.package);
|
|
36
|
+
const unique = [...new Set(names.filter(Boolean))];
|
|
37
|
+
return unique.filter((name) => !present.has(name));
|
|
38
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
normalizeOutput,
|
|
6
|
+
splitOutput,
|
|
7
|
+
suggestLinkRoot,
|
|
8
|
+
} from "../frameworks/links.mjs";
|
|
9
|
+
import { toYaml } from "./yaml.mjs";
|
|
10
|
+
|
|
11
|
+
/** GraphQL config file names that may already hold a project's configuration. */
|
|
12
|
+
const GRAPHQL_CONFIG_FILES = [
|
|
13
|
+
".graphqlrc",
|
|
14
|
+
".graphqlrc.yml",
|
|
15
|
+
".graphqlrc.yaml",
|
|
16
|
+
".graphqlrc.json",
|
|
17
|
+
".graphqlrc.js",
|
|
18
|
+
".graphqlrc.ts",
|
|
19
|
+
".graphqlrc.cjs",
|
|
20
|
+
".graphqlrc.mjs",
|
|
21
|
+
".graphqlrc.cts",
|
|
22
|
+
".graphqlrc.mts",
|
|
23
|
+
".graphqlrc.toml",
|
|
24
|
+
"graphql.config.yml",
|
|
25
|
+
"graphql.config.yaml",
|
|
26
|
+
"graphql.config.json",
|
|
27
|
+
"graphql.config.js",
|
|
28
|
+
"graphql.config.cjs",
|
|
29
|
+
"graphql.config.mjs",
|
|
30
|
+
"graphql.config.ts",
|
|
31
|
+
"graphql.config.cts",
|
|
32
|
+
"graphql.config.mts",
|
|
33
|
+
"graphql.config.toml",
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Inspect a project directory for package.json and an existing GraphQL config.
|
|
38
|
+
* @param {string} projectDir
|
|
39
|
+
* @returns {{ packageJson: object | null, packageJsonIndent: string | number, existingConfig: string | null, workspaceRoot: boolean, pnpmWorkspace: boolean, yarnBerry: boolean }}
|
|
40
|
+
*/
|
|
41
|
+
export function inspectProject(projectDir) {
|
|
42
|
+
let packageJson = null;
|
|
43
|
+
let packageJsonIndent = 2;
|
|
44
|
+
const pkgPath = path.join(projectDir, "package.json");
|
|
45
|
+
if (fs.existsSync(pkgPath)) {
|
|
46
|
+
const raw = fs.readFileSync(pkgPath, "utf8");
|
|
47
|
+
packageJson = JSON.parse(raw);
|
|
48
|
+
const match = /^([ \t]+)\S/m.exec(raw);
|
|
49
|
+
if (match) {
|
|
50
|
+
packageJsonIndent = match[1];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
let existingConfig = GRAPHQL_CONFIG_FILES.find((name) =>
|
|
54
|
+
fs.existsSync(path.join(projectDir, name)),
|
|
55
|
+
);
|
|
56
|
+
if (!existingConfig && packageJson && "graphql" in packageJson) {
|
|
57
|
+
existingConfig = "package.json#graphql";
|
|
58
|
+
}
|
|
59
|
+
const pnpmWorkspace = fs.existsSync(
|
|
60
|
+
path.join(projectDir, "pnpm-workspace.yaml"),
|
|
61
|
+
);
|
|
62
|
+
const workspaceRoot =
|
|
63
|
+
Boolean(packageJson && "workspaces" in packageJson) || pnpmWorkspace;
|
|
64
|
+
const yarnBerry =
|
|
65
|
+
fs.existsSync(path.join(projectDir, ".yarnrc.yml")) ||
|
|
66
|
+
/^yarn@(?:[2-9]|\d{2,})\./.test(packageJson?.packageManager ?? "");
|
|
67
|
+
return {
|
|
68
|
+
packageJson,
|
|
69
|
+
packageJsonIndent,
|
|
70
|
+
existingConfig: existingConfig ?? null,
|
|
71
|
+
workspaceRoot,
|
|
72
|
+
pnpmWorkspace,
|
|
73
|
+
yarnBerry,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Validate the output folder against the project directory.
|
|
79
|
+
* @param {string} projectDir
|
|
80
|
+
* @param {string} output
|
|
81
|
+
* @returns {{ error?: string, warning?: string }}
|
|
82
|
+
*/
|
|
83
|
+
export function validateOutput(projectDir, output) {
|
|
84
|
+
if (!output?.trim()) {
|
|
85
|
+
return { error: "Output folder is required." };
|
|
86
|
+
}
|
|
87
|
+
if (path.isAbsolute(output)) {
|
|
88
|
+
return { error: "Output folder must be relative to the project root." };
|
|
89
|
+
}
|
|
90
|
+
const normalized = path.posix.normalize(
|
|
91
|
+
normalizeOutput(output.trim()) || ".",
|
|
92
|
+
);
|
|
93
|
+
if (normalized === ".") {
|
|
94
|
+
return { error: "Output folder must be a subfolder of the project." };
|
|
95
|
+
}
|
|
96
|
+
if (normalized === ".." || normalized.startsWith("../")) {
|
|
97
|
+
return { error: "Output folder must be inside the project." };
|
|
98
|
+
}
|
|
99
|
+
const target = path.join(projectDir, normalized);
|
|
100
|
+
if (fs.existsSync(target)) {
|
|
101
|
+
if (!fs.statSync(target).isDirectory()) {
|
|
102
|
+
return { error: `${output} already exists and is not a directory.` };
|
|
103
|
+
}
|
|
104
|
+
if (fs.readdirSync(target).length > 0) {
|
|
105
|
+
return {
|
|
106
|
+
warning: `${output} already exists and is not empty — generated files may overwrite pages in it.`,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return {};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Build the .graphqlrc content as a plain object.
|
|
115
|
+
* @param {object} inputs
|
|
116
|
+
* @param {object} inputs.descriptor
|
|
117
|
+
* @param {string} inputs.schema
|
|
118
|
+
* @param {object} inputs.loader
|
|
119
|
+
* @param {string} inputs.output
|
|
120
|
+
* @param {string | null} [inputs.linkRoot] `null` omits linkRoot from the config.
|
|
121
|
+
* @param {string} [inputs.siteBase]
|
|
122
|
+
* @param {string} [inputs.formatter]
|
|
123
|
+
* @returns {object}
|
|
124
|
+
*/
|
|
125
|
+
export function buildGraphqlrc({
|
|
126
|
+
descriptor,
|
|
127
|
+
schema,
|
|
128
|
+
loader,
|
|
129
|
+
output,
|
|
130
|
+
linkRoot,
|
|
131
|
+
siteBase,
|
|
132
|
+
formatter,
|
|
133
|
+
}) {
|
|
134
|
+
const { rootPath, baseURL } = splitOutput(descriptor, output);
|
|
135
|
+
const resolvedLinkRoot =
|
|
136
|
+
descriptor.links === "relative" || linkRoot === null
|
|
137
|
+
? undefined
|
|
138
|
+
: (linkRoot ?? suggestLinkRoot(descriptor, output, { siteBase }));
|
|
139
|
+
const resolvedFormatter = formatter ?? descriptor.formatter;
|
|
140
|
+
|
|
141
|
+
const { options, tokenEnvVar } = loader;
|
|
142
|
+
const loaders =
|
|
143
|
+
!options && !tokenEnvVar
|
|
144
|
+
? { [loader.className]: loader.package }
|
|
145
|
+
: {
|
|
146
|
+
[loader.className]: {
|
|
147
|
+
module: loader.package,
|
|
148
|
+
options: {
|
|
149
|
+
...options,
|
|
150
|
+
...(tokenEnvVar && { token: "${" + tokenEnvVar + "}" }),
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
schema,
|
|
157
|
+
extensions: {
|
|
158
|
+
"graphql-markdown": {
|
|
159
|
+
rootPath: rootPath === "." ? "." : `./${rootPath}`,
|
|
160
|
+
baseURL,
|
|
161
|
+
...(resolvedLinkRoot !== undefined && { linkRoot: resolvedLinkRoot }),
|
|
162
|
+
...(resolvedFormatter && { formatter: resolvedFormatter }),
|
|
163
|
+
loaders,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Default package.json script added in wire mode; namespaced to avoid clashes. */
|
|
170
|
+
export const DEFAULT_SCRIPT_NAME = "gqlmd";
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Plan the files and package.json changes needed to wire a project.
|
|
174
|
+
* @param {object} inputs Arguments for buildGraphqlrc.
|
|
175
|
+
* @param {ReturnType<typeof inspectProject>} project
|
|
176
|
+
* @param {{ scriptName?: string }} [options]
|
|
177
|
+
* @returns {{ files: { path: string, content: string }[], mergeBlock?: string, script: object, warnings: string[] }}
|
|
178
|
+
*/
|
|
179
|
+
export function planWire(
|
|
180
|
+
inputs,
|
|
181
|
+
project,
|
|
182
|
+
{ scriptName = DEFAULT_SCRIPT_NAME } = {},
|
|
183
|
+
) {
|
|
184
|
+
const config = buildGraphqlrc(inputs);
|
|
185
|
+
const warnings = [];
|
|
186
|
+
let files = [];
|
|
187
|
+
let mergeBlock;
|
|
188
|
+
if (project.existingConfig) {
|
|
189
|
+
mergeBlock = toYaml({ extensions: config.extensions });
|
|
190
|
+
warnings.push(
|
|
191
|
+
`Found ${project.existingConfig} — not writing .graphqlrc. Merge the block below into it (schema: ${inputs.schema}).`,
|
|
192
|
+
);
|
|
193
|
+
} else {
|
|
194
|
+
files = [{ path: ".graphqlrc", content: toYaml(config) }];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let script;
|
|
198
|
+
if (!project.packageJson) {
|
|
199
|
+
script = { skipped: "no package.json" };
|
|
200
|
+
} else if (project.packageJson.scripts?.[scriptName]) {
|
|
201
|
+
script = { skipped: `script "${scriptName}" already exists` };
|
|
202
|
+
} else {
|
|
203
|
+
script = { name: scriptName, command: "gqlmd graphql-to-doc" };
|
|
204
|
+
}
|
|
205
|
+
return { files, mergeBlock, script, warnings };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Apply a wire plan to disk. Never overwrites existing files.
|
|
210
|
+
* @param {string} projectDir
|
|
211
|
+
* @param {ReturnType<typeof planWire>} plan
|
|
212
|
+
* @param {ReturnType<typeof inspectProject>} project
|
|
213
|
+
* @returns {string[]} Written paths (relative to projectDir).
|
|
214
|
+
*/
|
|
215
|
+
export function applyWirePlan(projectDir, plan, project) {
|
|
216
|
+
const written = [];
|
|
217
|
+
for (const file of plan.files) {
|
|
218
|
+
fs.writeFileSync(path.join(projectDir, file.path), file.content, {
|
|
219
|
+
flag: "wx",
|
|
220
|
+
});
|
|
221
|
+
written.push(file.path);
|
|
222
|
+
}
|
|
223
|
+
if (plan.script.name) {
|
|
224
|
+
const pkg = project.packageJson;
|
|
225
|
+
pkg.scripts = { ...pkg.scripts, [plan.script.name]: plan.script.command };
|
|
226
|
+
fs.writeFileSync(
|
|
227
|
+
path.join(projectDir, "package.json"),
|
|
228
|
+
JSON.stringify(pkg, null, project.packageJsonIndent) + "\n",
|
|
229
|
+
);
|
|
230
|
+
written.push("package.json");
|
|
231
|
+
}
|
|
232
|
+
return written;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Render a wire plan as human-readable text (for --dry-run).
|
|
237
|
+
* @param {ReturnType<typeof planWire>} plan
|
|
238
|
+
* @returns {string}
|
|
239
|
+
*/
|
|
240
|
+
export function formatWirePlan(plan) {
|
|
241
|
+
const lines = [];
|
|
242
|
+
for (const file of plan.files) {
|
|
243
|
+
lines.push(`Write ${file.path}:`, file.content);
|
|
244
|
+
}
|
|
245
|
+
if (plan.script.name) {
|
|
246
|
+
lines.push(
|
|
247
|
+
`Add script to package.json: "${plan.script.name}": "${plan.script.command}"`,
|
|
248
|
+
);
|
|
249
|
+
} else {
|
|
250
|
+
lines.push(`Skip package.json script: ${plan.script.skipped}`);
|
|
251
|
+
}
|
|
252
|
+
if (plan.mergeBlock) {
|
|
253
|
+
lines.push("", "Merge block:", plan.mergeBlock);
|
|
254
|
+
}
|
|
255
|
+
for (const warning of plan.warnings) {
|
|
256
|
+
lines.push(`Warning: ${warning}`);
|
|
257
|
+
}
|
|
258
|
+
return lines.join("\n");
|
|
259
|
+
}
|