Skip to content

Commit

Permalink
feat(package): add support for package version from lerna.json instea…
Browse files Browse the repository at this point in the history
…d of package.json
  • Loading branch information
bryanvaz committed Jan 26, 2021
1 parent f0ff567 commit f09b9da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ Mercurial status indicators is shown only when you have dirty repository.
Package version is shown when repository is a package.

* **npm**`npm` package contains a `package.json` file. We use `jq`, `python` to parse package version for improving performance and `node` as a fallback. Install [jq](https://stedolan.github.io/jq/) for **improved performance** of this section ([Why?](./Troubleshooting.md#why-is-my-prompt-slow))
* **lerna**`lerna` monorepo contains a `lerna.json` file. We use `jq`, `python` to parse package version for improving performance and `node` as a fallback. Install [jq](https://stedolan.github.io/jq/) for **improved performance** of this section (same reason as npm).
* **cargo**`cargo` package contains a `Cargo.toml` file. Currently, we use `cargo pkgid`, it depends on `Cargo.lock`. So if package version isn't shown, you may need to run some command like `cargo build` which can generate `Cargo.lock` file.

> **Note:** This is the version of the package you are working on, not the version of package manager itself.
Expand Down
21 changes: 21 additions & 0 deletions sections/package.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ spaceship_package() {
fi
fi

# Show package version from lerna.json if is a lerna monorepo
# Note: lerna does not have to be installed in the global context
# so checking for lerna binary does not make sense
if [[ -f lerna.json ]] && spaceship::exists npm; then
local 'lerna_package_version'
if spaceship::exists jq; then
lerna_package_version=$(jq -r '.version' lerna.json 2>/dev/null)
elif spaceship::exists python; then
lerna_package_version=$(python -c "import json; print(json.load(open('lerna.json'))['version'])" 2>/dev/null)
elif spaceship::exists node; then
lerna_package_version=$(node -p "require('./lerna.json').version" 2> /dev/null)
fi
if [[ ! -z "$lerna_package_version" || "$package_version" != "null" || "$package_version" != "undefined" ]]; then
if [[ "$lerna_package_version" == "independent" ]]; then
package_version="($lerna_package_version)"
else
package_version="${lerna_package_version}"
fi
fi
fi

if [[ -f Cargo.toml ]] && spaceship::exists cargo; then
# Handle missing field `version` in Cargo.toml.
# `cargo pkgid` need Cargo.lock exists too. If it does't, do not show package version
Expand Down

0 comments on commit f09b9da

Please sign in to comment.