refactor: add RootConfig (#14985)

This commit is contained in:
David Sherret 2022-06-28 16:45:55 -04:00 committed by GitHub
parent 5b7bcefa11
commit 01adbb1efb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 917 additions and 819 deletions

View file

@ -1,6 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::args::ConfigFile;
use crate::args::Flags;
use crate::args::TaskFlags;
use crate::colors;
@ -12,34 +11,6 @@ use deno_core::error::AnyError;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
fn get_tasks_config(
maybe_config_file: Option<&ConfigFile>,
) -> Result<BTreeMap<String, String>, AnyError> {
if let Some(config_file) = maybe_config_file {
let maybe_tasks_config = config_file.to_tasks_config()?;
if let Some(tasks_config) = maybe_tasks_config {
for key in tasks_config.keys() {
if key.is_empty() {
bail!("Configuration file task names cannot be empty");
} else if !key
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | ':'))
{
bail!("Configuration file task names must only contain alpha-numeric characters, colons (:), underscores (_), or dashes (-). Task: {}", key);
} else if !key.chars().next().unwrap().is_ascii_alphabetic() {
bail!("Configuration file task names must start with an alphabetic character. Task: {}", key);
}
}
Ok(tasks_config)
} else {
bail!("No tasks found in configuration file")
}
} else {
bail!("No config file found")
}
}
fn print_available_tasks(tasks_config: BTreeMap<String, String>) {
eprintln!("{}", colors::green("Available tasks:"));
@ -58,10 +29,9 @@ pub async fn execute_script(
"{} deno task is unstable and may drastically change in the future",
crate::colors::yellow("Warning"),
);
let flags = Arc::new(flags);
let ps = ProcState::build(flags.clone()).await?;
let tasks_config = get_tasks_config(ps.maybe_config_file.as_ref())?;
let config_file_url = &ps.maybe_config_file.as_ref().unwrap().specifier;
let ps = ProcState::build(flags).await?;
let tasks_config = ps.config.resolve_tasks_config()?;
let config_file_url = ps.config.maybe_config_file_specifier().unwrap();
let config_file_path = if config_file_url.scheme() == "file" {
config_file_url.to_file_path().unwrap()
} else {
@ -81,8 +51,9 @@ pub async fn execute_script(
let maybe_script = tasks_config.get(&task_name);
if let Some(script) = maybe_script {
let additional_args = flags
.argv
let additional_args = ps
.config
.argv()
.iter()
// surround all the additional arguments in double quotes
// and santize any command substition
@ -108,74 +79,3 @@ pub async fn execute_script(
Ok(1)
}
}
#[cfg(test)]
mod test {
use deno_ast::ModuleSpecifier;
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn tasks_no_tasks() {
run_task_error_test(r#"{}"#, "No tasks found in configuration file");
}
#[test]
fn task_name_invalid_chars() {
run_task_error_test(
r#"{
"tasks": {
"build": "deno test",
"some%test": "deno bundle mod.ts"
}
}"#,
concat!(
"Configuration file task names must only contain alpha-numeric ",
"characters, colons (:), underscores (_), or dashes (-). Task: some%test",
),
);
}
#[test]
fn task_name_non_alpha_starting_char() {
run_task_error_test(
r#"{
"tasks": {
"build": "deno test",
"1test": "deno bundle mod.ts"
}
}"#,
concat!(
"Configuration file task names must start with an ",
"alphabetic character. Task: 1test",
),
);
}
#[test]
fn task_name_empty() {
run_task_error_test(
r#"{
"tasks": {
"build": "deno test",
"": "deno bundle mod.ts"
}
}"#,
"Configuration file task names cannot be empty",
);
}
fn run_task_error_test(config_text: &str, expected_error: &str) {
let config_dir = ModuleSpecifier::parse("file:///deno/").unwrap();
let config_specifier = config_dir.join("tsconfig.json").unwrap();
let config_file = ConfigFile::new(config_text, &config_specifier).unwrap();
assert_eq!(
get_tasks_config(Some(&config_file))
.err()
.unwrap()
.to_string(),
expected_error,
);
}
}