|
1 | 1 | const fse = require('fs-extra');
|
2 | 2 | const path = require('path');
|
3 | 3 | const {spawnSync} = require('child_process');
|
| 4 | +const isWsl = require('is-wsl'); |
4 | 5 |
|
5 | 6 | /**
|
6 | 7 | * pip install the requirements to the .serverless/requirements directory
|
@@ -50,29 +51,39 @@ function installRequirements() {
|
50 | 51 |
|
51 | 52 | this.serverless.cli.log(`Docker Image: ${this.options.dockerImage}`);
|
52 | 53 |
|
53 |
| - // Check docker server os type from 'docker version' |
54 |
| - let volPath; |
| 54 | + // Determine os platform of docker CLI from 'docker version' |
55 | 55 | options = [
|
56 |
| - 'version', '--format', '{{with .Server}}{{.Os}}{{end}}' |
| 56 | + 'version', '--format', '{{with .Client}}{{.Os}}{{end}}' |
57 | 57 | ];
|
58 |
| - const ps = spawnSync(cmd, options, {'timeout': 2000, 'encoding': 'utf-8'}); |
| 58 | + const ps = spawnSync(cmd, options, {'timeout': 10000, 'encoding': 'utf-8'}); |
59 | 59 | if (ps.error) {
|
60 | 60 | if (ps.error.code === 'ENOENT') {
|
61 | 61 | throw new Error('docker not found! Please install it.');
|
62 | 62 | }
|
63 | 63 | throw new Error(ps.error);
|
64 |
| - } |
65 |
| - if (ps.status !== 0) { |
| 64 | + } else if (ps.status !== 0) { |
66 | 65 | 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 | + } |
69 | 80 | } else {
|
70 |
| - volPath = this.servicePath; |
| 81 | + bindPath = this.servicePath; |
71 | 82 | }
|
72 | 83 |
|
73 | 84 | options = [
|
74 | 85 | 'run', '--rm',
|
75 |
| - '-v', `${volPath}:/var/task:z`, |
| 86 | + '-v', `${bindPath}:/var/task:z`, |
76 | 87 | ];
|
77 | 88 | if (process.platform === 'linux')
|
78 | 89 | options.push('-u', `${process.getuid()}:${process.getgid()}`);
|
|
0 commit comments