From b176fc35aca7db0f3345d60e8bd0eea2ac76ecab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Sun, 21 Jun 2020 10:33:58 +0200 Subject: [PATCH] feat(nodejs): support additional file patterns (#1311) --- docs/config/README.md | 3 ++- src/modules/nodejs.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 44b8aadc9..95a89bb3f 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1091,7 +1091,8 @@ The module will be shown if any of the following conditions are met: - The current directory contains a `package.json` file - The current directory contains a `.node-version` file - The current directory contains a `node_modules` directory -- The current directory contains a file with the `.js` extension +- The current directory contains a file with the `.js`, `.mjs` or `.cjs` extension +- The current directory contains a file with the `.ts` extension ### Options diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index d07d19aff..ac81e09ea 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -6,14 +6,15 @@ use crate::utils; /// Creates a module with the current Node.js version /// /// Will display the Node.js version if any of the following criteria are met: -/// - Current directory contains a `.js` file +/// - Current directory contains a `.js`, `.mjs` or `.cjs` file +/// - Current directory contains a `.ts` file /// - Current directory contains a `package.json` or `.node-version` file /// - Current directory contains a `node_modules` directory pub fn module<'a>(context: &'a Context) -> Option> { let is_js_project = context .try_begin_scan()? .set_files(&["package.json", ".node-version"]) - .set_extensions(&["js"]) + .set_extensions(&["js", "mjs", "cjs", "ts"]) .set_folders(&["node_modules"]) .is_match(); @@ -84,6 +85,37 @@ mod tests { dir.close() } + #[test] + fn folder_with_mjs_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("index.mjs"))?.sync_all()?; + + let actual = render_module("nodejs", dir.path(), None); + let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + assert_eq!(expected, actual); + dir.close() + } + + fn folder_with_cjs_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("index.cjs"))?.sync_all()?; + + let actual = render_module("nodejs", dir.path(), None); + let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + assert_eq!(expected, actual); + dir.close() + } + + fn folder_with_ts_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("index.ts"))?.sync_all()?; + + let actual = render_module("nodejs", dir.path(), None); + let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + assert_eq!(expected, actual); + dir.close() + } + #[test] fn folder_with_node_modules() -> io::Result<()> { let dir = tempfile::tempdir()?;