|
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,35 +34,60 @@ 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 | + if (fse.pathExistsSync(path.join(servicePath, 'serverless.json'))) { |
| 50 | + return 'serverless.json'; |
| 51 | + } |
| 52 | + throw new Error( |
| 53 | + 'Unable to find serverless.yml or serverless.yaml or serverless.json for getBindPath()' |
| 54 | + ); |
| 55 | +} |
| 56 | + |
35 | 57 | /**
|
36 | 58 | * Test bind path to make sure it's working
|
37 | 59 | * @param {string} bindPath
|
38 | 60 | * @return {boolean}
|
39 | 61 | */
|
40 |
| -function tryBindPath(bindPath) { |
| 62 | +function tryBindPath(serverless, bindPath, testFile) { |
41 | 63 | const options = [
|
42 | 64 | 'run',
|
43 | 65 | '--rm',
|
44 | 66 | '-v',
|
45 | 67 | `${bindPath}:/test`,
|
46 | 68 | 'alpine',
|
47 | 69 | 'ls',
|
48 |
| - '/test/serverless.yml' |
| 70 | + `/test/${testFile}` |
49 | 71 | ];
|
50 | 72 | try {
|
51 | 73 | const ps = dockerCommand(options);
|
52 |
| - return ps.stdout.trim() === '/test/serverless.yml'; |
| 74 | + if (process.env.SLS_DEBUG) { |
| 75 | + serverless.cli.log(`Trying bindPath ${bindPath} (${options})`); |
| 76 | + serverless.cli.log(ps.stdout.trim()); |
| 77 | + } |
| 78 | + return ps.stdout.trim() === `/test/${testFile}`; |
53 | 79 | } catch (err) {
|
54 | 80 | return false;
|
55 | 81 | }
|
56 | 82 | }
|
57 | 83 |
|
58 | 84 | /**
|
59 | 85 | * Get bind path depending on os platform
|
| 86 | + * @param {object} serverless |
60 | 87 | * @param {string} servicePath
|
61 | 88 | * @return {string} The bind path.
|
62 | 89 | */
|
63 |
| -function getBindPath(servicePath) { |
| 90 | +function getBindPath(serverless, servicePath) { |
64 | 91 | // Determine bind path
|
65 | 92 | if (process.platform !== 'win32' && !isWsl) {
|
66 | 93 | return servicePath;
|
@@ -100,9 +127,11 @@ function getBindPath(servicePath) {
|
100 | 127 | bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
|
101 | 128 | bindPaths.push(`${drive.toUpperCase()}:/${path}`);
|
102 | 129 |
|
| 130 | + const testFile = findTestFile(servicePath); |
| 131 | + |
103 | 132 | for (let i = 0; i < bindPaths.length; i++) {
|
104 | 133 | const bindPath = bindPaths[i];
|
105 |
| - if (tryBindPath(bindPath)) { |
| 134 | + if (tryBindPath(serverless, bindPath, testFile)) { |
106 | 135 | return bindPath;
|
107 | 136 | }
|
108 | 137 | }
|
|
0 commit comments