Skip to content

Commit a24cbd1

Browse files
author
heri16
committed
Fix Windows support for dockerizePip
1 parent 32df7d6 commit a24cbd1

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/pip.js

Lines changed: 20 additions & 9 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
@@ -50,29 +51,39 @@ function installRequirements() {
5051

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

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

7384
options = [
7485
'run', '--rm',
75-
'-v', `${volPath}:/var/task:z`,
86+
'-v', `${bindPath}:/var/task:z`,
7687
];
7788
if (process.platform === 'linux')
7889
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)