Skip to content

Commit 9be6149

Browse files
authored
Merge pull request #58 from EmbeddedEnterprises/cross-update
fix: update cross compilation config based on npm_config_target
2 parents f5ffe96 + 6825642 commit 9be6149

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function detectCrossCompilation(
132132
return true
133133
}
134134
if (
135+
config.os === undefined &&
135136
process.env.npm_config_target_os !== undefined &&
136137
platforms.has(process.env.npm_config_target_os as NodeJS.Platform) &&
137138
process.env.npm_config_target_os !== process.platform
@@ -140,9 +141,11 @@ export function detectCrossCompilation(
140141
logger.debug(
141142
`Cross compilation detected: npm_config_target_os (${process.env.npm_config_target_os}) differs from process.platform (${process.platform})`,
142143
)
144+
config.os = process.env.npm_config_target_os as NodeJS.Platform
143145
return true
144146
}
145147
if (
148+
config.arch === undefined &&
146149
process.env.npm_config_target_arch !== undefined &&
147150
architectures.has(process.env.npm_config_target_arch as NodeJS.Architecture) &&
148151
process.env.npm_config_target_arch !== process.arch
@@ -151,6 +154,7 @@ export function detectCrossCompilation(
151154
logger.debug(
152155
`Cross compilation detected: npm_config_target_arch (${process.env.npm_config_target_arch}) differs from process.arch (${process.arch})`,
153156
)
157+
config.arch = process.env.npm_config_target_arch as NodeJS.Architecture
154158
return true
155159
}
156160
return false

test/config.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ suite("Config Functions", () => {
133133
expect(result.buildType).toBe("Release")
134134
})
135135

136-
test("should handle cross compilation flags", async () => {
136+
test("should detect os cross compilation", async () => {
137137
const partialConfig: Partial<BuildConfiguration> = {
138138
os: process.platform === "win32" ? "linux" : "win32",
139139
arch: "x64",
@@ -142,6 +142,7 @@ suite("Config Functions", () => {
142142
const result = await getBuildConfig(mockBuildOptions, partialConfig, mockConfigFile)
143143

144144
expect(result.cross).toBe(true)
145+
expect(result.os).toBe(process.platform === "win32" ? "linux" : "win32")
145146
})
146147

147148
test("should respect npm_config_target_arch when it matches config.arch", async () => {
@@ -195,7 +196,7 @@ suite("Config Functions", () => {
195196
vi.restoreAllMocks()
196197
})
197198

198-
test("should respect npm_config_target_arch when it differs from process.arch", async () => {
199+
test("should respect config.arch when it differs from process.arch even if npm_config_target_arch is set", async () => {
199200
// Set npm_config_target_arch to a different architecture than the current one
200201
process.env.npm_config_target_arch = process.arch === "x64" ? "arm64" : "x64"
201202

@@ -206,7 +207,29 @@ suite("Config Functions", () => {
206207

207208
const result = await getBuildConfig(mockBuildOptions, partialConfig, mockConfigFile)
208209

210+
expect(result.cross).toBe(false)
211+
expect(result.arch).toBe(process.arch)
212+
})
213+
214+
test("should respect npm_config_target_arch when it differs from process.arch with default config", async () => {
215+
// Set npm_config_target_arch to a different architecture than the current one
216+
process.env.npm_config_target_arch = process.arch === "x64" ? "arm64" : "x64"
217+
218+
const result = await getBuildConfig(mockBuildOptions, {}, mockConfigFile)
219+
220+
expect(result.cross).toBe(true)
221+
expect(result.arch).toBe(process.env.npm_config_target_arch)
222+
expect(result.os).toBe(process.platform)
223+
})
224+
225+
test("should respect npm_config_target_os when it differs from process.platform with default config", async () => {
226+
// Set npm_config_target_os to a different platform than the current one
227+
process.env.npm_config_target_os = process.platform === "win32" ? "linux" : "win32"
228+
229+
const result = await getBuildConfig(mockBuildOptions, {}, mockConfigFile)
230+
209231
expect(result.cross).toBe(true)
232+
expect(result.os).toBe(process.env.npm_config_target_os)
210233
expect(result.arch).toBe(process.arch)
211234
})
212235

0 commit comments

Comments
 (0)