-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
205 lines (148 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
const shasum = require( 'shasum' )
const spawn = require( 'child_process' ).spawn
const path = require( 'path' )
const fs = require( 'fs' )
const PACKAGE_NAME_EXTRACT_EXP = RegExp( '(@(.*)\/)?(.*)' )
const NPM_BIN_PATH = `/usr/local/bin/npm`
module.exports = {
createPackageName: createPackageName,
createImageName: createImageName,
init: init,
dockerBuild: dockerBuild,
dockerPush: dockerPush,
dockerRun: dockerRun
}
/**
* Create package name based on scope/name or overriden-scope/name
* @param {Object} packageConfig
* @return {string}
*/
function createPackageName( packageConfig ) {
// Parse package name
const packageConfigNameList = PACKAGE_NAME_EXTRACT_EXP.exec( packageConfig.name )
const packageConfigName = packageConfigNameList[ 3 ]
let packageConfigScope = packageConfigNameList[ 2 ]
if (
packageConfig.config &&
packageConfig.config.dockerScope
) {
packageConfigScope = packageConfig.config.dockerScope
}
if (
packageConfigScope === `` ||
packageConfigScope === undefined
) {
return packageConfigName
}
return [
packageConfigScope,
packageConfigName
].join( '/' )
}
/**
* Create Docker image name from package.json
* - create shasum from whole package.json string
* - get proper package name
* @param {Object} packageConfig
* @return {string}
*/
function createImageName( packageConfig ) {
const packageConfigHash = shasum( JSON.stringify( packageConfig ) )
const packageConfigName = createPackageName( packageConfig )
return `${packageConfigName}:${packageConfigHash}`
}
/**
* Copy Dockerfile and .dockerignore to project structure
*/
function init() {
copyFile( 'Dockerfile' )
copyFile( '.dockerignore' )
}
/**
* Copy dockerfile to project structure
*/
function copyFile( fileName ) {
const targetFilePath = path.resolve( process.env.PWD, fileName )
const writeStream = fs.createWriteStream( targetFilePath )
writeStream.on( 'error', _reportError )
const sourceFilePath = path.resolve( __dirname, fileName )
const readStream = fs.createReadStream( sourceFilePath ).pipe( writeStream )
readStream.on( 'error', _reportError )
function _reportError( err ) {
throw Error( err )
}
}
/**
* Spawn subprocess with specified command
* @param {string} cmd command name to execute
* @param {string[]} args arguments for executed command
*/
function execCommand( cmd, args ) {
console.log( `EXEC: ${cmd} ${args.join( ' ' )}` )
return spawn( cmd, args, {
env: process.env,
cwd: process.env.PWD,
stdio: [ 'inherit', process.stdout, process.stdout ]
} )
}
/**
* Execute docker build command
* @param {Object} packageConfig
*/
function dockerBuild( packageConfig ) {
const proc = `docker`
const args = [
`build`,
`-t`,
`${createImageName( packageConfig )}`,
`.`
]
return execCommand( proc, args )
}
/**
* Execute docker push command
* @param {Object} packageConfig
*/
function dockerPush( packageConfig ) {
const proc = `docker`
const args = [
`push`,
`${createImageName( packageConfig )}`
]
return execCommand( proc, args )
}
/**
* Run docker run with specified command
* @param {Object} packageConfig
* @param {string[]} command
*/
function dockerRun( packageConfig, cmdArgs ) {
const proc = `docker`
let args = [
`run`,
`--volume=${ process.cwd() }:/app/dev`,
`--interactive`,
`--tty`
]
// Forward port if defined in package.json or in commandline
if (
( packageConfig.config && packageConfig.config.port ) ||
cmdArgs.port
) {
const port = ( cmdArgs.port || packageConfig.config.port )
args = args.concat(
'-p',
`${port}:${port}`
)
}
args = args
.concat( `${createImageName( packageConfig )}` )
if (
cmdArgs.command !== undefined &&
cmdArgs.command.length
) {
args = args.concat( cmdArgs.command )
}
return execCommand( proc, args )
}