@tobiastengler/create-relay-app
Version:
Easy configuration of Relay for existing projects
58 lines (57 loc) • 2.31 kB
JavaScript
import path from "path";
import { bold } from "../utils/index.js";
import { ArgumentBase } from "./ArgumentBase.js";
export class ArtifactDirectoryArgument extends ArgumentBase {
constructor(fs, env) {
super();
this.fs = fs;
this.env = env;
this.name = "artifactDirectory";
this.promptMessage = "(Optional) Where to place Relay artifacts";
this.cliArg = "--artifact-directory";
}
registerCliOption(command) {
const flags = this.getCliFlags("-a", "<path>");
command.option(flags, "directory to place all Relay artifacts in", (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 (input ? ((_a = this.env.rel(input)) === null || _a === void 0 ? void 0 : _a.rel) || "" : ""); },
}, existingArgs);
}
isValid(value, existingArgs) {
if (!value) {
if (existingArgs.toolchain === "next") {
return "Required";
}
// The artifactDirectory is optional.
return true;
}
if (!this.fs.isDirectory(value)) {
return `Must be a directory`;
}
if (path.basename(value) !== "__generated__") {
return `Last directory segment should be called ${bold("__generated__")}`;
}
if (!this.fs.isSubDirectory(this.env.cwd, value)) {
return `Must be directory below ${bold(this.env.cwd)}`;
}
if (existingArgs.toolchain === "next") {
const pagesDirectory = this.env.rel("./pages");
if (this.fs.isSubDirectory(pagesDirectory.abs, value)) {
return `Can not be under ${bold(pagesDirectory.rel)}`;
}
}
return true;
}
getDefaultValue(existingArgs) {
if (existingArgs.toolchain === "next") {
// Artifacts need to be located outside the ./pages directory,
// or they will be treated as pages.
return Promise.resolve("./__generated__");
}
return Promise.resolve("");
}
}