From 3cecedcdac15e813fd1aac544ebd61674a549c77 Mon Sep 17 00:00:00 2001 From: Gabe Dunn Date: Thu, 8 Jul 2021 14:32:13 -0600 Subject: [PATCH] initial commit --- LICENSE | 20 +++++++++ functions/yup | 103 +++++++++++++++++++++++++++++++++++++++++++++ readme.md | 45 ++++++++++++++++++++ zsh-yup.plugin.zsh | 8 ++++ 4 files changed, 176 insertions(+) create mode 100644 LICENSE create mode 100755 functions/yup create mode 100644 readme.md create mode 100755 zsh-yup.plugin.zsh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1257eb0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) +Copyright (c) Gabe Dunn 2021 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/functions/yup b/functions/yup new file mode 100755 index 0000000..c8b66d3 --- /dev/null +++ b/functions/yup @@ -0,0 +1,103 @@ +#!/usr/bin/env zsh + +yup () { + # check if a package manager is installed + if (( $+commands[yarn] )); then + local pm_cmd="yarn add" + elif (( $+commands[npm] )); then + local pm_cmd="npm install" + else + echo "yarn or npm not found, exiting..." + return 1 + fi + + # make sure jq is installed + if (( ! $+commands[jq] )); then + echo "jq not found, exiting..." + return 1 + fi + + # make sure there's a package.json in the current directory + if ! test -f "./package.json"; then + echo "no package.json file in current directory, exiting..." + return 1 + fi + + # set default dependency scope + local scope="dependencies" + local both=false + + # check for args + for arg in "$@"; do + # help/usage command + if test "$arg" = "-h" -o "$arg" = "--help"; then + cat << 'eof' +Usage: yup [-h/--help] [-d/--dev] [-D/--both] [-n/--npm] +Options: + -h, --help show this help message + -d, --dev update the devDependencies + -b, --both update both dependencies and devDependencies + -n, --npm update the dependencies using npm instead of yarn +eof + return 0; + fi + + # set the command to use npm if the arg is passed + if test "$arg" = "-n" -o "$arg" = "--npm"; then + pm_cmd="npm install" + fi + + # set scope to devDependencies if the arg is passed + if test "$arg" = "-d" -o "$arg" = "--dev"; then + scope="devDependencies" + pm_cmd="$pm_cmd --save-dev" + elif test "$arg" = "-b" -o "$arg" = "--both"; then + both=true + fi + done + + # grab deps from package.json + # start with empty object and add the deps + local deps="$(jq "{}+.$scope|keys|map(.+\" \")+[\"\"]|add" ./package.json | sed -E 's/^\"//; s/( )?\"$//')" + + # if there aren't any dependencies, exit the function + if test -z "$deps"; then + echo "no $scope in package.json" + return 0 + fi + + # set the command to run + local cmd_to_run="$pm_cmd $deps" + + # run the command + echo "Updating $scope..." + eval "$cmd_to_run" + + # if the both arg was passed, run the command again, but for devDependencies + if $both; then + # change the scope to devDependencies + pm_cmd="$pm_cmd --save-dev" + scope="devDependencies" + + # grab the deps again + deps="$(jq "{}+.$scope|keys|map(.+\" \")+[\"\"]|add" ./package.json | sed -E 's/^\"//; s/( )?\"$//')" + + # if there aren't any dependencies, exit the function + if test -z "$deps"; then + echo "no $scope in package.json" + return 0 + fi + + # set the command to run + cmd_to_run="$pm_cmd $deps" + + # run the command + echo "Updating $scope..." + eval "$cmd_to_run" + fi + + # end the function + echo "Upgrades finished!" + return 0 +} + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..434ce96 --- /dev/null +++ b/readme.md @@ -0,0 +1,45 @@ +# yup +> yup is a zsh plugin that provides a function upgrades all dependencies in a +> yarn/npm project. + +## Installing + +### zinit +Add this to your zinit config (.zshrc): +```zsh +zinit light redxtech/zsh-yup + +# it also works with turbo mode: +zinit ice wait lucid +zinit load redxtech/zsh-yup +``` + +### oh-my-zsh +Install it with your favourite zsh package manager, or clone it directly to your +`$ZSH_CUSTOM/plugins` directory with git, and add `zsh-yup` to the plugins +array in your `.zshrc` file: + +```zsh +plugins=(... zsh-yup) +``` + +## Usage +`yup` is very easy to use, simply run the command: + +```zsh +yup +``` + +There are some options to pass to the command: +```zsh +Usage: yup [-h/--help] [-d/--dev] [-D/--both] [-n/--npm] +Options: + -h, --help show this help message + -d, --dev update the devDependencies + -b, --both update both dependencies and devDependencies + -n, --npm update the dependencies using npm +``` + +## Author +**yup** © [Gabe Dunn](https://github.com/redxtech), Released under the [MIT](./license.md) License. + diff --git a/zsh-yup.plugin.zsh b/zsh-yup.plugin.zsh new file mode 100755 index 0000000..c4c8569 --- /dev/null +++ b/zsh-yup.plugin.zsh @@ -0,0 +1,8 @@ +#!/usr/bin/env zsh + +# zsh plugin to update all dependencies of a yarn/npm project + +# add the functions to the function path & autoload them +fpath+=("$(dirname ${0})/functions") +autoload -Uz yup +