UNPKG

@tobiastengler/create-relay-app

Version:

Easy configuration of Relay for existing projects

52 lines (51 loc) 1.95 kB
import path from "path"; import { NEXT_SRC_PATH } from "../consts.js"; import { bold } from "../utils/index.js"; import { ArgumentBase } from "./ArgumentBase.js"; export class SchemaFileArgument extends ArgumentBase { constructor(fs, env) { super(); this.fs = fs; this.env = env; this.name = "schemaFile"; this.promptMessage = "Where's your GraphQL schema file"; this.cliArg = "--schema-file"; } registerCliOption(command) { const flags = this.getCliFlags("-f", "<path>"); command.option(flags, "path to a GraphQL schema file", (value) => { var _a; return (_a = this.env.rel(value)) === null || _a === void 0 ? void 0 : _a.rel; }); } promptForValue(existingArgs) { return this.showInquirerPrompt({ type: "input", validate: (input) => this.isValid(input, existingArgs), filter: (input) => { var _a; return ((_a = this.env.rel(input)) === null || _a === void 0 ? void 0 : _a.rel) || ""; }, }, existingArgs); } isValid(value, existingArgs) { if (!value) { return "Required"; } const graphqlExt = ".graphql"; const filename = path.basename(value); if (!filename.endsWith(graphqlExt)) { return `File needs to end in ${bold(graphqlExt)}`; } if (!this.fs.isFile(value)) { return `Must be a file`; } if (!this.fs.isSubDirectory(this.env.cwd, value)) { return `Must be a file somewhere below ${bold(this.env.cwd)}`; } return true; } getDefaultValue(existingArgs) { const filename = "schema.graphql"; let srcPath = existingArgs.src; if (existingArgs.toolchain === "next") { srcPath = NEXT_SRC_PATH; } const filepath = path.join(srcPath, filename); return Promise.resolve(this.env.rel(filepath).rel); } }