diff --git a/docs/config/README.md b/docs/config/README.md index 44b8aadc..95a89bb3 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 d07d19af..ac81e09e 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()?;