#!/bin/bash

#==================================================
# Get the directory of the script file
#==================================================
# Using pwd alone will not work if you are not running the script from
# the directory it is contained in.
#
# $() acts as a kind of quoting for commands but they're run in their own context.
# dirname gives you the path portion of the provided argument (removing the file key).
#
# This command gets the script's source file pathname, strips it to just
# the path portion, cds to that path, then uses pwd to return the (effectively)
# full path of the script. This is assigned to the variable. After all of
# that, the context is unwound so you end up back in the directory you started
# at but with an environment variable containing the script's path.
dirScript="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
dirPackages=${dirScript}/../packages/;

function updateAllModules() {
    ls;
#    sudo ncu -u;
#    sudo npm i --force;
#    npm run build;
#    npm run bump;
}

### Print a list of directories 1 level below the starting directory that contain package.json files.
function printPackageDirectoryAll() {
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 2 -print0 | xargs -0 -n1 dirname | sort --unique | echo ${1}/boo
}

### Create a .npmrc file in all package directories and write the text 'package-lock=false' into it.
### This will prevent npm from automatically creating package-lock.json files.
function disablePackageLockAll() {
    cd $$irPackages;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'echo "package-lock=false" >> .npmrc' \;
}

### Remove package-lock.json files from all package directories.
function removePackageLockFileAll() {
    cd $dirPackages;
    ls;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'sudo rm "package-lock.json" "yarn.lock" "yarn-error.log"' \;
}

function installAll() {
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'sudo yarn install' \;
}

function upgradeAll() {
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'sudo yarn upgrade' \;
}

function removeNodeModulesAll() {
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'sudo rm -fr node_modules' \;
}

### Find all the folders that have a package.json file and execute the given script within each.
function nested() {
    cd $dirPackages;
    find . -name package.json -maxdepth 2 -execdir sudo ncu -u \;
    cd $dirPackages;
    find . -name package.json -maxdepth 2 -execdir sudo npm update \;
}

function gitCommitAll() {
    local message="$(read -p 'Commit message: ')";
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'sudo git add . && sudo git add -u && sudo git commit -m "${message}" && sudo git push' \;
}

function buildAll() {
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 3 -execdir sh -c 'sudo yarn build' \;
}

function npmUpdateScriptsAll() {
    local script="";
    local path="../../bin";

    ### Remove any existing scripts
    cd $dirPackages;
    find . -name 'package.json' -maxdepth 2 -execdir sh -c 'jq ".scripts = { }" package.json > package_temp.json && mv package_temp.json package.json' \;

    ### These scripts use node-jq to parse json files. Must be installed globally
    ### https://www.npmjs.com/package/node-jq

    ### clean
    cd $dirPackages;
    script="jq '.scripts.clean = \"source ${path}/clean.sh; clean;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;

    ### permit
    cd $dirPackages;
    script="jq '.scripts.permit = \"source ${path}/permit.sh; permit;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;

    ### pretest
    cd $dirPackages;
    script="jq '.scripts.pretest = \"source ${path}/pretest.sh; pretest;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;

    ### test
    cd $dirPackages;
    script="jq '.scripts.test = \"source ${path}/test.sh; test;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;

    ### bump
    cd $dirPackages;
    script="jq '.scripts.bump = \"source ${path}/npm-bump.sh; bump;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;

    ### build
    cd $dirPackages;
    script="jq '.scripts.build = \"source ${path}/build.sh; build;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;

    ### publish
    cd $dirPackages;
    script="jq '.scripts.publish = \"source ${path}/npm-publish.sh; publish;\"' package.json > package_temp.json && mv package_temp.json package.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;
}

function npmUpdateTsconfigAll() {
    local script="";

    ### experimentalDecorators
    cd $dirPackages;
    script="jq '.compilerOptions.experimentalDecorators = true' tsconfig.json > tsconfig_temp.json && mv tsconfig_temp.json tsconfig.json";
    find . -name 'package.json' -maxdepth 2 -execdir sh -c "${script}" \;
}
