Skip to content

Commit

Permalink
feat(nodejs): support additional file patterns (starship#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimyr authored Jun 21, 2020
1 parent b238574 commit b176fc3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 34 additions & 2 deletions src/modules/nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Module<'a>> {
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();

Expand Down Expand Up @@ -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()?;
Expand Down

0 comments on commit b176fc3

Please sign in to comment.