@tobiastengler/create-relay-app
Version:
Easy configuration of Relay for existing projects
65 lines (64 loc) • 2.05 kB
JavaScript
import path from "path";
import { NEXT_SRC_PATH, PACKAGE_FILE, RELAY_ENV } from "../consts.js";
export class ProjectContext {
constructor(env, args, manager, fs) {
this.env = env;
this.manager = manager;
this.fs = fs;
this.args = args;
this.schemaPath = this.env.rel(args.schemaFile);
this.srcPath = this.env.rel(args.src);
if (args.artifactDirectory) {
this.artifactPath = this.env.rel(args.artifactDirectory);
}
else {
this.artifactPath = null;
}
this.compilerLanguage = getRelayCompilerLanguage(args.typescript, args.toolchain);
this.relayEnvFile = getRelayEnvFilepath(env, args);
}
get gitAttributesFile() {
return this.env.rel(".gitattributes");
}
get relayConfigFile() {
return this.env.rel("relay.config.json");
}
get ownPackageJsonFile() {
return this.env.rel(PACKAGE_FILE);
}
get artifactExtension() {
if (this.args.typescript) {
return ".graphql.ts";
}
else {
return ".graphql.js";
}
}
is(toolchain) {
return this.args.toolchain === toolchain;
}
}
function getRelayCompilerLanguage(useTypeScript, toolchain) {
if (useTypeScript ||
// Next does not support 'javascript' as an option,
// only typescript or flow. So we opt for typescript
// since it's more wide spread.
toolchain === "next") {
return "typescript";
}
else {
return "javascript";
}
}
function getRelayEnvFilepath(env, args) {
const filename = RELAY_ENV + (args.typescript ? ".ts" : ".js");
let srcDirectory = args.src;
// The src directory for next is likely the project root,
// so we always default to the ./src directory to place the
// RelayEnvironment file in.
if (args.toolchain === "next") {
srcDirectory = NEXT_SRC_PATH;
}
const filepath = path.join(srcDirectory, filename);
return env.rel(filepath);
}