|
1 | 1 | const { spawnSync } = require('child_process');
|
2 | 2 | const isWsl = require('is-wsl');
|
| 3 | +const fse = require('fs-extra'); |
| 4 | +const path = require('path'); |
3 | 5 |
|
4 | 6 | /**
|
5 | 7 | * Helper function to run a docker command
|
@@ -32,24 +34,43 @@ function buildImage(dockerFile) {
|
32 | 34 | return imageName;
|
33 | 35 | }
|
34 | 36 |
|
| 37 | +/** |
| 38 | + * Find a file that exists on all projects so we can test if Docker can see it too |
| 39 | + * @param {string} servicePath |
| 40 | + * @return {string} file name |
| 41 | + */ |
| 42 | +function findTestFile(servicePath) { |
| 43 | + if (fse.pathExistsSync(path.join(servicePath, 'serverless.yml'))) { |
| 44 | + return 'serverless.yml'; |
| 45 | + } |
| 46 | + if (fse.pathExistsSync(path.join(servicePath, 'serverless.yaml'))) { |
| 47 | + return 'serverless.yaml'; |
| 48 | + } |
| 49 | + throw new Error('Unable to find serverless.yml or serverless.yaml for getBindPath()'); |
| 50 | +} |
| 51 | + |
35 | 52 | /**
|
36 | 53 | * Test bind path to make sure it's working
|
37 | 54 | * @param {string} bindPath
|
38 | 55 | * @return {boolean}
|
39 | 56 | */
|
40 |
| -function tryBindPath(bindPath) { |
| 57 | +function tryBindPath(bindPath, testFile) { |
41 | 58 | const options = [
|
42 | 59 | 'run',
|
43 | 60 | '--rm',
|
44 | 61 | '-v',
|
45 | 62 | `${bindPath}:/test`,
|
46 | 63 | 'alpine',
|
47 | 64 | 'ls',
|
48 |
| - '/test/serverless.yml' |
| 65 | + `/test/${testFile}` |
49 | 66 | ];
|
50 | 67 | try {
|
51 | 68 | const ps = dockerCommand(options);
|
52 |
| - return ps.stdout.trim() === '/test/serverless.yml'; |
| 69 | + if (process.env.SLS_DEBUG) { |
| 70 | + console.log(`Trying bindPath ${bindPath} (${options})`) |
| 71 | + console.log(ps.stdout.trim()); |
| 72 | + } |
| 73 | + return ps.stdout.trim() === `/test/${testFile}`; |
53 | 74 | } catch (err) {
|
54 | 75 | return false;
|
55 | 76 | }
|
@@ -94,15 +115,16 @@ function getBindPath(servicePath) {
|
94 | 115 |
|
95 | 116 | bindPaths.push(`/${drive.toLowerCase()}/${path}`); // Docker Toolbox (seems like Docker for Windows can support this too)
|
96 | 117 | bindPaths.push(`${drive.toLowerCase()}:/${path}`); // Docker for Windows
|
97 |
| - // other options just in case |
98 | 118 | bindPaths.push(`/${drive.toUpperCase()}/${path}`);
|
99 | 119 | bindPaths.push(`/mnt/${drive.toLowerCase()}/${path}`);
|
100 | 120 | bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
|
101 | 121 | bindPaths.push(`${drive.toUpperCase()}:/${path}`);
|
| 122 | + |
| 123 | + const testFile = findTestFile(servicePath); |
102 | 124 |
|
103 | 125 | for (let i = 0; i < bindPaths.length; i++) {
|
104 | 126 | const bindPath = bindPaths[i];
|
105 |
| - if (tryBindPath(bindPath)) { |
| 127 | + if (tryBindPath(bindPath, testFile)) { |
106 | 128 | return bindPath;
|
107 | 129 | }
|
108 | 130 | }
|
|
0 commit comments