diff --git a/compiler/crates/relay-bin/src/main.rs b/compiler/crates/relay-bin/src/main.rs index 2251b47256e74..09669fc2b353c 100644 --- a/compiler/crates/relay-bin/src/main.rs +++ b/compiler/crates/relay-bin/src/main.rs @@ -9,10 +9,9 @@ use clap::{ArgEnum, Parser}; use common::ConsoleLogger; use log::{error, info}; use relay_compiler::{ - build_project::artifact_writer::ArtifactValidationWriter, - compiler::Compiler, - config::{Config, SingleProjectConfigFile}, - FileSourceKind, LocalPersister, OperationPersister, PersistConfig, RemotePersister, + build_project::artifact_writer::ArtifactValidationWriter, compiler::Compiler, config::Config, + errors::Error, FileSourceKind, LocalPersister, OperationPersister, PersistConfig, + RemotePersister, }; use simplelog::{ ColorChoice, ConfigBuilder as SimpleLogConfigBuilder, LevelFilter, TermLogger, TerminalMode, @@ -26,11 +25,11 @@ use std::{ #[derive(Parser)] #[clap( - name = "Relay Compiler", - version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"), - about = "Compiles Relay files and writes generated files.", - rename_all = "camel_case" -)] + name = "Relay Compiler", + version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"), + about = "Compiles Relay files and writes generated files.", + rename_all = "camel_case" + )] struct Opt { /// Compile and watch for changes #[clap(long, short)] @@ -71,29 +70,35 @@ enum OutputKind { pub struct CliConfig { /// Path for the directory where to search for source code #[clap(long)] - pub src: Option, + pub src: Option, /// Path to schema file #[clap(long)] - pub schema: Option, + pub schema: Option, /// Path to a directory, where the compiler should write artifacts #[clap(long)] - pub artifact_directory: Option, + pub artifact_directory: Option, } impl CliConfig { - pub fn is_defined(&self) -> bool { + fn is_defined(&self) -> bool { self.src.is_some() || self.schema.is_some() || self.artifact_directory.is_some() } -} -impl From for SingleProjectConfigFile { - fn from(cli_config: CliConfig) -> Self { - SingleProjectConfigFile { - schema: cli_config.schema.expect("schema is required."), - artifact_directory: cli_config.artifact_directory, - src: cli_config.src.unwrap_or_else(|| PathBuf::from("./")), - ..Default::default() - } + fn get_config_string(self) -> String { + let src = self.src.unwrap_or_else(|| "./src".into()); + let schema = self.schema.unwrap_or_else(|| "./path-to-schema".into()); + let artifact_directory = self.artifact_directory.map_or("".to_string(), |a| { + format!("\n \"artifactDirectory\": \"{}\",", a) + }); + format!( + r#" + {{ + "src": "{}", + "schema": "{}",{} + "language": "javascript" + }}"#, + src, schema, artifact_directory + ) } } @@ -126,7 +131,12 @@ async fn main() { let config_result = if let Some(config_path) = opt.config { Config::load(config_path) } else if opt.cli_config.is_defined() { - Ok(Config::from(SingleProjectConfigFile::from(opt.cli_config))) + Err(Error::ConfigError { + details: format!( + "\nPassing Relay compiler configuration is not supported. Please add `relay.config.json` file,\nor \"relay\" section to your `package.json` file.\n\nCompiler configuration JSON:{}", + opt.cli_config.get_config_string(), + ), + }) } else { Config::search(¤t_dir().expect("Unable to get current working directory.")) }; diff --git a/packages/relay-compiler/README.md b/packages/relay-compiler/README.md index d937ff9078939..02d61eb84fbaf 100644 --- a/packages/relay-compiler/README.md +++ b/packages/relay-compiler/README.md @@ -114,14 +114,8 @@ file sources, and "listen" to the file changes in the "watch" mode. If of the exposed types are unified with `$data` and `$variables` suffixes. -### CLI configuration +### CLI Arguments -We also support a limited set of CLI arguments that should cover the most cases -when you need to run the compiler. - -- `--src` Relative path to the source code. -- `--schema` Relative path to schema file. -- `--artifactDirectory` Compiler output directory. - `--repersist` Run the persister even if the query has not changed. - `--watch` Run compiler in `watch` mode. Requires [`watchman`](https://facebook.github.io/watchman/) to be installed.