UNPKG

@tobiastengler/create-relay-app

Version:

Easy configuration of Relay for existing projects

44 lines (43 loc) 1.62 kB
import { TaskBase } from "./TaskBase.js"; import { bold } from "../utils/cli.js"; import { execSync } from "child_process"; const validateRelayArtifactsScript = "relay-compiler --validate"; export class AddRelayCompilerScriptsTask extends TaskBase { constructor(context) { super(); this.context = context; this.message = `Add ${bold("relay-compiler")} scripts`; } isEnabled() { return true; } async run() { var _a; this.updateMessage(this.message + ` in ${bold(this.context.ownPackageJsonFile.rel)}`); const packageJson = await this.context.env.packageJson.parse(); const scriptsSection = (_a = packageJson["scripts"]) !== null && _a !== void 0 ? _a : {}; if (!scriptsSection["relay"]) { const watchmanInstalled = isWatchmanInstalled(); // Add "relay" script scriptsSection["relay"] = "relay-compiler"; if (watchmanInstalled) { scriptsSection["relay"] += " --watch"; } } const buildScript = scriptsSection["build"]; if (buildScript && typeof buildScript === "string" && !buildScript.includes(validateRelayArtifactsScript)) { // Validate Relay's artifacts as the first build step. scriptsSection["build"] = validateRelayArtifactsScript + " && " + buildScript; } this.context.env.packageJson.persist(packageJson); } } function isWatchmanInstalled() { try { execSync("watchman", { stdio: "ignore" }); return true; } catch (_a) { return false; } }