summaryrefslogtreecommitdiffstats
path: root/meta/classes/npm.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/npm.bbclass')
-rw-r--r--meta/classes/npm.bbclass63
1 files changed, 42 insertions, 21 deletions
diff --git a/meta/classes/npm.bbclass b/meta/classes/npm.bbclass
index ba50fcac20..45e6b4fac7 100644
--- a/meta/classes/npm.bbclass
+++ b/meta/classes/npm.bbclass
@@ -19,7 +19,7 @@
inherit python3native
-DEPENDS:prepend = "nodejs-native "
+DEPENDS:prepend = "nodejs-native nodejs-oe-cache-native "
RDEPENDS:${PN}:append:class-target = " nodejs"
EXTRA_OENPM = ""
@@ -46,6 +46,7 @@ NPM_ARCH ?= "${@npm_target_arch_map(d.getVar("TARGET_ARCH"))}"
NPM_PACKAGE = "${WORKDIR}/npm-package"
NPM_CACHE = "${WORKDIR}/npm-cache"
NPM_BUILD = "${WORKDIR}/npm-build"
+NPM_REGISTRY = "${WORKDIR}/npm-registry"
def npm_global_configs(d):
"""Get the npm global configuration"""
@@ -57,13 +58,36 @@ def npm_global_configs(d):
configs.append(("cache", d.getVar("NPM_CACHE")))
return configs
+## 'npm pack' runs 'prepare' and 'prepack' scripts. Support for
+## 'ignore-scripts' which prevents this behavior has been removed
+## from nodejs 16. Use simple 'tar' instead of.
def npm_pack(env, srcdir, workdir):
- """Run 'npm pack' on a specified directory"""
- import shlex
- cmd = "npm pack %s" % shlex.quote(srcdir)
- args = [("ignore-scripts", "true")]
- tarball = env.run(cmd, args=args, workdir=workdir).strip("\n")
- return os.path.join(workdir, tarball)
+ """Emulate 'npm pack' on a specified directory"""
+ import subprocess
+ import os
+ import json
+
+ src = os.path.join(srcdir, 'package.json')
+ with open(src) as f:
+ j = json.load(f)
+
+ # base does not really matter and is for documentation purposes
+ # only. But the 'version' part must exist because other parts of
+ # the bbclass rely on it.
+ base = j['name'].split('/')[-1]
+ tarball = os.path.join(workdir, "%s-%s.tgz" % (base, j['version']));
+
+ # TODO: real 'npm pack' does not include directories while 'tar'
+ # does. But this does not seem to matter...
+ subprocess.run(['tar', 'czf', tarball,
+ '--exclude', './node-modules',
+ '--exclude-vcs',
+ '--transform', r's,^\./,package/,',
+ '--mtime', '1985-10-26T08:15:00.000Z',
+ '.'],
+ check = True, cwd = srcdir)
+
+ return (tarball, j)
python npm_do_configure() {
"""
@@ -86,27 +110,24 @@ python npm_do_configure() {
from bb.fetch2.npm import npm_unpack
from bb.fetch2.npmsw import foreach_dependencies
from bb.progress import OutOfProgressHandler
+ from oe.npm_registry import NpmRegistry
bb.utils.remove(d.getVar("NPM_CACHE"), recurse=True)
bb.utils.remove(d.getVar("NPM_PACKAGE"), recurse=True)
env = NpmEnvironment(d, configs=npm_global_configs(d))
+ registry = NpmRegistry(d.getVar('NPM_REGISTRY'), d.getVar('NPM_CACHE'))
- def _npm_cache_add(tarball):
- """Run 'npm cache add' for a specified tarball"""
- cmd = "npm cache add %s" % shlex.quote(tarball)
- env.run(cmd)
+ def _npm_cache_add(tarball, pkg):
+ """Add tarball to local registry and register it in the
+ cache"""
+ registry.add_pkg(tarball, pkg)
def _npm_integrity(tarball):
"""Return the npm integrity of a specified tarball"""
sha512 = bb.utils.sha512_file(tarball)
return "sha512-" + base64.b64encode(bytes.fromhex(sha512)).decode()
- def _npm_version(tarball):
- """Return the version of a specified tarball"""
- regex = r"-(\d+\.\d+\.\d+(-.*)?(\+.*)?)\.tgz"
- return re.search(regex, tarball).group(1)
-
def _npmsw_dependency_dict(orig, deptree):
"""
Return the sub dictionary in the 'orig' dictionary corresponding to the
@@ -163,11 +184,11 @@ python npm_do_configure() {
with tempfile.TemporaryDirectory() as tmpdir:
# Add the dependency to the npm cache
destdir = os.path.join(d.getVar("S"), destsuffix)
- tarball = npm_pack(env, destdir, tmpdir)
- _npm_cache_add(tarball)
+ (tarball, pkg) = npm_pack(env, destdir, tmpdir)
+ _npm_cache_add(tarball, pkg)
# Add its signature to the cached shrinkwrap
dep = _npmsw_dependency_dict(cached_shrinkwrap, deptree)
- dep["version"] = _npm_version(tarball)
+ dep["version"] = pkg['version']
dep["integrity"] = _npm_integrity(tarball)
if params.get("dev", False):
dep["dev"] = True
@@ -184,7 +205,7 @@ python npm_do_configure() {
# Configure the main package
with tempfile.TemporaryDirectory() as tmpdir:
- tarball = npm_pack(env, d.getVar("S"), tmpdir)
+ (tarball, _) = npm_pack(env, d.getVar("S"), tmpdir)
npm_unpack(tarball, d.getVar("NPM_PACKAGE"), d)
# Configure the cached manifest file and cached shrinkwrap file
@@ -257,7 +278,7 @@ python npm_do_compile() {
args.append(("build-from-source", "true"))
# Pack and install the main package
- tarball = npm_pack(env, d.getVar("NPM_PACKAGE"), tmpdir)
+ (tarball, _) = npm_pack(env, d.getVar("NPM_PACKAGE"), tmpdir)
cmd = "npm install %s %s" % (shlex.quote(tarball), d.getVar("EXTRA_OENPM"))
env.run(cmd, args=args)
}