1- import { existsSync } from "fs " ;
1+ import { isDockerfile } from "@cloudflare/containers-shared " ;
22import { type Config } from "../config" ;
33import { type ContainerApp } from "../config/environment" ;
44import { getDockerPath } from "../environment-variables/misc-variables" ;
@@ -7,7 +7,7 @@ import { isNonInteractiveOrCI } from "../is-interactive";
77import { logger } from "../logger" ;
88import { fetchVersion } from "../versions/api" ;
99import { apply } from "./apply" ;
10- import { buildAndMaybePush , isDir } from "./build" ;
10+ import { buildAndMaybePush } from "./build" ;
1111import { fillOpenAPIConfiguration } from "./common" ;
1212import type { BuildArgs } from "@cloudflare/containers-shared/src/types" ;
1313
@@ -18,11 +18,22 @@ export async function maybeBuildContainer(
1818 dryRun : boolean ,
1919 pathToDocker : string
2020) {
21- if (
22- ! isDockerfile ( containerConfig . image ?? containerConfig . configuration . image )
23- ) {
24- return containerConfig . image ?? containerConfig . configuration . image ;
21+ try {
22+ if (
23+ ! isDockerfile (
24+ containerConfig . image ?? containerConfig . configuration . image
25+ )
26+ ) {
27+ return containerConfig . image ?? containerConfig . configuration . image ;
28+ }
29+ } catch ( err ) {
30+ if ( err instanceof Error ) {
31+ throw new UserError ( err . message ) ;
32+ }
33+
34+ throw err ;
2535 }
36+
2637 const options = getBuildArguments ( containerConfig , imageTag ) ;
2738 logger . log ( "Building image" , options . tag ) ;
2839 const tag = await buildAndMaybePush (
@@ -126,43 +137,3 @@ export function getBuildArguments(
126137 args : container . image_vars ,
127138 } ;
128139}
129-
130- export const isDockerfile = ( image : string ) : boolean => {
131- // TODO: move this into config validation
132- if ( existsSync ( image ) ) {
133- if ( isDir ( image ) ) {
134- throw new UserError (
135- `${ image } is a directory, you should specify a path to the Dockerfile`
136- ) ;
137- }
138- return true ;
139- }
140-
141- const errorPrefix = `The image "${ image } " does not appear to be a valid path to a Dockerfile, or a valid image registry path:\n` ;
142- // not found, not a dockerfile, let's try parsing the image ref as an URL?
143- try {
144- new URL ( `https://${ image } ` ) ;
145- } catch ( e ) {
146- if ( e instanceof Error ) {
147- throw new UserError ( errorPrefix + e . message ) ;
148- }
149- throw e ;
150- }
151- const imageParts = image . split ( "/" ) ;
152-
153- if ( ! imageParts [ imageParts . length - 1 ] . includes ( ":" ) ) {
154- throw new UserError (
155- errorPrefix +
156- `If this is an image registry path, it needs to include at least a tag ':' (e.g: docker.io/httpd:1)`
157- ) ;
158- }
159-
160- // validate URL
161- if ( image . includes ( "://" ) ) {
162- throw new UserError (
163- errorPrefix +
164- `Image reference should not include the protocol part (e.g: docker.io/httpd:1, not https://docker.io/httpd:1)`
165- ) ;
166- }
167- return false ;
168- } ;
0 commit comments