Skip to content

Commit

Permalink
feat(nodejs): Configure when the module is shown (starship#2348)
Browse files Browse the repository at this point in the history
This makes it possible to configure when the nodejs module is shown
based on the contents of a directory. This should make it possible to
be a lot more granular when configuring the module.
  • Loading branch information
cappyzawa authored Feb 21, 2021
1 parent 4651060 commit c0a0e85
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
19 changes: 11 additions & 8 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ format = 'via [☃️ $state( \($name\))](bold blue) '
## NodeJS

The `nodejs` module shows the currently installed version of NodeJS.
The module will be shown if any of the following conditions are met:
By default 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
Expand All @@ -1799,13 +1799,16 @@ The module will be shown if any of the following conditions are met:

### Options

| Option | Default | Description |
| ---------- | ---------------------------------- | -------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"⬢ "` | A format string representing the symbol of NodeJS. |
| `style` | `"bold green"` | The style for the module. |
| `disabled` | `false` | Disables the `nodejs` module. |
| `not_capable_style` | `bold red` | The style for the module when an engines property in Packages.json does not match the NodeJS version. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | -------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"⬢ "` | A format string representing the symbol of NodeJS. |
| `detect_extensions` | `["js", "mjs", "cjs", "ts"]` | Which extensions should trigger this moudle. |
| `detect_files` | `["package.json", ".node-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `["node_modules"]` | Which folders should trigger this module. |
| `style` | `"bold green"` | The style for the module. |
| `disabled` | `false` | Disables the `nodejs` module. |
| `not_capable_style` | `bold red` | The style for the module when an engines property in Packages.json does not match the NodeJS version. |

### Variables

Expand Down
6 changes: 6 additions & 0 deletions src/configs/nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub struct NodejsConfig<'a> {
pub style: &'a str,
pub disabled: bool,
pub not_capable_style: &'a str,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> {
Expand All @@ -19,6 +22,9 @@ impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> {
style: "bold green",
disabled: false,
not_capable_style: "bold red",
detect_extensions: vec!["js", "mjs", "cjs", "ts"],
detect_files: vec!["package.json", ".node-version"],
detect_folders: vec!["node_modules"],
}
}
}
16 changes: 5 additions & 11 deletions src/modules/nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ use std::ops::Deref;
use std::path::Path;

/// 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`, `.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 mut module = context.new_module("nodejs");
let config = NodejsConfig::try_load(module.config);
let is_js_project = context
.try_begin_scan()?
.set_files(&["package.json", ".node-version"])
.set_extensions(&["js", "mjs", "cjs", "ts"])
.set_folders(&["node_modules"])
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();

let is_esy_project = context
Expand All @@ -36,8 +32,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None;
}

let mut module = context.new_module("nodejs");
let config = NodejsConfig::try_load(module.config);
let nodejs_version = Lazy::new(|| {
context
.exec_cmd("node", &["--version"])
Expand Down

0 comments on commit c0a0e85

Please sign in to comment.