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