Skip to content

Commit ce8eb24

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

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/pip.js

Lines changed: 20 additions & 7 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,10 +51,10 @@ 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 bindPath semantics from 'docker version'
55+
let bindPath;
5556
options = [
56-
'version', '--format', '{{with .Server}}{{.Os}}{{end}}'
57+
'version', '--format', '{{with .Client}}{{.Os}}{{end}}'
5758
];
5859
const ps = spawnSync(cmd, options, {'timeout': 2000, 'encoding': 'utf-8'});
5960
if (ps.error) {
@@ -64,15 +65,27 @@ function installRequirements() {
6465
}
6566
if (ps.status !== 0) {
6667
throw new Error(ps.stderr);
67-
} else if (ps.stdout.trim() === 'windows') {
68-
volPath = this.servicePath.replace(/\\/g, '/').replace(/^\/mnt\//, '/');
68+
}
69+
70+
const cliPlatform = ps.stdout.trim();
71+
72+
if (process.platform === 'win32') {
73+
bindPath = this.servicePath.replace(/\\([^\s])/g, '/$1');
74+
if (cliPlatform === 'windows') {
75+
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
76+
}
77+
} else if (isWsl) {
78+
bindPath = this.servicePath.replace(/^\/mnt\//, '/');
79+
if (cliPlatform === 'windows') {
80+
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
81+
}
6982
} else {
70-
volPath = this.servicePath;
83+
bindPath = this.servicePath;
7184
}
7285

7386
options = [
7487
'run', '--rm',
75-
'-v', `${volPath}:/var/task:z`,
88+
'-v', `${bindPath}:/var/task:z`,
7689
];
7790
if (process.platform === 'linux')
7891
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)