@@ -18,7 +18,7 @@ function dockerCommand(options) {
18
18
throw new Error ( ps . stderr ) ;
19
19
}
20
20
return ps ;
21
- }
21
+ } ;
22
22
23
23
/**
24
24
* Build the custom Docker image
@@ -34,34 +34,67 @@ function buildImage(dockerFile) {
34
34
return imageName ;
35
35
} ;
36
36
37
+ /**
38
+ * Test bind path to make sure it's working
39
+ * @param {string } bindPath
40
+ * @return {boolean }
41
+ */
42
+ function tryBindPath ( bindPath ) {
43
+ const options = [ 'run' , '--rm' , '-v' , `${ bindPath } :/test` ,
44
+ 'alpine' , 'ls' , '/test/serverless.yml' ] ;
45
+ try {
46
+ const ps = dockerCommand ( options ) ;
47
+ return ps . stdout . trim ( ) === '/test/serverless.yml' ;
48
+ } catch ( err ) {
49
+ return false ;
50
+ }
51
+ } ;
52
+
37
53
/**
38
54
* Get bind path depending on os platform
39
55
* @param {string } servicePath
40
56
* @return {string } The bind path.
41
57
*/
42
58
function getBindPath ( servicePath ) {
43
- // Determine os platform of docker CLI from 'docker version'
44
- const options = [ 'version' , '--format' , '{{with .Client}}{{.Os}}{{end}}' ] ;
45
- const ps = dockerCommand ( options ) ;
46
- const cliPlatform = ps . stdout . trim ( ) ;
47
-
48
59
// Determine bind path
49
- let bindPath ;
50
- if ( process . platform === 'win32' ) {
51
- bindPath = servicePath . replace ( / \\ ( [ ^ \s ] ) / g, '/$1' ) ;
52
- if ( cliPlatform === 'windows' ) {
53
- bindPath = bindPath . replace ( / ^ \/ ( \w ) \/ / i, '$1:/' ) ;
54
- }
55
- } else if ( isWsl ) {
56
- bindPath = servicePath . replace ( / ^ \/ m n t \/ / , '/' ) ;
57
- if ( cliPlatform === 'windows' ) {
58
- bindPath = bindPath . replace ( / ^ \/ ( \w ) \/ / i, '$1:/' ) ;
59
- }
60
+ if ( process . platform !== 'win32' && ! isWsl ) {
61
+ return servicePath ;
62
+ }
63
+
64
+ let bindPaths = [ ] ;
65
+ let baseBindPath = servicePath . replace ( / \\ ( [ ^ \s ] ) / g, '/$1' ) ;
66
+ let drive ;
67
+ let path ;
68
+
69
+ bindPaths . push ( baseBindPath ) ;
70
+ if ( baseBindPath . startsWith ( '/mnt/' ) ) { // cygwin "/mnt/C/users/..."
71
+ baseBindPath = baseBindPath . replace ( / ^ \/ m n t \/ / , '/' ) ;
72
+ }
73
+ if ( baseBindPath [ 1 ] == ':' ) { // normal windows "c:/users/..."
74
+ drive = baseBindPath [ 0 ] ;
75
+ path = baseBindPath . substring ( 3 ) ;
76
+ } else if ( baseBindPath [ 0 ] == '/' && baseBindPath [ 2 ] == '/' ) { // gitbash "/c/users/..."
77
+ drive = baseBindPath [ 1 ] ;
78
+ path = baseBindPath . substring ( 3 ) ;
60
79
} else {
61
- bindPath = servicePath ;
80
+ throw new Error ( `Unknown path format ${ baseBindPath . substr ( 10 ) } ...` ) ;
81
+ }
82
+
83
+ bindPaths . push ( `/${ drive . toLowerCase ( ) } /${ path } ` ) ;
84
+ bindPaths . push ( `/${ drive . toUpperCase ( ) } /${ path } ` ) ;
85
+ bindPaths . push ( `/mnt/${ drive . toLowerCase ( ) } /${ path } ` ) ;
86
+ bindPaths . push ( `/mnt/${ drive . toUpperCase ( ) } /${ path } ` ) ;
87
+ bindPaths . push ( `${ drive . toLowerCase ( ) } :/${ path } ` ) ;
88
+ bindPaths . push ( `${ drive . toUpperCase ( ) } :/${ path } ` ) ;
89
+
90
+ for ( let i = 0 ; i < bindPaths . length ; i ++ ) {
91
+ const bindPath = bindPaths [ i ] ;
92
+ if ( tryBindPath ( bindPath ) ) {
93
+ return bindPath ;
94
+ }
62
95
}
63
96
64
- return bindPath ;
97
+ throw new Error ( 'Unable to find good bind path format' ) ;
65
98
} ;
66
99
67
100
module . exports = { buildImage, getBindPath} ;
0 commit comments