Skip to content

Commit 5c42b0a

Browse files
authored
Merge pull request #116 from heri16/patch-3
Fix Windows support for dockerizePip
2 parents 0ed99c4 + 239ba61 commit 5c42b0a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/pip.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fse = require('fs-extra');
22
const path = require('path');
33
const {spawnSync} = require('child_process');
4+
const isWsl = require('is-wsl');
45

56
/**
67
* pip install the requirements to the .serverless/requirements directory
@@ -51,29 +52,39 @@ function installRequirements() {
5152

5253
this.serverless.cli.log(`Docker Image: ${this.options.dockerImage}`);
5354

54-
// Check docker server os type from 'docker version'
55-
let volPath;
55+
// Determine os platform of docker CLI from 'docker version'
5656
options = [
57-
'version', '--format', '{{with .Server}}{{.Os}}{{end}}'
57+
'version', '--format', '{{with .Client}}{{.Os}}{{end}}'
5858
];
59-
const ps = spawnSync(cmd, options, {'timeout': 2000, 'encoding': 'utf-8'});
59+
const ps = spawnSync(cmd, options, {'timeout': 10000, 'encoding': 'utf-8'});
6060
if (ps.error) {
6161
if (ps.error.code === 'ENOENT') {
6262
throw new Error('docker not found! Please install it.');
6363
}
6464
throw new Error(ps.error);
65-
}
66-
if (ps.status !== 0) {
65+
} else if (ps.status !== 0) {
6766
throw new Error(ps.stderr);
68-
} else if (ps.stdout.trim() === 'windows') {
69-
volPath = this.servicePath.replace(/\\/g, '/').replace(/^\/mnt\//, '/');
67+
}
68+
69+
let bindPath;
70+
const cliPlatform = ps.stdout.trim();
71+
if (process.platform === 'win32') {
72+
bindPath = this.servicePath.replace(/\\([^\s])/g, '/$1');
73+
if (cliPlatform === 'windows') {
74+
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
75+
}
76+
} else if (isWsl) {
77+
bindPath = this.servicePath.replace(/^\/mnt\//, '/');
78+
if (cliPlatform === 'windows') {
79+
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
80+
}
7081
} else {
71-
volPath = this.servicePath;
82+
bindPath = this.servicePath;
7283
}
7384

7485
options = [
7586
'run', '--rm',
76-
'-v', `${volPath}:/var/task:z`,
87+
'-v', `${bindPath}:/var/task:z`,
7788
];
7889
if (process.platform === 'linux')
7990
options.push('-u', `${process.getuid()}:${process.getgid()}`);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"fs-extra": "^3.0.1",
5252
"glob-all": "^3.1.0",
5353
"graceful-fs": "^4.1.11",
54+
"is-wsl": "^1.1.0",
5455
"lodash": "^4.13.1"
5556
}
5657
}

0 commit comments

Comments
 (0)