commit: 41738037d29a04097449aa217e5db16e9ea84df0
parent 07b52a0a9c4be140317ca1101083eac71f2d5f87
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 16 Sep 2023 06:41:52 +0200
nodejs.eclass: Handle package.json as close enough to spec as possible
Diffstat:
1 file changed, 56 insertions(+), 2 deletions(-)
diff --git a/eclass/nodejs.eclass b/eclass/nodejs.eclass
@@ -1,4 +1,4 @@
-# Copyright 2022 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
+# Copyright 2022-2023 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: nodejs.eclass
@@ -11,6 +11,8 @@
# @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)
;;
@@ -19,11 +21,12 @@ case "${EAPI:-0}" in
;;
esac
-EXPORT_FUNCTIONS src_test
+EXPORT_FUNCTIONS src_compile src_test src_install
BDEPEND="
net-libs/nodejs
app-misc/jq
+ sys-apps/cmd-glob
"
RDEPEND="dev-nodejs/node_path"
@@ -38,3 +41,54 @@ nodejs_src_test() {
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")' <package.json >/dev/null
+ then
+ npm run install || die
+ 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
+}
+
+nodejs_src_install() {
+ # https://docs.npmjs.com/cli/v6/configuring-npm/package-json/#files
+ insinto "${NODEJS_SITELIB}${PN}"
+ doins package.json
+
+ if jq -e 'has("files")' <package.json >/dev/null
+ then
+ jq -r .files[] <package.json \
+ | xargs -d '\n' glob -m \
+ | while read file; do
+ doins -r "${file}"
+ done
+ else
+ doins -r .
+ fi
+
+ if jq -e 'has("main")' <package.json >/dev/null
+ then
+ doins "$(jq -r -e '.main' <package.json)"
+ else
+ test -e index.js && doins index.js
+ fi
+
+ if jq -e 'has("bin")' <package.json >/dev/null
+ then
+ jq -r '.bin | to_entries | .[] | .key + " " + .value' <package.json \
+ | while read bin file; do
+ doins "${file}"
+ fperms 755 "${NODEJS_SITELIB}${PN}/${file#./}"
+ dosym "${NODEJS_SITELIB}${PN}/${file#./}" "/usr/bin/${bin}"
+ done
+ fi
+}