Skip to content

Commit 59566e4

Browse files
authored
Support serverless.yml and serverless.yaml
This should fix the issue reported by @mrpgraae in serverless#210. I've also added support for `SLS_DEBUG` so we can more easily get some information on future errors.
1 parent 71f36e7 commit 59566e4

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

lib/docker.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { spawnSync } = require('child_process');
22
const isWsl = require('is-wsl');
3+
const fse = require('fs-extra');
4+
const path = require('path');
35

46
/**
57
* Helper function to run a docker command
@@ -32,24 +34,43 @@ function buildImage(dockerFile) {
3234
return imageName;
3335
}
3436

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+
3552
/**
3653
* Test bind path to make sure it's working
3754
* @param {string} bindPath
3855
* @return {boolean}
3956
*/
40-
function tryBindPath(bindPath) {
57+
function tryBindPath(bindPath, testFile) {
4158
const options = [
4259
'run',
4360
'--rm',
4461
'-v',
4562
`${bindPath}:/test`,
4663
'alpine',
4764
'ls',
48-
'/test/serverless.yml'
65+
`/test/${testFile}`
4966
];
5067
try {
5168
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}`;
5374
} catch (err) {
5475
return false;
5576
}
@@ -94,15 +115,16 @@ function getBindPath(servicePath) {
94115

95116
bindPaths.push(`/${drive.toLowerCase()}/${path}`); // Docker Toolbox (seems like Docker for Windows can support this too)
96117
bindPaths.push(`${drive.toLowerCase()}:/${path}`); // Docker for Windows
97-
// other options just in case
98118
bindPaths.push(`/${drive.toUpperCase()}/${path}`);
99119
bindPaths.push(`/mnt/${drive.toLowerCase()}/${path}`);
100120
bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
101121
bindPaths.push(`${drive.toUpperCase()}:/${path}`);
122+
123+
const testFile = findTestFile(servicePath);
102124

103125
for (let i = 0; i < bindPaths.length; i++) {
104126
const bindPath = bindPaths[i];
105-
if (tryBindPath(bindPath)) {
127+
if (tryBindPath(bindPath, testFile)) {
106128
return bindPath;
107129
}
108130
}

0 commit comments

Comments
 (0)