commit c45b174d0b22b2e6598fcbdc589ded2024e98a2c
parent 86cbb50375aa5d382275f1130c9d429e28132d1d
Author: finwo <finwo@pm.me>
Date: Tue, 24 Sep 2019 14:10:59 +0200
Added common repo scripts
Diffstat:
9 files changed, 413 insertions(+), 0 deletions(-)
diff --git a/scripts/.eslintrc.js b/scripts/.eslintrc.js
@@ -0,0 +1,75 @@
+module.exports = {
+ 'env': {
+ 'browser' : true,
+ 'commonjs': true,
+ 'es6' : true,
+ 'node' : true
+ },
+ 'extends': [
+ 'eslint:recommended',
+ 'plugin:varspacing/recommended'
+ ],
+ 'globals': {
+ 'Atomics' : 'readonly',
+ 'SharedArrayBuffer': 'readonly'
+ },
+ 'parserOptions': {
+ 'ecmaVersion': 2018,
+ 'sourceType' : 'module'
+ },
+ 'plugins': [
+ 'json',
+ 'varspacing',
+ ],
+ 'rules': {
+ 'require-atomic-updates': 0, // off: https://github.com/eslint/eslint/issues/11954
+ 'indent' : [
+ 'error',
+ 2,
+ {
+ 'SwitchCase' : 1,
+ 'VariableDeclarator': 'first'
+ }
+ ],
+ 'space-in-parens': [
+ 'error',
+ 'never',
+ ],
+ 'eol-last': [
+ 'error',
+ 'always',
+ ],
+ 'keyword-spacing': [
+ 'error',
+ {
+ 'before': true,
+ 'after' : true,
+ },
+ ],
+ 'linebreak-style': [
+ 'error',
+ 'unix'
+ ],
+ 'quotes': [
+ 'error',
+ 'single'
+ ],
+ 'semi': [
+ 'error',
+ 'always'
+ ],
+ 'no-trailing-spaces': [
+ 'error'
+ ],
+ 'comma-dangle': [
+ 'error',
+ 'always-multiline'
+ ],
+ 'key-spacing': [
+ 'error',
+ {
+ 'align': 'colon',
+ }
+ ],
+ }
+};
diff --git a/scripts/install-gcloud-sdk.sh b/scripts/install-gcloud-sdk.sh
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+
+# Ensure bash in approot
+[ "$BASH" ] || exec bash $0 "$@"
+cd "$(dirname "$0")/.."
+
+# Ensure curl is there
+command -v curl &>/dev/null || \
+ scripts/install-system-package.sh curl
+
+# Install if missing
+[ -d "${HOME}/google-cloud-sdk" ] || {
+ curl https://sdk.cloud.google.com | bash /dev/stdin --disable-prompts >/dev/null
+}
+
+# Setup path if not loaded yet
+command -v gcloud || {
+ export PATH="${HOME}/google-cloud-sdk/bin:${PATH}"
+}
diff --git a/scripts/install-system-package.sh b/scripts/install-system-package.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+
+# Lists of how to install stuff
+declare -A PACKAGES
+PACKAGES[linux,alpine,x86_64,curl]="apk add curl"
+PACKAGES[linux,alpine,x86_64,make]="apk add make"
+PACKAGES[linux,debian,x86_64,curl]="apt-get install -y curl"
+PACKAGES[linux,debian,x86_64,make]="apt-get install -y make"
+PACKAGES[linux,gentoo,x86_64,curl]="emerge net-misc/curl"
+PACKAGES[linux,gentoo,x86_64,make]="echo \"make is already in the base image\""
+
+# List of requested packages
+REQUESTED=()
+
+# Defaults
+ARCH="unknown"
+PLATFORM="unknown"
+DISTRO="unknown"
+
+# Detect OS
+if [[ `uname` == "Linux" ]]; then
+ PLATFORM="linux"
+
+ ARCH=$(uname -m)
+ if [ -n "$(command -v lsb_release)" ]; then
+ DISTRO=$(lsb_release -s -d)
+ elif [ -f "/etc/os-release" ]; then
+ DISTRO=$(grep ID /etc/os-release | sed 's/ID=//g' | tr -d '="')
+ elif [ -f "/etc/debian_version" ]; then
+ DISTRO="debian"
+ elif [ -f "/etc/redhat-release" ]; then
+ DISTRO=$(cat /etc/redhat-release)
+ fi
+
+fi
+
+# Iterate through requested packages
+while (( "$#" )); do
+ case "$1" in
+ -h|--help)
+ echo ""
+ echo "Usage: $0 [options] <package> [package] [...]"
+ echo ""
+ echo "Options:"
+ echo " -h --help Show this usage"
+ echo " -l --list Show a list of supported packages"
+ echo ""
+ exit 0
+ ;;
+ -l|--list)
+ printf "%-8s | %-8s | %-8s | %-8s \n" "Platform" "Distro" "Arch" "Package"
+ echo -en "-------- | -------- | -------- | -------- \n"
+ for details in "${!PACKAGES[@]}"; do
+ echo $details
+ done | sort | while IFS=, read platform distro arch pkg; do
+ printf "%-8s | %-8s | %-8s | %-8s \n" "$platform" "$distro" "$arch" "$pkg"
+ done
+ ;;
+ *)
+ REQUESTED+=("$1")
+ ;;
+ esac
+ shift
+done
+
+for pkg in "${REQUESTED[@]}"; do
+ CMD=${PACKAGES[$PLATFORM,$DISTRO,$ARCH,$pkg]}
+ if [ -z "$CMD" ]; then
+ echo "Package '$pkg' not found or system not supported" >&2
+ echo " PLATFORM: $PLATFORM" >&2
+ echo " ARCH : $ARCH" >&2
+ echo " DISTRO : $DISTRO" >&2
+ echo " PACKAGE : $pkg" >&2
+ exit 1
+ fi
+ bash -c "$CMD" || exit $?
+done
+
+
diff --git a/scripts/json.js b/scripts/json.js
@@ -0,0 +1,101 @@
+#!/usr/bin/env node
+
+// Makes usage easier to write
+function print(str, ...args) {
+ for (let index in args) {
+ if (!Object.prototype.hasOwnPropert.call(args,index)) continue;
+ str = str.split('{'+index+'}').join(args[index]);
+ }
+ return process.stdout.write(str);
+}
+
+// We'll use this a lot
+function getRef(ref, path) {
+ if ('string' === typeof path) path = path.split('.');
+ if (!Array.isArray(path)) return undefined;
+ path = path.slice();
+ while (path.length) {
+ let key = path.shift();
+ ref = ref[key] = ref[key] || {};
+ }
+ return ref;
+}
+
+function pathParent(path) {
+ if ('string' === typeof path) path = path.split('.');
+ path = path.slice();
+ path.pop();
+ return path;
+}
+
+function pathBase(path) {
+ if ('string' === typeof path) path = path.split('.');
+ return path.slice().pop();
+}
+
+const fs = require('fs');
+const argv = require('minimist')(process.argv.slice(2));
+const commands = {
+ set: {
+ args: 2,
+ fn : function(db, key, value) {
+ let last = pathBase(key);
+ let ref = getRef(db,pathParent(key));
+ try {
+ ref[last] = JSON.parse(value);
+ } catch (e) {
+ ref[last] = value;
+ }
+ },
+ },
+ push: {
+ args: 2,
+ fn : function(db, key, value) {
+ let last = pathBase(key);
+ let ref = getRef(db,pathParent(key));
+ ref = ref[last] = ref[last] || [];
+ try {
+ ref.push(JSON.parse(value));
+ } catch (e) {
+ ref.push(value);
+ }
+ },
+ },
+};
+
+
+// Handle help (including subjects)
+if (argv.help) {
+ switch (argv.help) {
+ default:
+ print('\n');
+ print('Usage: {0} --file <file> [options] command {arguments} [ command {arguments} [..] ]\n', process.argv[1].split('/').pop());
+ print('\n');
+ print('Options:\n');
+ print(' --help [subject] Show global usage or subject specific\n');
+ print('\n');
+ print('Commands:\n');
+ print(' set <key> <value> Set value at a specific path\n');
+ print(' push <key> <value> Push value at a specific path\n');
+ print('\n');
+ break;
+ }
+ process.exit(0);
+}
+
+// Load the file
+const db = require(process.cwd()+'/'+argv.file);
+
+(async function next () {
+ if (!argv._.length) return;
+ const command = argv._.shift();
+ if (!commands[command]) return next();
+ const cmd = commands[command];
+ const args = argv._.slice(0,cmd.args);
+ argv._ = argv._.slice(cmd.args);
+ await cmd.fn(db,...args);
+ fs.writeFile(process.cwd()+'/'+argv.file, JSON.stringify(db,null,2)+'\n', err => {
+ if (err) throw err;
+ next();
+ });
+})();
diff --git a/scripts/lint.sh b/scripts/lint.sh
@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+
+# Ensure bash in approot
+[ "$BASH" ] || exec bash $0 "$@"
+cd "$(dirname "$0")/.."
+
+# Load assert lib
+HAS_ASSERT=
+if [ -f "lib/assert/assert.sh" ]; then
+ source 'lib/assert/assert.sh'
+ HAS_ASSERT=1
+fi
+
+# Parse arguments
+filename=
+opts=()
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ --fix)
+ opts+=("--fix")
+ ;;
+ *)
+ filename=$1
+ ;;
+ esac
+ shift
+done
+
+node_modules/.bin/eslint --cache "${filename}" ${opts[@]}
+if [ "$?" == 0 ]; then
+ [ "$HAS_ASSERT" ] && \
+ log_success "${filename}" || \
+ echo -e "\e[42;30m PASS \e[0m ${filename}"
+else
+ [ "$HAS_ASSERT" ] && \
+ log_failure "${filename}" || \
+ echo -e "\e[41;30m FAIL \e[0m ${filename}"
+ exit 1
+fi
diff --git a/scripts/linter.sh b/scripts/linter.sh
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+
+# Ensure bash in approot
+[ "$BASH" ] || exec bash $0 "$@"
+cd "$(dirname "$0")/.."
+
+
+# Install deps if needed
+[ -f ".eslintrc.js" ] || cp scripts/.eslintrc.js .
+[ -f "node_modules/.bin/eslint" ] || npm install --save-dev eslint
+[ -d "node_modules/eslint-plugin-json" ] || npm install --save-dev eslint-plugin-json
+[ -d "node_modules/eslint-plugin-varspacing" ] || npm install --save-dev eslint-plugin-varspacing
+
+# Whether there as an error
+error=0
+
+# Find and lint all javascript files
+xargs -n 1 -P $(( $(nproc) + 1 )) scripts/lint.sh "$@" < <( find -name '*.js' | \
+ sort | \
+ grep -v node_modules | \
+ egrep -v "^\.\/api\/_site" | \
+ egrep -v "^\.\/frontend\/docroot\/assets\/" | \
+ egrep -v "\/dist\/" | \
+ egrep -v "^\.\/lib\/js-interpreter\/" | \
+ egrep -v "^\.\/public\/" | \
+ egrep -v "^\.\/google-cloud-sdk\/" | \
+ egrep -v '\/\.eslintrc\.js$' | \
+ egrep -v "\.min\.js\$"
+) || error=1
+
+# Find and lint all json files
+xargs -n 1 -P $(( $(nproc) + 1 )) scripts/lint.sh "$@" < <( find -name '*.json' | \
+ sort | \
+ grep -v node_modules | \
+ egrep -v "^\.\/frontend\/docroot\/assets\/" | \
+ egrep -v '\/\.eslintrc\.json$' | \
+ egrep -v '\/\.nyc_output\/' | \
+ egrep -v "^\.\/google-cloud-sdk\/" | \
+ egrep -v '\/bower\.json$'
+) || error=1
+
+exit $error
diff --git a/scripts/postinstall.sh b/scripts/postinstall.sh
@@ -0,0 +1,34 @@
+#!/usr/bin/env bash
+
+# Ensure bash in approot
+[ "$BASH" ] || exec bash $0 "$@"
+cd "$(dirname "$0")/.."
+
+# Reset values in package.json
+scripts/json.js --file package.json set contributors []
+
+# Update contibutors list
+git log --pretty="%ce %cn" | sort | uniq -c | while read line; do
+ arr=($line)
+
+ # Decompose
+ commits=${arr[0]}
+ email=${arr[1]}
+ name="${arr[@]:2}"
+
+ # Push contributor to package.json
+ scripts/json.js --file package.json push contributors "$(cat <<EOF
+{
+ "name": "$name",
+ "email": "$email",
+ "contributions": $commits
+}
+EOF
+)"
+done
+
+# Build client
+[ -d lib/client ] && {
+ ( cd lib/client && git checkout package.json && git reset --hard HEAD && git pull origin master && npm install && npm run build );
+ cp lib/client/dist/* public;
+} || echo -en ""
diff --git a/scripts/preinstall.sh b/scripts/preinstall.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+# Ensure bash in approot
+[ "$BASH" ] || exec bash $0 "$@"
+cd "$(dirname "$0")/.."
+
+# # Fetch the indicated versions
+# git submodule update --init --recursive --force
+
+# Checkout master everywhere
+git submodule foreach --recursive git checkout master
+git submodule foreach --recursive git pull
diff --git a/scripts/test.sh b/scripts/test.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+# Ensure bash in approot
+[ "$BASH" ] || exec bash $0 "$@"
+cd "$(dirname "$0")/.."
+
+# Ensure our dependencies are installed
+[ -f "node_modules/.bin/tape" ] || npm install --save-dev tape
+[ -d "node_modules/esm" ] || npm install --save-dev esm
+
+FILES=$(find -name '*.test.js' | egrep -v '\/node_modules\/')
+node_modules/.bin/tape -r esm ${FILES}