@@ -10,6 +10,9 @@ import {CLIError} from '../script/cli';
1010import logger from '../script/logger' ;
1111import { initDefault } from '../config' ;
1212import pushDocker from '../push-docker' ;
13+ import * as fs from 'fs' ;
14+ import Logger
15+ from '../script/logger' ;
1316
1417const { Select} = require ( 'enquirer' ) ;
1518
@@ -18,23 +21,46 @@ interface IArgs {
1821 dockerfile ?: string
1922}
2023
24+ interface IOptions {
25+ publicUrl ?: string
26+ }
27+
28+
29+ /**
30+ * Object.keys 型別增強
31+ * @param object
32+ */
33+ export function objectKeys < T extends object > ( object : T ) : Array < keyof T > {
34+ return Object . keys ( object ) as Array < keyof T > ;
35+ }
36+
37+
38+
2139
2240function buildDockerImage ( imageName : string , version : string , remoteAddress : string , dockerfile : string , publicUrl ?: string ) : Promise < string > {
2341
2442 return new Promise ( ( resolve , reject ) => {
25- const options = {
26- publicUrl : isEmpty ( publicUrl ) ? `PUBLIC_URL=${ publicUrl } ` : undefined ,
43+ const options : IOptions = {
44+ publicUrl : isEmpty ( publicUrl ) ? undefined : `PUBLIC_URL=${ publicUrl } ` ,
2745 } ;
28- const dockerBuildArgs = [ 'build' , '-t' , imageName , '-f' , dockerfile , '--build-arg' , options . publicUrl , '.' ]
46+ const optionsLength = objectKeys ( options ) . filter ( ( key ) => typeof options [ key ] !== 'undefined' ) . length ;
47+ const buildArgTag = optionsLength > 0 ? '--build-arg' : undefined ;
48+
49+ const dockerBuildArgs = [ 'build' , '-t' , imageName , '-f' , dockerfile , buildArgTag , options . publicUrl , '.' ]
2950 . filter ( arg => typeof arg !== 'undefined' ) as string [ ] ;
3051
31- const loader = ora ( ) ;
3252 logger . info (
3353 `Building ${ chalk . dim (
3454 `(using "docker ${ dockerBuildArgs . join ( ' ' ) } ")` ,
3555 ) } `
3656 ) ;
3757
58+ // 檢查Docker存在
59+ if ( ! fs . existsSync ( dockerfile ) ) {
60+ throw new Error ( 'dockerfile not exists, please check your dockerfile path' ) ;
61+ }
62+
63+ const loader = ora ( ) ;
3864 const buildProcess = child_process . spawn ( 'docker' , dockerBuildArgs , {
3965 env : {
4066 ...process . env ,
@@ -94,24 +120,34 @@ async function run(args?: IArgs) {
94120
95121 console . log ( `ready release ${ imageName } :${ imageVersion } ...` ) ;
96122
97- // Build Image
98- const targetImageName = await buildDockerImage ( imageName , imageVersion , remoteAddress , dockerfile , publicUrl ) ;
123+ try {
124+ // Build Image
125+ const targetImageName = await buildDockerImage ( imageName , imageVersion , remoteAddress , dockerfile , publicUrl ) ;
99126
100- // By OSX Notice
101- bash ( `osascript -e 'display notification "${ targetImageName } done" with title "build done"'` ) ;
127+ // By OSX Notice
128+ bash ( `osascript -e 'display notification "${ targetImageName } done" with title "build done"'` ) ;
102129
103- const prompt = new Select ( {
104- name : 'confirmPush' ,
105- message : 'do you want to push it?' ,
106- choices : [
107- { name : 'y' , message : 'Yes' } ,
108- { name : 'n' , message : 'No' } ,
109- ] ,
110- } ) ;
111- const confirmPush = await prompt . run ( ) ;
112- if ( confirmPush === 'y' ) {
113- pushDocker ( ) ;
130+ const prompt = new Select ( {
131+ name : 'confirmPush' ,
132+ message : 'do you want to push it?' ,
133+ choices : [
134+ { name : 'y' , message : 'Yes' } ,
135+ { name : 'n' , message : 'No' } ,
136+ ] ,
137+ } ) ;
138+ const confirmPush = await prompt . run ( ) ;
139+ if ( confirmPush === 'y' ) {
140+ pushDocker ( ) ;
141+ }
142+
143+ } catch ( e : any ) {
144+ if ( e instanceof Error ) {
145+ Logger . error ( e . message ) ;
146+ } else {
147+ console . log ( e ) ;
148+ }
114149 }
150+
115151}
116152
117153export default run ;
0 commit comments