Skip to content

Commit 699beb6

Browse files
LinuxSuRenzhaoxiaojie
andauthored
fix: pnpm install command failure (#9)
Co-authored-by: zhaoxiaojie <zhaoxiaojie@ailinking.com>
1 parent 54cf073 commit 699beb6

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

runtime/node.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"log/slog"
910
"os"
@@ -139,34 +140,28 @@ func (d *Node) GenerateDockerfile(path string) ([]byte, error) {
139140
See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, packageManager, installCMD, buildCMD, startCMD),
140141
)
141142

142-
if installCMD != "" {
143-
installCMDJSON, _ := json.Marshal(installCMD)
144-
installCMD = string(installCMDJSON)
145-
}
146-
147-
if buildCMD != "" {
148-
buildCMDJSON, _ := json.Marshal(buildCMD)
149-
buildCMD = string(buildCMDJSON)
150-
}
151-
152-
if startCMD != "" {
153-
startCMDJSON, _ := json.Marshal(startCMD)
154-
startCMD = string(startCMDJSON)
155-
}
156-
157143
var buf bytes.Buffer
158144
if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{
159145
"Version": *version,
160-
"InstallCMD": installCMD,
161-
"BuildCMD": buildCMD,
162-
"StartCMD": startCMD,
146+
"InstallCMD": safeCommand(installCMD),
147+
"BuildCMD": safeCommand(buildCMD),
148+
"StartCMD": safeCommand(startCMD),
163149
}); err != nil {
164-
return nil, fmt.Errorf("Failed to execute template")
150+
return nil, errors.New("Failed to execute template")
165151
}
166152

167153
return buf.Bytes(), nil
168154
}
169155

156+
func safeCommand(cmd string) string {
157+
if cmd == "" {
158+
return ""
159+
}
160+
161+
cmdJSON, _ := json.Marshal(cmd)
162+
return strings.ReplaceAll(string(cmdJSON), `\u0026\u0026`, "&&")
163+
}
164+
170165
var startScriptRe = regexp.MustCompile(`^.*?\b(ts-)?node(mon)?\b.*?(index|main|server|client)\.([cm]?[tj]s)\b`)
171166

172167
var nodeTemplate = strings.TrimSpace(`
@@ -177,7 +172,7 @@ FROM base AS deps
177172
WORKDIR /app
178173
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* bun.lockb* ./
179174
ARG INSTALL_CMD={{.InstallCMD}}
180-
RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi
175+
RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi
181176
182177
FROM base AS builder
183178
WORKDIR /app

runtime/node_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestNodeGenerateDockerfile(t *testing.T) {
6060
{
6161
name: "Node project with pnpm",
6262
path: "../testdata/node-pnpm",
63-
expected: []any{`ARG VERSION=16.0.0`, `ARG INSTALL_CMD="corepack enable pnpm \u0026\u0026 pnpm i --frozen-lockfile"`, `ARG BUILD_CMD="pnpm run build:prod"`, `ARG START_CMD="pnpm run start:production"`},
63+
expected: []any{`ARG VERSION=16.0.0`, `ARG INSTALL_CMD="corepack enable pnpm && pnpm i --frozen-lockfile"`, `ARG BUILD_CMD="pnpm run build:prod"`, `ARG START_CMD="pnpm run start:production"`},
6464
},
6565
{
6666
name: "Node project with yarn",

0 commit comments

Comments
 (0)