Skip to content

Commit

Permalink
Remove compiler CLI arguments
Browse files Browse the repository at this point in the history
Summary:
To simplify the configuration loading and overall setup instructions, we've discussed the possibility of removing support of some of the CLI arguments (src, schema, artifact_directory).

The error message will print a suggested JSON for the configuration.

Reviewed By: kassens

Differential Revision: D35085454

fbshipit-source-id: 1554ed9df39c9f992f3b0a9065a2afec24e369e6
  • Loading branch information
alunyov authored and facebook-github-bot committed Mar 24, 2022
1 parent 549db94 commit 8bb7478
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
56 changes: 33 additions & 23 deletions compiler/crates/relay-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -71,29 +70,35 @@ enum OutputKind {
pub struct CliConfig {
/// Path for the directory where to search for source code
#[clap(long)]
pub src: Option<PathBuf>,
pub src: Option<String>,
/// Path to schema file
#[clap(long)]
pub schema: Option<PathBuf>,
pub schema: Option<String>,
/// Path to a directory, where the compiler should write artifacts
#[clap(long)]
pub artifact_directory: Option<PathBuf>,
pub artifact_directory: Option<String>,
}

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<CliConfig> 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
)
}
}

Expand Down Expand Up @@ -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(&current_dir().expect("Unable to get current working directory."))
};
Expand Down
8 changes: 1 addition & 7 deletions packages/relay-compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,8 @@ file sources, and "listen" to the file changes in the "watch" mode. If
of the exposed types are unified with `<module>$data` and `<module>$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.
Expand Down

0 comments on commit 8bb7478

Please sign in to comment.