Skip to content

Commit

Permalink
Add CLI option to generate-persisted-query-manifest that lists all …
Browse files Browse the repository at this point in the history
…matched files using the `documents` pattern (#344)
  • Loading branch information
jerelmiller authored Aug 23, 2023
1 parent 145836c commit b32a3d1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-flowers-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/generate-persisted-query-manifest": minor
---

Add a `--list-files` option that lists the set of matched files against the `documents` pattern.
24 changes: 23 additions & 1 deletion packages/generate-persisted-query-manifest/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

const { Command } = require("commander");
const { cosmiconfig } = require("cosmiconfig");
const { generatePersistedQueryManifest, defaults } = require("./dist/index.js");
const {
generatePersistedQueryManifest,
getFilepaths,
defaults,
} = require("./dist/index.js");
const { TypeScriptLoader } = require("cosmiconfig-typescript-loader");
const { version } = require("./package.json");
const { writeFileSync } = require("node:fs");
Expand All @@ -28,13 +32,31 @@ async function getUserConfig({ config: configPath }) {
return configPath ? explorer.load(configPath) : explorer.search();
}

async function listFiles(config) {
const filepaths = await getFilepaths(config?.documents ?? defaults.documents);

if (filepaths.length > 0) {
console.log(filepaths.join("\n"));
}
}

program
.name("generate-persisted-query-manifest")
.description("Generate a persisted query manifest file")
.option("-c, --config <path>", "path to the config file")
.option(
"-l, --list-files",
"prints the files matched from the documents pattern",
)
.version(version, "-v, --version")
.action(async (cliOptions) => {
const result = await getUserConfig(cliOptions);

if (cliOptions.listFiles) {
await listFiles(result?.config);
process.exit(0);
}

const outputPath = result?.config.output ?? defaults.output;

const manifest = await generatePersistedQueryManifest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test("prints help message with --help", async () => {
"Generate a persisted query manifest file",
"Options:",
"-c, --config <path> path to the config file",
"-l, --list-files prints the files matched from the documents pattern",
"-v, --version output the version number",
"-h, --help display help for command",
]
Expand All @@ -45,6 +46,31 @@ test("prints version number with --version", async () => {
await cleanup();
});

test("prints list of matched files with --list-files option", async () => {
const { cleanup, runCommand, writeFile } = await setup();

await writeFile("./src/query.graphql", "");
await writeFile("./src/components/legacy.js", "");
await writeFile("./src/components/my-component.tsx", "");
await writeFile("./src/queries/root.graphql", "");
// Include a file that isn't part of the globbed pattern
await writeFile("./not-included.ts", "");

const { code, stdout } = await runCommand("--list-files");

expect(code).toBe(0);
expect(stdout).toMatchInlineSnapshot(`
[
"src/components/legacy.js",
"src/components/my-component.tsx",
"src/queries/root.graphql",
"src/query.graphql",
]
`);

await cleanup();
});

test("writes manifest file and prints location", async () => {
const { cleanup, exists, runCommand } = await setup();

Expand Down
12 changes: 10 additions & 2 deletions packages/generate-persisted-query-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ function uniq<T>(arr: T[]) {
return [...new Set(arr)];
}

// Unfortunately globby does not guarantee deterministic file sorting so we
// apply some sorting on the files in this function.
//
// https:/sindresorhus/globby/issues/131
export async function getFilepaths(documents: string | string[]) {
return [...uniq(await globby(documents))].sort((a, b) => a.localeCompare(b));
}

export async function generatePersistedQueryManifest(
config: PersistedQueryManifestConfig = {},
configFilePath: string | undefined,
Expand All @@ -229,8 +237,8 @@ export async function generatePersistedQueryManifest(
? relative(process.cwd(), configFilePath)
: "<virtual>",
});
const filepaths = await globby(documents);
const sources = uniq(filepaths).flatMap(getDocumentSources);
const filepaths = await getFilepaths(documents);
const sources = filepaths.flatMap(getDocumentSources);

const fragmentsByName = new Map<string, DocumentSource[]>();
const operationsByName = new Map<string, DocumentSource[]>();
Expand Down

0 comments on commit b32a3d1

Please sign in to comment.