Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function typeNameFromFilename(filename: string): string {

async function samplesFromDirectory(
dataDir: string,
httpHeaders?: string[],
options: CLIOptions,
): Promise<TypeSource[]> {
async function readFilesOrURLsInDirectory(
d: string,
Expand All @@ -152,13 +152,9 @@ async function samplesFromDirectory(
}

if (file.endsWith(".url") || file.endsWith(".json")) {
sourcesInDir.push({
kind: "json",
name,
samples: [
await readableFromFileOrURL(fileOrUrl, httpHeaders),
],
});
sourcesInDir.push(
...(await typeSourcesForURIs(name, [fileOrUrl], options)),
);
} else if (file.endsWith(".schema")) {
sourcesInDir.push({
kind: "schema",
Expand All @@ -175,7 +171,7 @@ async function samplesFromDirectory(
);
graphQLSchema = await readableFromFileOrURL(
fileOrUrl,
httpHeaders,
options.httpHeader,
);
graphQLSchemaFileName = fileOrUrl;
} else if (file.endsWith(".graphql")) {
Expand All @@ -184,7 +180,10 @@ async function samplesFromDirectory(
name,
schema: undefined,
query: await getStream(
await readableFromFileOrURL(fileOrUrl, httpHeaders),
await readableFromFileOrURL(
fileOrUrl,
options.httpHeader,
),
),
});
}
Expand Down Expand Up @@ -874,9 +873,7 @@ async function getSources(options: CLIOptions): Promise<TypeSource[]> {
const directories = exists.filter((x) => fs.lstatSync(x).isDirectory());

for (const dataDir of directories) {
sources = sources.concat(
await samplesFromDirectory(dataDir, options.httpHeader),
);
sources = sources.concat(await samplesFromDirectory(dataDir, options));
}

// Every src that's not a directory is assumed to be a file or URL
Expand Down
52 changes: 52 additions & 0 deletions test/unit/directory-input.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";

import { expect, test } from "vitest";

import { main as quicktype } from "../../src";

test("honors schema source language for JSON files in a directory", async () => {
const temporaryDirectory = fs.mkdtempSync(
path.join(os.tmpdir(), "quicktype-directory-"),
);
const schemaDirectory = path.join(temporaryDirectory, "schemas");
const outputPath = path.join(temporaryDirectory, "out.ts");
fs.mkdirSync(schemaDirectory);

fs.writeFileSync(
path.join(schemaDirectory, "Name.json"),
JSON.stringify({
$schema: "http://json-schema.org/draft-07/schema#",
type: "string",
pattern: "^[a-zA-Z0-9]+$",
}),
);
fs.writeFileSync(
path.join(schemaDirectory, "AnotherName.json"),
JSON.stringify({
$schema: "http://json-schema.org/draft-07/schema#",
type: "string",
pattern: "^\\d+$",
}),
);

try {
await quicktype({
srcLang: "schema",
src: [schemaDirectory],
lang: "typescript",
out: outputPath,
quiet: true,
telemetry: "disable",
});
const output = fs.readFileSync(outputPath, "utf8");

expect(output).toContain("type AnotherName = string;");
expect(output).toContain("type Name = string;");
expect(output).not.toContain("$schema");
expect(output).not.toContain("export interface Name");
} finally {
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
}
});
Loading