Skip to content

Commit ee990d7

Browse files
authored
Merge pull request #144 from kichik/win32_bind_paths
Support more bind path options
2 parents 75b4d70 + 5207a77 commit ee990d7

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

lib/docker.js

+52-19
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function dockerCommand(options) {
1818
throw new Error(ps.stderr);
1919
}
2020
return ps;
21-
}
21+
};
2222

2323
/**
2424
* Build the custom Docker image
@@ -34,34 +34,67 @@ function buildImage(dockerFile) {
3434
return imageName;
3535
};
3636

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+
3753
/**
3854
* Get bind path depending on os platform
3955
* @param {string} servicePath
4056
* @return {string} The bind path.
4157
*/
4258
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-
4859
// 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(/^\/mnt\//, '/');
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(/^\/mnt\//, '/');
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);
6079
} 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+
}
6295
}
6396

64-
return bindPath;
97+
throw new Error('Unable to find good bind path format');
6598
};
6699

67100
module.exports = {buildImage, getBindPath};

0 commit comments

Comments
 (0)