# Copyright 2022-2023 Haelwenn (lanodan) Monnier # Distributed under the terms of the GNU General Public License v2 # @ECLASS: nodejs.eclass # @MAINTAINER: # Haelwenn (lanodan) Monnier # @AUTHOR: # Haelwenn (lanodan) Monnier # @SUPPORTED_EAPIS: 7 8 # @BLURB: Build NodeJS projects # @DESCRIPTION: # An eclass providing functions to build NodeJS projects # https://docs.npmjs.com/cli/v6/configuring-npm/package-json/ case "${EAPI:-0}" in 7|8) ;; *) die "Unsupported EAPI=${EAPI} for ${ECLASS}" ;; esac EXPORT_FUNCTIONS src_compile src_test src_install BDEPEND=" net-libs/nodejs app-misc/jq sys-apps/cmd-glob " RDEPEND="dev-nodejs/node_path" NODEJS_SITELIB="/usr/share/nodejs/" NPM_FLAGS=( --audit false --offline --verbose ) enpm() { debug-print-function ${FUNCNAME} "$@" set -- npm "${NPM_FLAGS[@]}" "$@" echo "$@" >&2 "$@" || die } nodejs_src_test() { if jq -e '.scripts | has("test")' /dev/null then # --ignore-scripts: do not run pretest and posttest enpm run test --ignore-scripts else die 'No "test" command defined in package.json' fi } nodejs_src_compile() { # https://docs.npmjs.com/cli/v10/configuring-npm/package-json/#default-values if jq -e '.scripts | has("install")' /dev/null then if jq -e '.scripts | has("preinstall")' /dev/null then enpm run preinstall fi npm "${NPM_FLAGS[@]}" run install || die if jq -e '.scripts | has("postinstall")' /dev/null then enpm run postinstall fi else if test -e binding.gyp; then if has_version -b dev-nodejs/node-gyp; then node-gyp rebuild || die else # TODO: Check BDEPEND as QA die "binding.gyp found but dev-nodejs/node-gyp is not available as BDEPEND" fi fi fi if jq -e '.scripts | has("prepare")' /dev/null then enpm run prepare fi } # Install files in nodejs hierarchy with preserving path of source files nodejs_install_path() { for file in "$@"; do target_dir="${ED}/${NODEJS_MODULE_DIR}/$(dirname "${file}")/" mkdir -p "${target_dir}" || die "Failed to create directory for ${file}" cp -r "${file}" "${target_dir}" || die "Failed to copy ${file}" done } nodejs_src_install() { einstalldocs # https://docs.npmjs.com/cli/v6/configuring-npm/package-json/#files if jq -e '.type == "module"' /dev/null then # Assume that everything on NPM is using semver NODEJS_MODULE_DIR="${NODEJS_SITELIB}${PN}@$(ver_cut 1)" [[ "${SLOT}" != "$(ver_cut 1)" ]] && eqawarn "Got SLOT value ${SLOT} while ES Module would expect $(ver_cut 1)" else NODEJS_MODULE_DIR="${NODEJS_SITELIB}${PN}" fi insinto "${NODEJS_MODULE_DIR}" doins package.json if jq -e 'has("files")' /dev/null then jq -r .files[] /dev/null then main="$(jq -r -e '.main'