-
Notifications
You must be signed in to change notification settings - Fork 9
/
nodejs.go
154 lines (126 loc) · 4.34 KB
/
nodejs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nodejs
import (
"context"
"fmt"
"io/fs"
"path/filepath"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/languages/opaque"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
const (
startScript = "start"
buildScript = "build"
devScript = "dev"
npmLockfile = "package-lock.json"
yarnLockfile = "yarn.lock"
pnpmLockfile = "pnpm-lock.yaml"
)
type Parser struct{}
func (i *Parser) Url() string { return "namespace.so/from-nodejs" }
func (i *Parser) Shortcut() string { return "nodejs" }
type cueIntegrationNodejs struct {
Build cueIntegrationNodejsBuild `json:"build"`
Pkg string `json:"pkg"`
// Name -> package name.
// The ingress urls for backends are injected into the built image as a JS file.
Backends map[string]string `json:"backends"`
}
type cueIntegrationNodejsBuild struct {
OutDir string `json:"outDir"`
}
func (i *Parser) Parse(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, loc pkggraph.Location, v *fncue.CueV) (proto.Message, error) {
var bits cueIntegrationNodejs
if v != nil {
if err := v.Val.Decode(&bits); err != nil {
return nil, err
}
}
relPath := filepath.Join(loc.Rel(), bits.Pkg)
pkgMgr, err := detectPkgMgr(ctx, pl, loc, relPath)
if err != nil {
return nil, err
}
fmt.Fprintf(console.Debug(ctx), "nodejs: %s: detected package manager: %s\n", loc.Abs(), pkgMgr)
packageJson, err := readPackageJson(ctx, pl, loc, relPath)
if err != nil {
return nil, err
}
scripts := []string{}
for s := range packageJson.Scripts {
scripts = append(scripts, s)
}
backends, err := ParseBackends(loc, bits.Backends)
if err != nil {
return nil, err
}
out := &schema.NodejsBuild{
Pkg: bits.Pkg,
NodePkgMgr: pkgMgr,
InternalDoNotUseBackend: backends,
}
if opaque.UseDevBuild(env) {
// Existence of the "dev" script is not checked, because this code is executed during package loading,
// and for "ns test" it happens initially with the "DEV" environment.
out.RunScript = devScript
} else {
if !slices.Contains(scripts, startScript) {
return nil, fnerrors.UserError(loc, `package.json must contain a script named '%s': it is invoked when starting the server in non-dev environments`, startScript)
}
out.RunScript = startScript
out.Prod = &schema.NodejsBuild_Prod{
BuildOutDir: bits.Build.OutDir,
InstallDeps: true,
}
if slices.Contains(scripts, buildScript) {
out.Prod.BuildScript = buildScript
} else {
if out.Prod.BuildOutDir != "" {
return nil, fnerrors.UserError(loc, `package.json must contain '%s' script if 'build.outDir' is set`, buildScript)
}
}
}
return out, nil
}
func detectPkgMgr(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location, relPath string) (schema.NodejsBuild_NodePkgMgr, error) {
fsys, err := pl.WorkspaceOf(ctx, loc.Module)
if err != nil {
return schema.NodejsBuild_PKG_MGR_UNKNOWN, err
}
if _, err := fs.Stat(fsys, filepath.Join(relPath, npmLockfile)); err == nil {
return schema.NodejsBuild_NPM, nil
}
if _, err := fs.Stat(fsys, filepath.Join(relPath, ".yarn", "releases")); err == nil {
return schema.NodejsBuild_YARN3, nil
}
if _, err := fs.Stat(fsys, filepath.Join(relPath, yarnLockfile)); err == nil {
return schema.NodejsBuild_YARN, nil
}
if _, err := fs.Stat(fsys, filepath.Join(relPath, pnpmLockfile)); err == nil {
return schema.NodejsBuild_PNPM, nil
}
return schema.NodejsBuild_PKG_MGR_UNKNOWN, fnerrors.UserError(loc, "no package manager detected")
}
func ParseBackends(loc pkggraph.Location, src map[string]string) ([]*schema.NodejsBuild_Backend, error) {
backends := []*schema.NodejsBuild_Backend{}
for k, v := range src {
serviceRef, err := schema.ParsePackageRef(loc.PackageName, v)
if err != nil {
return nil, err
}
backends = append(backends, &schema.NodejsBuild_Backend{
Name: k,
Service: serviceRef,
})
}
return backends, nil
}