From patchwork Mon Feb 17 23:43:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nicolas Graves X-Patchwork-Id: 38799 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 472CD27BBE2; Mon, 17 Feb 2025 23:49:29 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id C2D8E27BBE9 for ; Mon, 17 Feb 2025 23:49:24 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tkArJ-0004mP-Dc; Mon, 17 Feb 2025 18:49:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tkArG-0004lk-GV for guix-patches@gnu.org; Mon, 17 Feb 2025 18:49:02 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tkArG-0005Dx-7m for guix-patches@gnu.org; Mon, 17 Feb 2025 18:49:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=lwgmqWlMsIyr7GCOXS70gge+bIh2Df5gA7T+XHAbYiI=; b=uL2ninF8Ox0Oe/hkqNJHXirVFL3FCHr0xPMZLRrgCe7Gs4WFg9RO/aMJ60LsXmb5j6iMjmLA42BLlAxsVIY7Efj4zUbk6VPPEMSWK5Xwt9BEXpbLydLogrrG1IujjTs04e0im3CTTrpPvH/fJdemO4bNd2TDnjg78bwmG7bZX+SZsSisRIEQF+Va2bq7Vsr1xmKpm9EIbNU4uwOUpxdTFryJqPuc8aR1kywhlfEdZoXbtO6024ASbl6gF0TSLMOymdiQJ6ybg71hyVLteGB39miY7Ztv4NRPI3qCqWBrRFspj3zj856SLdiLY+2kq47ntfo5puTyABblpBvSAFHAhA==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tkArG-0007Oy-1t for guix-patches@gnu.org; Mon, 17 Feb 2025 18:49:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#74900] [PATCH v2 1/6] build-system/node: Switch to (json). Resent-From: Nicolas Graves Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 17 Feb 2025 23:49:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74900 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 74900@debbugs.gnu.org Cc: jlicht@fsfe.org, d.khodabakhsh@gmail.com Received: via spool by 74900-submit@debbugs.gnu.org id=B74900.173983611828303 (code B ref 74900); Mon, 17 Feb 2025 23:49:02 +0000 Received: (at 74900) by debbugs.gnu.org; 17 Feb 2025 23:48:38 +0000 Received: from localhost ([127.0.0.1]:51163 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tkAqp-0007M3-JP for submit@debbugs.gnu.org; Mon, 17 Feb 2025 18:48:38 -0500 Received: from 5.mo560.mail-out.ovh.net ([87.98.181.248]:35473) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tkAqj-0007LB-JK for 74900@debbugs.gnu.org; Mon, 17 Feb 2025 18:48:33 -0500 Received: from director3.ghost.mail-out.ovh.net (unknown [10.109.176.96]) by mo560.mail-out.ovh.net (Postfix) with ESMTP id 4YxfVg4hy1z1fbY for <74900@debbugs.gnu.org>; Mon, 17 Feb 2025 23:48:27 +0000 (UTC) Received: from ghost-submission-5b5ff79f4f-cdqmd (unknown [10.110.168.82]) by director3.ghost.mail-out.ovh.net (Postfix) with ESMTPS id 362711FD86; Mon, 17 Feb 2025 23:48:27 +0000 (UTC) Received: from ngraves.fr ([37.59.142.108]) by ghost-submission-5b5ff79f4f-cdqmd with ESMTPSA id Mc3aN8rKs2cX6AIA6KgjNw (envelope-from ); Mon, 17 Feb 2025 23:48:27 +0000 Authentication-Results: garm.ovh; auth=pass (GARM-108S002f23f5fba-8074-470a-a5d1-bcb9b4536151, C3652AECFC9F6A3CBC46B0867312B59102225E78) smtp.auth=ngraves@ngraves.fr X-OVh-ClientIp: 90.92.117.144 Date: Tue, 18 Feb 2025 00:43:52 +0100 Message-ID: <20250217234823.10533-2-ngraves@ngraves.fr> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250217234823.10533-1-ngraves@ngraves.fr> References: <20250217234823.10533-1-ngraves@ngraves.fr> MIME-Version: 1.0 X-Ovh-Tracer-Id: 14612773415954866930 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdehleejjecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemucehtddtnecunecujfgurhephffvvefufffkofgjfhggtgfgsehtkeertdertdejnecuhfhrohhmpefpihgtohhlrghsucfirhgrvhgvshcuoehnghhrrghvvghssehnghhrrghvvghsrdhfrheqnecuggftrfgrthhtvghrnhepteeghefhfeetvdevueevhfefjeegfefgleeuvdeutdduudeljeduveehkefgvdfgnecuffhomhgrihhnpehgnhhurdhorhhgpdhgihhthhhusgdrtghomhenucfkphepuddvjedrtddrtddruddpledtrdelvddruddujedrudeggedpfeejrdehledrudegvddruddtkeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeduvdejrddtrddtrddupdhmrghilhhfrhhomhepnhhgrhgrvhgvshesnhhgrhgrvhgvshdrfhhrpdhnsggprhgtphhtthhopedupdhrtghpthhtohepjeegledttdesuggvsggsuhhgshdrghhnuhdrohhrghdpoffvtefjohhsthepmhhoheeitdgmpdhmohguvgepshhmthhpohhuth DKIM-Signature: a=rsa-sha256; bh=lwgmqWlMsIyr7GCOXS70gge+bIh2Df5gA7T+XHAbYiI=; c=relaxed/relaxed; d=ngraves.fr; h=From; s=ovhmo4487190-selector1; t=1739836107; v=1; b=SJcd5TKswbDVxVYoeQXjxL4u+7fUvmSwq2lb9YyrntbtEmajf4ErrbMgqoS+nQmhyrtNh1e+ WHdlBfCVECsRTPrtIitBrvVYnbF0YDCNuimCuJ4SXW44oyfzYPrqTTtzvDX2Y/F3+dOFDe6GFSY OLY3ZYb/vUEj1kUuaBNb3r7Ar1wVzPpFlin32nl0N6kM74TPhD3jpEvvBqYRhlnSfiyO6iOkuu3 vSnshiyvlZ3khnErSBGdOUol/Btsh8pOqdtQdhoC/efmx5xBdQxe1PlIs3exwoDz0zapR+83NHh UgH4qMR10Tx2I1c9dwS7LyBOa9Fw6BuEuMm9jvcIFn63A== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Nicolas Graves X-ACL-Warn: , Nicolas Graves via Guix-patches X-Patchwork-Original-From: Nicolas Graves via Guix-patches via From: Nicolas Graves Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches From: Daniel Khodabakhsh Replace (guix build json) with (json) in node-build-system * guix/build/node-build-system.scm (jsobject-ref, jsobject-update, jsobject-union, newest, unkeyed->keyed): Remove unused functions. (with-atomic-json-file-replacement): Switch to (json) module. Move file argument as a second optional argument. (alist-update): Switch to (json) module. Remove optional default argument. (patch-dependencies, delete-dependencies, build): Switch to (json) module. Arguments are unchanged. * guix/build-system/node.scm (%node-build-system-modules): Switch to (json) module. * gnu/packages/node-xyz.scm (node-acorn, node-addon-api, node-serialport-bindings, node-sqlite3): Adapt package custom <#:phases> to the replacement. Signed-off-by: Daniel Khodabakhsh Change-Id: I9fd5152a98b6a241d414e9a94ab179c9cabcfb85 --- gnu/packages/node-xyz.scm | 148 ++++++++++++------------- guix/build-system/node.scm | 6 +- guix/build/node-build-system.scm | 180 ++++++++++++------------------- 3 files changed, 139 insertions(+), 195 deletions(-) diff --git a/gnu/packages/node-xyz.scm b/gnu/packages/node-xyz.scm index bbdcbb2b78..84adde9e50 100644 --- a/gnu/packages/node-xyz.scm +++ b/gnu/packages/node-xyz.scm @@ -7,6 +7,7 @@ ;;; Copyright © 2021 Dhruvin Gandhi ;;; Copyright © 2022 Nicolas Graves ;;; Copyright © 2023 Jelle Licht +;;; Copyright © 2024 Daniel Khodabakhsh ;;; ;;; This file is part of GNU Guix. ;;; @@ -24,14 +25,14 @@ ;;; along with GNU Guix. If not, see . (define-module (gnu packages node-xyz) - #:use-module ((guix licenses) #:prefix license:) #:use-module (gnu packages sqlite) #:use-module (gnu packages python) #:use-module (gnu packages web) + #:use-module (guix build-system node) #:use-module (guix gexp) - #:use-module (guix packages) #:use-module (guix git-download) - #:use-module (guix build-system node)) + #:use-module ((guix licenses) #:prefix license:) + #:use-module (guix packages)) ;;; ;;; Please: Try to add new module packages in alphabetic order. @@ -69,19 +70,17 @@ (define-public node-acorn ;; it would try to use the build environment and would block the ;; automatic building by other packages making use of node-acorn. ;; TODO: Add utility function - (with-atomic-json-file-replacement "package.json" - (match-lambda - (('@ . pkg-meta-alist) - (cons '@ (map (match-lambda - (("scripts" @ . scripts-alist) - `("scripts" @ ,@(filter (match-lambda - (("prepare" . _) - #f) - (_ - #t)) - scripts-alist))) - (other other)) - pkg-meta-alist))))))) + (with-atomic-json-file-replacement (lambda (pkg-meta-alist) + (map + (match-lambda + (("scripts" . scripts-alist) + (cons "scripts" (filter + (match-lambda + (("prepare" . _) #f) + (_ #t)) + scripts-alist))) + (other other)) + pkg-meta-alist))))) (replace 'build (lambda* (#:key inputs native-inputs #:allow-other-keys) (let ((esbuild (search-input-file (or native-inputs inputs) @@ -158,23 +157,17 @@ (define-public node-addon-api (lambda args (define new-test-script "echo stopping after pretest on Guix") - (with-atomic-json-file-replacement "package.json" - (match-lambda - (('@ . pkg-meta-alist) - (cons - '@ - (map (match-lambda - (("scripts" '@ . scripts-alist) - `("scripts" @ ,@(map (match-lambda - (("test" . _) - (cons "test" - new-test-script)) - (other - other)) - scripts-alist))) - (other - other)) - pkg-meta-alist)))))))))) + (with-atomic-json-file-replacement (lambda (pkg-meta-alist) + (map + (match-lambda + (("scripts" . scripts-alist) + (cons "scripts" (map + (match-lambda + (("test" . _) (cons "test" new-test-script)) + (other other)) + scripts-alist))) + (other other)) + pkg-meta-alist)))))))) (home-page "https://github.com/nodejs/node-addon-api") (synopsis "Node.js API (Node-API) header-only C++ wrappers") (description "This module contains header-only C++ wrapper classes which @@ -1223,22 +1216,19 @@ (define-public node-serialport-bindings "node-abi")))) (add-after 'chdir 'avoid-prebuild-install (lambda args - (with-atomic-json-file-replacement "package.json" - (match-lambda - (('@ . pkg-meta-alist) - (cons '@ (map (match-lambda - (("scripts" @ . scripts-alist) - `("scripts" @ ,@(filter (match-lambda - (("install" . _) - #f) - (_ - #t)) - scripts-alist))) - (("gypfile" . _) - '("gypfile" . #f)) - (other - other)) - pkg-meta-alist)))))))) + (with-atomic-json-file-replacement (lambda (pkg-meta-alist) + (map + (match-lambda + (("scripts" . scripts-alist) + (cons "scripts" (filter + (match-lambda + (("install" . _) #f) + (_ #t)) + scripts-alist))) + (("gypfile" . _) + (cons "gypfile" #f)) + (other other)) + pkg-meta-alist)))))) #:tests? #f)) (synopsis "Abstract base class for Node SerialPort bindings") (description "Node SerialPort is a modular suite of Node.js packages for @@ -1524,39 +1514,33 @@ (define-public node-sqlite3 (substitute* ".npmignore" (("lib/binding") "#lib/binding # <- patched for Guix")) - (with-atomic-json-file-replacement "package.json" - (match-lambda - (('@ . pkg-meta-alist) - (match (assoc-ref pkg-meta-alist "binary") - (('@ . binary-alist) - ;; When it builds from source, node-pre-gyp supplies - ;; module_name and module_path based on the entries under - ;; "binary" from "package.json", so this package's - ;; "binding.gyp" doesn't define them. Thus, we also need - ;; to supply them. The GYP_DEFINES environment variable - ;; turns out to be the easiest way to make sure they are - ;; propagated from npm to node-gyp to gyp. - (setenv "GYP_DEFINES" - (string-append - "module_name=" - (assoc-ref binary-alist "module_name") - " " - "module_path=" - (assoc-ref binary-alist "module_path"))))) - ;; We need to remove the install script from "package.json", - ;; as it would try to use node-pre-gyp and would block the - ;; automatic building performed by `npm install`. - (cons '@ (map (match-lambda - (("scripts" @ . scripts-alist) - `("scripts" @ ,@(filter (match-lambda - (("install" . _) - #f) - (_ - #t)) - scripts-alist))) - (other - other)) - pkg-meta-alist)))))))))) + (with-atomic-json-file-replacement (lambda (pkg-meta-alist) + (let ((binary-alist (assoc-ref pkg-meta-alist "binary"))) + ;; When it builds from source, node-pre-gyp supplies + ;; module_name and module_path based on the entries under + ;; "binary" from "package.json", so this package's + ;; "binding.gyp" doesn't define them. Thus, we also need + ;; to supply them. The GYP_DEFINES environment variable + ;; turns out to be the easiest way to make sure they are + ;; propagated from npm to node-gyp to gyp. + (setenv "GYP_DEFINES" (string-append + "module_name=" + (assoc-ref binary-alist "module_name") + " module_path=" + (assoc-ref binary-alist "module_path")))) + ;; We need to remove the install script from "package.json", + ;; as it would try to use node-pre-gyp and would block the + ;; automatic building performed by `npm install`. + (map + (match-lambda + (("scripts" . scripts-alist) + (cons "scripts" (filter + (match-lambda + (("install" . _) #f) + (_ #t)) + scripts-alist))) + (other other)) + pkg-meta-alist)))))))) (home-page "https://github.com/mapbox/node-sqlite3") (synopsis "Node.js bindings for SQLite3") (description diff --git a/guix/build-system/node.scm b/guix/build-system/node.scm index 57fe5f6030..425b8cd9b3 100644 --- a/guix/build-system/node.scm +++ b/guix/build-system/node.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2021 Ludovic Courtès ;;; Copyright © 2021 Pierre Langlois ;;; Copyright © 2021 Philip McGrath +;;; Copyright © 2024 Daniel Khodabakhsh ;;; ;;; This file is part of GNU Guix. ;;; @@ -36,7 +37,10 @@ (define-module (guix build-system node) (define %node-build-system-modules ;; Build-side modules imported by default. `((guix build node-build-system) - (guix build json) + (json) + (json builder) + (json parser) + (json record) ,@%default-gnu-imported-modules)) (define (default-node) diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm index fb23894bc1..df7ea7774c 100644 --- a/guix/build/node-build-system.scm +++ b/guix/build/node-build-system.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2019, 2021 Timothy Sample ;;; Copyright © 2021, 2022 Philip McGrath ;;; Copyright © 2022 Liliana Marie Prikler +;;; Copyright © 2024 Daniel Khodabakhsh ;;; ;;; This file is part of GNU Guix. ;;; @@ -23,10 +24,10 @@ (define-module (guix build node-build-system) #:use-module ((guix build gnu-build-system) #:prefix gnu:) #:use-module (guix build utils) - #:use-module (guix build json) #:use-module (ice-9 ftw) #:use-module (ice-9 regex) #:use-module (ice-9 match) + #:use-module (json) #:use-module (srfi srfi-1) #:use-module (srfi srfi-71) #:export (%standard-phases @@ -34,13 +35,14 @@ (define-module (guix build node-build-system) delete-dependencies node-build)) -(define (with-atomic-json-file-replacement file proc) +(define* (with-atomic-json-file-replacement proc + #:optional (file "package.json")) "Like 'with-atomic-file-replacement', but PROC is called with a single argument---the result of parsing FILE's contents as json---and should a value to be written as json to the replacement FILE." (with-atomic-file-replacement file (lambda (in out) - (write-json (proc (read-json in)) out)))) + (scm->json (proc (json->scm in)) out)))) (define* (assoc-ref* alist key #:optional default) "Like assoc-ref, but return DEFAULT instead of #f if no value exists." @@ -48,10 +50,6 @@ (define* (assoc-ref* alist key #:optional default) (#f default) ((_ . value) value))) -(define* (jsobject-ref obj key #:optional default) - (match obj - (('@ . alist) (assoc-ref* alist key default)))) - (define* (alist-pop alist key #:optional (= equal?)) "Return two values, the first pair in ALIST with key KEY, and the other elements. Equality calls are made as (= KEY ALISTCAR)." @@ -63,67 +61,17 @@ (define (found? pair) (values (car after) (append before (cdr after))) (values #f before)))) -(define* (alist-update alist key proc #:optional default (= equal?)) +(define* (alist-update alist key proc #:optional (= equal?)) "Return an association list like ALIST, but with KEY mapped to the result of PROC applied to the first value found under the comparison (= KEY ALISTCAR). -If no such value exists, use DEFAULT instead. +If no such value exists, return the list unchanged. Unlike acons, this removes the previous association of KEY (assuming it is unique), but the result may still share storage with ALIST." (let ((pair rest (alist-pop alist key =))) - (acons key - (proc (if (pair? pair) - (cdr pair) - default)) - rest))) + (if (pair? pair) + (acons key (proc (cdr pair)) rest) + alist))) -(define (jsobject-update* js . updates) - "Return a json object like JS, but with all UPDATES applied. Each update is -a list (KEY PROC [DEFAULT]), so that KEY is mapped to the result of PROC -applied to the value to which KEY is mapped in JS. If no such mapping exists, -PROC is instead applied to DEFAULT, or to '#f' is no DEFAULT is specified. -The update takes place from left to right, so later UPDATERs will receive the -values returned by earlier UPDATERs for the same KEY." - (match js - (('@ . alist) - (let loop ((alist alist) - (updates updates)) - (match updates - (() (cons '@ alist)) - (((key proc) . updates) - (loop (alist-update alist key proc #f equal?) updates)) - (((key proc default) . updates) - (loop (alist-update alist key proc default equal?) updates))))))) - -(define (jsobject-union combine seed . objects) - "Merge OBJECTS into SEED by applying (COMBINE KEY VAL0 VAL), where VAL0 -is the value found in the (possibly updated) SEED and VAL is the new value -found in one of the OBJECTS." - (match seed - (('@ . aseed) - (match objects - (() seed) - ((('@ . alists) ...) - (cons - '@ - (fold (lambda (alist aseed) - (if (null? aseed) alist - (fold - (match-lambda* - (((k . v) aseed) - (let ((pair tail (alist-pop alist k))) - (match pair - (#f (acons k v aseed)) - ((_ . v0) (acons k (combine k v0 v) aseed)))))) - aseed - alist))) - aseed - alists))))))) - -;; Possibly useful helper functions: -;; (define (newest key val0 val) val) -;; (define (unkeyed->keyed proc) (lambda (_key val0 val) (proc val0 val))) - - ;;; ;;; Phases. ;;; @@ -142,8 +90,8 @@ (define (set-home . _) (define (module-name module) (let* ((package.json (string-append module "/package.json")) - (package-meta (call-with-input-file package.json read-json))) - (jsobject-ref package-meta "name"))) + (package-meta (call-with-input-file package.json json->scm))) + (assoc-ref package-meta "name"))) (define (index-modules input-paths) (define (list-modules directory) @@ -167,49 +115,59 @@ (define* (patch-dependencies #:key inputs #:allow-other-keys) (define index (index-modules (map cdr inputs))) - (define resolve-dependencies - (match-lambda - (('@ . alist) - (cons '@ (map (match-lambda - ((key . value) - (cons key (hash-ref index key value)))) - alist))))) + (define (resolve-dependencies dependencies) + (map + (match-lambda + ((dependency . version) + (cons dependency (hash-ref index dependency version)))) + dependencies)) - (with-atomic-json-file-replacement "package.json" + (with-atomic-json-file-replacement (lambda (pkg-meta) - (jsobject-update* - pkg-meta - `("devDependencies" ,resolve-dependencies (@)) - `("dependencies" ,(lambda (deps) - (resolve-dependencies - (jsobject-union - (lambda (k a b) b) - (jsobject-ref pkg-meta "peerDependencies" '(@)) - deps))) - (@))))) + (fold + (lambda (proc pkg-meta) (proc pkg-meta)) + pkg-meta + (list + (lambda (pkg-meta) + (alist-update pkg-meta "devDependencies" resolve-dependencies)) + (lambda (pkg-meta) + (assoc-set! + pkg-meta + "dependencies" + (resolve-dependencies + ; Combined "peerDependencies" and "dependencies" dependencies + ; with "dependencies" taking precedent. + (fold + (lambda (dependency dependencies) + (assoc-set! dependencies (car dependency) (cdr dependency))) + (assoc-ref* pkg-meta "peerDependencies" '()) + (assoc-ref* pkg-meta "dependencies" '()))))))))) #t) -(define (delete-dependencies absent) +(define (delete-dependencies dependencies-to-remove) "Rewrite 'package.json' to allow the build to proceed without packages -listed in ABSENT, a list of strings naming npm packages. +listed in 'dependencies-to-remove', a list of strings naming npm packages. To prevent the deleted dependencies from being reintroduced, use this function only after the 'patch-dependencies' phase." - (define delete-from-jsobject - (match-lambda - (('@ . alist) - (cons '@ (filter (match-lambda - ((k . _) - (not (member k absent)))) - alist))))) - - (with-atomic-json-file-replacement "package.json" + (with-atomic-json-file-replacement (lambda (pkg-meta) - (jsobject-update* - pkg-meta - `("devDependencies" ,delete-from-jsobject (@)) - `("dependencies" ,delete-from-jsobject (@)) - `("peerDependencies" ,delete-from-jsobject (@)))))) + (fold + (lambda (dependency-key pkg-meta) + (alist-update + pkg-meta + dependency-key + (lambda (dependencies) + (remove + (lambda (dependency) + (member (car dependency) dependencies-to-remove)) + dependencies)))) + pkg-meta + (list + "devDependencies" + "dependencies" + "peerDependencies" + "optionalDependencies"))))) (define* (delete-lockfiles #:key inputs #:allow-other-keys) "Delete 'package-lock.json', 'yarn.lock', and 'npm-shrinkwrap.json', if they @@ -228,8 +186,8 @@ (define* (configure #:key outputs inputs #:allow-other-keys) #t)) (define* (build #:key inputs #:allow-other-keys) - (let ((package-meta (call-with-input-file "package.json" read-json))) - (if (jsobject-ref (jsobject-ref package-meta "scripts" '(@)) "build" #f) + (let ((package-meta (call-with-input-file "package.json" json->scm))) + (if (assoc-ref* (assoc-ref* package-meta "scripts" '()) "build" #f) (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm"))) (invoke npm "run" "build")) (format #t "there is no build script to run~%")) @@ -301,22 +259,20 @@ (define installed-package.json ;; even need to overwrite this file. Therefore, let's use some helpers ;; that we'd otherwise not need. (define pkg-meta - (call-with-input-file installed-package.json read-json)) + (call-with-input-file installed-package.json json->scm)) (define scripts - (jsobject-ref pkg-meta "scripts" '(@))) - (define (jsobject-set js key val) - (jsobject-update* js (list key (const val)))) + (assoc-ref* pkg-meta "scripts" '())) - (when (equal? "node-gyp rebuild" (jsobject-ref scripts "install" #f)) + (when (equal? "node-gyp rebuild" (assoc-ref* scripts "install" #f)) (call-with-output-file installed-package.json (lambda (out) - (write-json - (jsobject-set pkg-meta - "scripts" - (jsobject-set scripts - "install" - "echo Guix: avoiding node-gyp rebuild")) - out))))) + (scm->json + (assoc-set! pkg-meta + "scripts" + (assoc-set! scripts + "install" + "echo Guix: avoiding node-gyp rebuild")) + out))))) (define %standard-phases (modify-phases gnu:%standard-phases