Skip to content

Commit af53fc1

Browse files
committed
add check dockerfile path
1 parent b1c2b09 commit af53fc1

5 files changed

Lines changed: 64 additions & 22 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ package.json
7979
}
8080
```
8181

82+
### Test
83+
84+
```bash
85+
ts-node lib/build-docker/node-run.ts
86+
```
87+
8288

8389
## License
8490

lib/build-docker/index.ts

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {CLIError} from '../script/cli';
1010
import logger from '../script/logger';
1111
import {initDefault} from '../config';
1212
import pushDocker from '../push-docker';
13+
import * as fs from 'fs';
14+
import Logger
15+
from '../script/logger';
1316

1417
const {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

2240
function 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

117153
export default run;

lib/build-docker/node-run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import buildDocker from './index';
22

33
// $ ts-node lib/build-docker/node-run.ts
4-
buildDocker();
4+
buildDocker({dockerfile: './config/dockerfile/nest/Dockerfile'});

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = async function bearScript () {
88
return yargs
99
.positional('publicUrl', {
1010
describe: 'react-script build public url (ex: /recommend)',
11-
default: 'undefined',
11+
default: '',
1212
})
1313
.positional('dockerfile', {
1414
describe: 'custom dockerfile path (ex: ./)',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bear-node-docker",
3-
"version": "2.2.5",
3+
"version": "2.2.6-alpha.0",
44
"description": "Common tools build docker image for node project development",
55
"keywords": [
66
"reactjs",

0 commit comments

Comments
 (0)