spiegel-keyman/developer/server/build.sh
Marc Durdin 4d94786852 chore(web): replace lerna with npm workspaces and ts projects
Fixes #6320.

Part 1 of moving from lerna to a simpler, maintained monorepo solution,
using TypeScript Projects and NPM Workspaces.

There is more work to be done here. At this point, KeymanWeb builds and
runs without errors, but the built file is substantially different,
mostly in include order.

Using TypeScript Projects, we move away from the need to run build
scripts in various locations for almost all the Typescript modules.

TODO: Embedded versions and tests have not been verified.

TODO: developer/server is not yet verified.

TODO: developer/js (needs a rename!) is not yet verified.

TODO: Currently, the predictive-text folder needs refactoring to move
the construction of the worker wrapper out of the Predictive Text build
and into the final assembly of keymanweb.js (as it should be valid to
run it as a separate .js anyway).

TODO: Most of the `<reference>` paths need to be re-verified. Ideally
there should be no references outside the current module for any given
.ts.

TODO: The embedded vs browser vs node (headless) builds should be tidied
up for consistency so that it's obvious what depends on what. This is
currently messiest in the predictive-text folder, where the output names
diverge from the filenames and the various files are mixed in the same
folder (as evidenced by the exclusions listed in each tsconfig.json).

TODO: `npm install` should be removed from most build scripts and
instead `npm ci` (#6196) should be run only once from the top-level
folder for any given build. I've had eliminated side-effects from the
`install` action for npm, which makes it easier to reason about state.

TODO: verify_npm_setup and related functions can probably be eliminated.

TODO: most of the build scripts should be largely eliminated for web.

TODO: several ts projects use inconsistent output folders.

TODO: it may be possible to generate a .d.ts for models/types so that
we can use a consistent reference for those as well.

TODO: build.sh, tsconfig.json should always be in the module's top-level
folder, not in a subfolder such as src (e.g. see input-processor/src,
keyboard-processor/src, web/source).

TODO: resources/web-environment should be in common/web.

TODO: other js node_modules imports should be wrapped like es6-shim.

TODO: fix up the publish code for npm modules

TODO: eliminate version numbers from package.json if possible?

Whew, that's most of the stuff I noticed!
2022-04-12 21:02:33 +10:00

174 lines
4.5 KiB
Bash

#!/usr/bin/env bash
#
# Compiles Keyman Developer Server for deployment
#
# Exit on command failure and when using unset variables:
set -eu
## START STANDARD BUILD SCRIPT INCLUDE
# adjust relative paths as necessary
THIS_SCRIPT="$(greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null || readlink -f "${BASH_SOURCE[0]}")"
. "$(dirname "$THIS_SCRIPT")/../../resources/build/build-utils.sh"
. "$KEYMAN_ROOT/resources/shellHelperFunctions.sh"
## END STANDARD BUILD SCRIPT INCLUDE
EX_USAGE=64
pushd "$(dirname "$THIS_SCRIPT")"
# Build the main script.
build () {
npm run build || fail "Could not build top-level JavaScript file."
}
display_usage ( ) {
echo "Usage: $0 [--production|--watch] [--test|--tdd]"
echo " $0 --help"
echo
echo " --help displays this screen and exits"
echo " --production, -p builds production release in build/"
echo " --skip-package-install, -S don't run npm install (not valid with --production)"
echo " --test, -t runs unit tests after building"
#echo " --watch, -w builds dev server in watch mode"
echo " --tdd runs unit tests WITHOUT building"
echo " --no-build-addins don't build/copy Win32 addins"
echo " --no-build-kmw don't build KeymanWeb"
echo " --no-copy-kmw don't copy KeymanWeb"
}
################################ Main script ################################
run_tests=0
production=0
build=1
install_dependencies=1
build_keymanweb=1
copy_keymanweb=1
build_addins=1
# Process command-line arguments
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
--help|-h|-?)
display_usage
exit
;;
--production|-p)
production=1
;;
--skip-package-install|-S)
install_dependencies=0
;;
--no-build-kmw)
build_keymanweb=0
;;
--no-build-addins)
build_addins=0
;;
--no-copy-kmw)
copy_keymanweb=0
;;
--test)
run_tests=1
;;
--tdd)
run_tests=1
build=0
install_dependencies=0
;;
*)
echo "$0: invalid option: $key"
display_usage
exit $EX_USAGE
esac
shift # past the processed argument
done
# ----------------------------------------
# Install dependencies
# ----------------------------------------
# Check if Node.JS/npm is installed.
type npm >/dev/null ||\
fail "Build environment setup error detected! Please ensure Node.js is installed!"
if (( install_dependencies )) ; then
verify_npm_setup
npm install --production=false
# See https://github.com/bubenshchykov/ngrok/issues/254, https://github.com/bubenshchykov/ngrok/pull/255
rm -f node_modules/ngrok/bin/ngrok.exe
fi
set_npm_version || fail "Setting version failed."
# ----------------------------------------
# Rebuild and bundle addins
# ----------------------------------------
if (( build_addins )); then
. ./build-addins.inc.sh
# If we have an x64 version of node installed
if [[ $(isNodeX64) ]]; then
build_addins
fi
# Build with the Keyman Developer x86 version of node
PATH="$KEYMAN_ROOT/developer/inst/dist:$PATH" build_addins
fi
# ----------------------------------------
# Build and bundle KeymanWeb
# ----------------------------------------
if (( build_keymanweb )); then
pushd "$KEYMAN_ROOT/web/source"
./build.sh -no_minify
popd
fi
if (( copy_keymanweb )); then
SRC="$KEYMAN_ROOT/web/release/unminified"
DST="$(dirname "$THIS_SCRIPT")/src/site/resource"
rm -rf "$DST"
mkdir -p "$DST/osk"
mkdir -p "$DST/ui"
cp "$SRC/web/"*.js "$SRC/web/"*.js.map "$DST/"
cp -R "$SRC/web/osk/"* "$DST/osk/"
cp -R "$SRC/web/ui/"* "$DST/ui/"
cp "$KEYMAN_ROOT/web/LICENSE" "$DST/"
cp "$KEYMAN_ROOT/web/README.md" "$DST/"
fi
# ----------------------------------------
# Build the project
# ----------------------------------------
npm run build || fail "Compilation failed."
echo "Typescript compilation successful."
# ----------------------------------------
# Unit tests
# ----------------------------------------
if (( run_tests )); then
npm test || fail "Tests failed"
fi
# ----------------------------------------
# Deploy to dist/
# ----------------------------------------
if (( production )) ; then
# We'll build in the build/ folder
rm -rf build/
mkdir build/
cp -R dist/ package.json package-lock.json build/
cd build/
npm install --omit=dev --omit=optional
# See https://github.com/bubenshchykov/ngrok/issues/254, https://github.com/bubenshchykov/ngrok/pull/255
rm -f node_modules/ngrok/bin/ngrok.exe
cd ..
fi