mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05:00
load plugins from a flag
This commit is contained in:
parent
7f45875211
commit
c9fa293b9d
3 changed files with 79 additions and 5 deletions
1
bar.js
Normal file
1
bar.js
Normal file
|
@ -0,0 +1 @@
|
|||
const foo = "baz123";
|
|
@ -302,6 +302,7 @@ pub struct LintFlags {
|
|||
pub json: bool,
|
||||
pub compact: bool,
|
||||
pub watch: Option<WatchFlags>,
|
||||
pub maybe_plugins: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl LintFlags {
|
||||
|
@ -2849,6 +2850,16 @@ To ignore linting on an entire file, you can add an ignore comment at the top of
|
|||
.help("Use set of rules with a tag")
|
||||
.help_heading(LINT_HEADING),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("plugins")
|
||||
.long("plugins")
|
||||
.require_equals(true)
|
||||
.num_args(1..)
|
||||
.action(ArgAction::Append)
|
||||
.use_value_delimiter(true)
|
||||
.help("Plugins to run, relative or absolute paths")
|
||||
.help_heading(LINT_HEADING),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("rules-include")
|
||||
.long("rules-include")
|
||||
|
@ -5109,6 +5120,10 @@ fn lint_parse(
|
|||
.remove_many::<String>("rules-exclude")
|
||||
.map(|f| f.collect());
|
||||
|
||||
let maybe_plugins = matches
|
||||
.remove_many::<String>("plugins")
|
||||
.map(|f| f.collect());
|
||||
|
||||
let json = matches.get_flag("json");
|
||||
let compact = matches.get_flag("compact");
|
||||
|
||||
|
@ -5125,6 +5140,7 @@ fn lint_parse(
|
|||
json,
|
||||
compact,
|
||||
watch: watch_arg_parse(matches)?,
|
||||
maybe_plugins,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
@ -7127,6 +7143,7 @@ mod tests {
|
|||
json: false,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7155,6 +7172,7 @@ mod tests {
|
|||
json: false,
|
||||
compact: false,
|
||||
watch: Some(Default::default()),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7188,6 +7206,7 @@ mod tests {
|
|||
no_clear_screen: true,
|
||||
exclude: vec![],
|
||||
}),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7215,6 +7234,7 @@ mod tests {
|
|||
json: false,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7237,6 +7257,7 @@ mod tests {
|
|||
json: false,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7264,6 +7285,7 @@ mod tests {
|
|||
json: false,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7292,6 +7314,7 @@ mod tests {
|
|||
json: false,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7314,6 +7337,7 @@ mod tests {
|
|||
json: true,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -7343,6 +7367,7 @@ mod tests {
|
|||
json: true,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
config_flag: ConfigFlag::Path("Deno.jsonc".to_string()),
|
||||
..Flags::default()
|
||||
|
@ -7373,11 +7398,42 @@ mod tests {
|
|||
json: false,
|
||||
compact: true,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: None,
|
||||
}),
|
||||
config_flag: ConfigFlag::Path("Deno.jsonc".to_string()),
|
||||
..Flags::default()
|
||||
}
|
||||
);
|
||||
|
||||
let r = flags_from_vec(svec![
|
||||
"deno",
|
||||
"lint",
|
||||
"--plugins=./plugins/plugin1.js,/dev/plugins/plugin2.js",
|
||||
]);
|
||||
assert_eq!(
|
||||
r.unwrap(),
|
||||
Flags {
|
||||
subcommand: DenoSubcommand::Lint(LintFlags {
|
||||
files: FileFlags {
|
||||
include: vec![],
|
||||
ignore: vec![],
|
||||
},
|
||||
fix: false,
|
||||
rules: false,
|
||||
maybe_rules_tags: None,
|
||||
maybe_rules_include: None,
|
||||
maybe_rules_exclude: None,
|
||||
json: false,
|
||||
compact: false,
|
||||
watch: Default::default(),
|
||||
maybe_plugins: Some(vec![
|
||||
"./plugins/plugin1.js".to_string(),
|
||||
"/dev/plugins/plugin2.js".to_string()
|
||||
]),
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -10,6 +10,7 @@ use deno_config::glob::FileCollector;
|
|||
use deno_config::glob::FilePatterns;
|
||||
use deno_config::workspace::WorkspaceDirectory;
|
||||
use deno_core::anyhow::anyhow;
|
||||
use deno_core::error::custom_error;
|
||||
use deno_core::error::generic_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::futures::future::LocalBoxFuture;
|
||||
|
@ -125,6 +126,7 @@ pub async fn lint(
|
|||
lint_config.clone(),
|
||||
paths_with_options.dir,
|
||||
paths_with_options.paths,
|
||||
lint_flags.maybe_plugins.clone(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
@ -189,6 +191,7 @@ pub async fn lint(
|
|||
deno_lint_config.clone(),
|
||||
paths_with_options.dir,
|
||||
paths_with_options.paths,
|
||||
lint_flags.maybe_plugins.clone(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
@ -275,6 +278,7 @@ impl WorkspaceLinter {
|
|||
lint_config: LintConfig,
|
||||
member_dir: WorkspaceDirectory,
|
||||
paths: Vec<PathBuf>,
|
||||
maybe_plugins: Option<Vec<String>>,
|
||||
) -> Result<(), AnyError> {
|
||||
self.file_count += paths.len();
|
||||
|
||||
|
@ -291,11 +295,24 @@ impl WorkspaceLinter {
|
|||
))
|
||||
});
|
||||
|
||||
// TODO: pass from the caller
|
||||
let plugin_specifiers = vec![ModuleSpecifier::from_file_path(
|
||||
&std::env::current_dir().unwrap().join("./plugin.js"),
|
||||
)
|
||||
.unwrap()];
|
||||
let plugin_specifiers = if let Some(plugins) = maybe_plugins {
|
||||
let mut plugin_specifiers = Vec::with_capacity(plugins.len());
|
||||
let cwd = cli_options.initial_cwd();
|
||||
for plugin in plugins {
|
||||
let path = cwd.join(plugin);
|
||||
let url = ModuleSpecifier::from_file_path(&path).map_err(|_| {
|
||||
custom_error(
|
||||
"NotFound",
|
||||
format!("Bad plugin path: {}", path.display()),
|
||||
)
|
||||
})?;
|
||||
plugin_specifiers.push(url);
|
||||
}
|
||||
plugin_specifiers
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
let maybe_plugin_runner = Some(Arc::new(Mutex::new(
|
||||
plugins::create_runner_and_load_plugins(plugin_specifiers)
|
||||
.await
|
||||
|
|
Loading…
Add table
Reference in a new issue