浏览代码

feat(cli.rs): allow config argument to be a path to a JSON file (#2938)

Lucas Fernandes Nogueira 3 年之前
父节点
当前提交
7b81e5b82e
共有 3 个文件被更改,包括 17 次插入4 次删除
  1. 5 0
      .changes/cli-config-path.md
  2. 2 2
      tooling/cli.rs/src/cli.yml
  3. 10 2
      tooling/cli.rs/src/main.rs

+ 5 - 0
.changes/cli-config-path.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Allow `config` arg to be a path to a JSON file on the `dev` and `build` commands.

+ 2 - 2
tooling/cli.rs/src/cli.yml

@@ -15,7 +15,7 @@ subcommands:
           - config:
               short: c
               long: config
-              about: config JSON to merge with tauri.conf.json
+              about: JSON string or path to JSON file to merge with tauri.conf.json
               takes_value: true
           - exit-on-panic:
               short: e
@@ -66,7 +66,7 @@ subcommands:
           - config:
               short: c
               long: config
-              about: config JSON to merge with tauri.conf.json
+              about: JSON string or path to JSON file to merge with tauri.conf.json
               takes_value: true
           - target:
               short: t

+ 10 - 2
tooling/cli.rs/src/main.rs

@@ -57,6 +57,14 @@ macro_rules! value_or_prompt {
   }};
 }
 
+fn get_config(config: &str) -> Result<String> {
+  if config.starts_with('{') {
+    Ok(config.into())
+  } else {
+    std::fs::read_to_string(&config).map_err(Into::into)
+  }
+}
+
 fn plugin_command(matches: &ArgMatches) -> Result<()> {
   if let Some(matches) = matches.subcommand_matches("init") {
     let api = matches.is_present("api");
@@ -199,7 +207,7 @@ fn dev_command(matches: &ArgMatches) -> Result<()> {
     dev_runner = dev_runner.target(target.to_string());
   }
   if let Some(config) = config {
-    dev_runner = dev_runner.config(config.to_string());
+    dev_runner = dev_runner.config(get_config(config)?);
   }
 
   dev_runner.run()
@@ -234,7 +242,7 @@ fn build_command(matches: &ArgMatches) -> Result<()> {
     build_runner = build_runner.bundles(bundles);
   }
   if let Some(config) = config {
-    build_runner = build_runner.config(config.to_string());
+    build_runner = build_runner.config(get_config(config)?);
   }
 
   build_runner.run()