An install ask's body runs npm ci from the lock with no install script and no link, packs the tree alone under modules/<module>/node_modules, publishes it to the artifact store as the archive <module>:node-modules under the ask's version and answers its reference, which the step's end carries. The package.json npm ci needs is written from the lock's root entry, a lock without one refused. The integration level's setup bound in code goes: half of the level's bound, which each level's script gets as MESH_TEST_LEVEL_BOUND. Builds: design 47 H2 §800; H-A-399 (2), H-A-403, H-A-427
122 lines
5.4 KiB
Go
122 lines
5.4 KiB
Go
package main
|
|
|
|
// **An install of a module's npm tree** (design 47 H2 §800; H-A-399 (2), H-A-403): the runner asks the seat node-build-node
|
|
// a StepAsk of the kind install, on the seat's install queue, for a module whose Go artifact's check-needs names this
|
|
// seat. The install takes the module's package lock at the commit, installs it from the npm registry seat with an
|
|
// integrity-checked install (npm ci) that runs no install script and makes no link (--ignore-scripts, --no-bin-links: the
|
|
// SDK's Pack and the unpack refuse a symlink, and the mesh creates none), packs the tree, publishes the archive to the
|
|
// artifact store as the module's artifact <module>:node-modules under the ask's version, and answers its reference, which
|
|
// the step's end carries and the integration ask names for the Go build module to unpack.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
artifactref "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient/reference"
|
|
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
|
)
|
|
|
|
// installKind is the artifact kind the install archive is published as (H-A-403: kind archive).
|
|
const installKind = "archive"
|
|
|
|
// installTree is where the installed tree lies in the archive, from the workspace's tree: the module's directory's
|
|
// node_modules, which the Go build module unpacks at the tree's root (layInstall).
|
|
func installTree(module string) string { return filepath.Join("modules", module, "node_modules") }
|
|
|
|
// installTreeStep is AskSteps.Install: the install body of an install ask (steprun's installStep gives its arguments).
|
|
func (m *Module) installTreeStep(ctx context.Context, args map[string]any) (any, error) {
|
|
module, _ := args["module"].(string)
|
|
lock, _ := args["lock"].(string)
|
|
artifact, _ := args["artifact"].(string)
|
|
version, _ := args["version"].(string)
|
|
name, ok := strings.CutPrefix(artifact, module+":")
|
|
switch {
|
|
case module == "" || !ok || artifact != sdkstep.InstallArtifact(module):
|
|
return nil, fmt.Errorf("the install of %q names the artifact %q, not %s", module, artifact, sdkstep.InstallArtifact(module))
|
|
case version == "":
|
|
return nil, fmt.Errorf("the install of %s names no version to publish its archive under", module)
|
|
}
|
|
pkg, err := lockRoot(lock)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the install of %s: %w", module, err)
|
|
}
|
|
if err := os.MkdirAll(m.Work, 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
root, err := os.MkdirTemp(m.Work, "install-")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer os.RemoveAll(root)
|
|
dir := filepath.Join(root, "npm")
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), pkg, 0o600); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "package-lock.json"), []byte(lock), 0o600); err != nil {
|
|
return nil, err
|
|
}
|
|
// The registry is given through a config file of its own, outside the workspace, removed after (npmrcIn).
|
|
npmrc, err := m.npmrcIn(root)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the install of %s: %w", module, err)
|
|
}
|
|
defer os.Remove(npmrc)
|
|
env := append(m.baseEnv(), "npm_config_cache="+m.npmCache())
|
|
r := m.Run(ctx, env, "npm", "ci", "--ignore-scripts", "--no-bin-links", "--no-audit", "--no-fund", "--prefix", dir, "--userconfig", npmrc)
|
|
if why := ranFailed(r); why != "" {
|
|
return nil, steprun.Transport(fmt.Errorf("installing the npm tree of %s: %s", module, why))
|
|
}
|
|
// Only the tree is archived, under the module's directory: never the package.json written here from the lock, which
|
|
// would lie over the module's own when unpacked.
|
|
packed := filepath.Join(root, "packed")
|
|
into := filepath.Join(packed, installTree(module))
|
|
if err := os.MkdirAll(filepath.Dir(into), 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := os.Rename(filepath.Join(dir, "node_modules"), into); err != nil {
|
|
return nil, fmt.Errorf("the install of %s made no node_modules: %w", module, err)
|
|
}
|
|
archive, digest, err := steprun.Pack(packed)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("packing the npm tree of %s: %w", module, err)
|
|
}
|
|
if err := m.publishOn(ctx, steprun.ArtifactSeat, module+"/"+name, installKind, version, digest, archive); err != nil {
|
|
return nil, fmt.Errorf("publishing the npm tree of %s: %w", module, err)
|
|
}
|
|
reference := artifactref.SeatReference(steprun.ArtifactSeat, module, name, digest)
|
|
if m.Say != nil {
|
|
m.Say("installed the npm tree of %s at %s (%d bytes) as %s", module, version, len(archive), reference)
|
|
}
|
|
return map[string]any{"archive": reference}, nil
|
|
}
|
|
|
|
// lockRoot is the package.json npm ci needs beside a lock, written from the lock's root package (packages[""]): its
|
|
// name, version and dependency maps, never a script. A lock with no root package (lockfileVersion 1) is refused.
|
|
func lockRoot(lock string) ([]byte, error) {
|
|
var l struct {
|
|
Packages map[string]map[string]any `json:"packages"`
|
|
}
|
|
if err := json.Unmarshal([]byte(lock), &l); err != nil {
|
|
return nil, fmt.Errorf("the package lock is not JSON: %w", err)
|
|
}
|
|
root, ok := l.Packages[""]
|
|
if !ok {
|
|
return nil, fmt.Errorf("the package lock names no root package (packages[\"\"]): a lock of lockfileVersion 2 or later is needed")
|
|
}
|
|
pkg := map[string]any{}
|
|
for _, k := range []string{"name", "version", "dependencies", "devDependencies", "optionalDependencies", "peerDependencies"} {
|
|
if v, ok := root[k]; ok {
|
|
pkg[k] = v
|
|
}
|
|
}
|
|
return json.Marshal(pkg)
|
|
}
|