Skip to content

Commit 4bc470d

Browse files
committed
Add --arch flag to build and publish commands for single-arch builds
1 parent f63933f commit 4bc470d

File tree

6 files changed

+76
-14
lines changed

6 files changed

+76
-14
lines changed

src/commands/build/handler.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Listr from "listr";
2+
import { Architecture, architectures } from "@dappnode/types";
23
import { buildAndUpload } from "../../tasks/buildAndUpload/index.js";
34
import { ListrContextBuild } from "../../types.js";
45
import {
@@ -23,6 +24,7 @@ export async function buildHandler({
2324
variants_dir_name: variantsDirName = defaultVariantsDirName,
2425
variants,
2526
skip_compose_validation: skipComposeValidation,
27+
arch,
2628
// Global options
2729
dir = defaultDir,
2830
compose_file_name: composeFileName = defaultComposeFileName,
@@ -33,6 +35,27 @@ export async function buildHandler({
3335

3436
const variantsDirPath = path.join(dir, variantsDirName);
3537

38+
const architecture = arch ? normalizeArchitecture(arch) : undefined;
39+
40+
const packagesToBuildProps = getPackagesToBuildProps({
41+
allVariants: Boolean(allVariants),
42+
commaSeparatedVariants: variants,
43+
rootDir: dir,
44+
variantsDirPath,
45+
composeFileName
46+
});
47+
48+
// Filter architectures if --arch flag is provided
49+
if (architecture) {
50+
for (const pkg of packagesToBuildProps) {
51+
if (!pkg.architectures.includes(architecture))
52+
throw Error(
53+
`Architecture '${architecture}' is not supported by package ${pkg.manifest.name}. Supported: ${pkg.architectures.join(", ")}`
54+
);
55+
pkg.architectures = [architecture];
56+
}
57+
}
58+
3659
const buildOptions: BuildAndUploadOptions = {
3760
dir,
3861
contentProvider,
@@ -45,13 +68,7 @@ export async function buildHandler({
4568
deleteOldPins,
4669
variantsDirPath,
4770
skipComposeValidation,
48-
packagesToBuildProps: getPackagesToBuildProps({
49-
allVariants: Boolean(allVariants),
50-
commaSeparatedVariants: variants,
51-
rootDir: dir,
52-
variantsDirPath,
53-
composeFileName
54-
})
71+
packagesToBuildProps
5572
};
5673

5774
const verbosityOptions: VerbosityOptions = {
@@ -62,3 +79,21 @@ export async function buildHandler({
6279

6380
return await buildTasks.run();
6481
}
82+
83+
const archShorthands: Record<string, Architecture> = {
84+
amd64: "linux/amd64",
85+
x86_64: "linux/amd64",
86+
x64: "linux/amd64",
87+
arm64: "linux/arm64",
88+
aarch64: "linux/arm64",
89+
arm: "linux/arm64"
90+
};
91+
92+
export function normalizeArchitecture(arch: string): Architecture {
93+
const normalized = archShorthands[arch] ?? arch;
94+
if (!architectures.includes(normalized as Architecture))
95+
throw Error(
96+
`Invalid architecture '${arch}', allowed values: ${architectures.join(", ")} (or shorthands: ${Object.keys(archShorthands).join(", ")})`
97+
);
98+
return normalized as Architecture;
99+
}

src/commands/build/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export const build: CommandModule<CliGlobalOptions, BuildCommandOptions> = {
5858
alias: "skip-compose-validation",
5959
description: `Skip the Dappnode compose validation step`,
6060
type: "boolean"
61+
},
62+
arch: {
63+
description: `Specify the architecture to build for: "linux/amd64" or "linux/arm64"`,
64+
type: "string"
6165
}
6266
},
6367

src/commands/build/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface BuildCommandOptions extends CliGlobalOptions {
1313
variants?: string;
1414
all_variants?: boolean;
1515
skip_compose_validation?: boolean;
16+
arch?: string;
1617
}
1718

1819
export interface VerbosityOptions {

src/commands/publish/handler.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { PublishCommandOptions } from "./types.js";
1111
import { publish } from "../../tasks/publish/index.js";
1212
import { parseReleaseType } from "./parseReleaseType.js";
1313
import { getPackagesToBuildProps } from "../build/variants.js";
14+
import { normalizeArchitecture } from "../build/handler.js";
1415

1516
/**
1617
* Common handler for CLI and programatic usage
@@ -28,6 +29,7 @@ export async function publishHandler({
2829
require_git_data: requireGitData,
2930
delete_old_pins: deleteOldPins,
3031
skip_compose_validation: skipComposeValidation,
32+
arch,
3133
// Global options
3234
dir = defaultDir,
3335
compose_file_name: composeFileName = defaultComposeFileName,
@@ -64,8 +66,29 @@ export async function publishHandler({
6466

6567
const variantsDirPath = path.join(dir, variantsDirName);
6668

69+
const architecture = arch ? normalizeArchitecture(arch) : undefined;
70+
6771
const isMultiVariant = !!allVariants || !!variants;
6872

73+
const packagesToBuildProps = getPackagesToBuildProps({
74+
allVariants: Boolean(allVariants),
75+
commaSeparatedVariants: variants,
76+
rootDir: dir,
77+
variantsDirPath,
78+
composeFileName
79+
});
80+
81+
// Filter architectures if --arch flag is provided
82+
if (architecture) {
83+
for (const pkg of packagesToBuildProps) {
84+
if (!pkg.architectures.includes(architecture))
85+
throw Error(
86+
`Architecture '${architecture}' is not supported by package ${pkg.manifest.name}. Supported: ${pkg.architectures.join(", ")}`
87+
);
88+
pkg.architectures = [architecture];
89+
}
90+
}
91+
6992
const publishTasks = new Listr(
7093
publish({
7194
releaseType,
@@ -82,13 +105,7 @@ export async function publishHandler({
82105
verbosityOptions,
83106
variantsDirPath,
84107
skipComposeValidation,
85-
packagesToBuildProps: getPackagesToBuildProps({
86-
allVariants: Boolean(allVariants),
87-
commaSeparatedVariants: variants,
88-
rootDir: dir,
89-
variantsDirPath,
90-
composeFileName
91-
}),
108+
packagesToBuildProps,
92109
isMultiVariant
93110
}),
94111
verbosityOptions

src/commands/publish/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ export const publish: CommandModule<CliGlobalOptions, PublishCommandOptions> = {
8888
alias: "skip-compose-validation",
8989
description: `Skip the Dappnode compose validation step`,
9090
type: "boolean"
91+
},
92+
arch: {
93+
description: `Specify the architecture to build for: "linux/amd64" or "linux/arm64"`,
94+
type: "string"
9195
}
9296
},
9397

src/commands/publish/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ export interface PublishCommandOptions extends CliGlobalOptions {
1717
variants?: string;
1818
all_variants?: boolean;
1919
skip_compose_validation?: boolean;
20+
arch?: string;
2021
}

0 commit comments

Comments
 (0)