Skip to content

Commit fa12ab4

Browse files
kichikdschep
authored andcommitted
Support serverless.yml and serverless.yaml in getBindPath() (#213)
This should fix the issue reported by @mrpgraae in #210. I've also added support for `SLS_DEBUG` so we can more easily get some information on future errors.
1 parent 37adf9e commit fa12ab4

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/docker.js

+34-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,35 +34,60 @@ 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+
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+
3557
/**
3658
* Test bind path to make sure it's working
3759
* @param {string} bindPath
3860
* @return {boolean}
3961
*/
40-
function tryBindPath(bindPath) {
62+
function tryBindPath(serverless, bindPath, testFile) {
4163
const options = [
4264
'run',
4365
'--rm',
4466
'-v',
4567
`${bindPath}:/test`,
4668
'alpine',
4769
'ls',
48-
'/test/serverless.yml'
70+
`/test/${testFile}`
4971
];
5072
try {
5173
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}`;
5379
} catch (err) {
5480
return false;
5581
}
5682
}
5783

5884
/**
5985
* Get bind path depending on os platform
86+
* @param {object} serverless
6087
* @param {string} servicePath
6188
* @return {string} The bind path.
6289
*/
63-
function getBindPath(servicePath) {
90+
function getBindPath(serverless, servicePath) {
6491
// Determine bind path
6592
if (process.platform !== 'win32' && !isWsl) {
6693
return servicePath;
@@ -100,9 +127,11 @@ function getBindPath(servicePath) {
100127
bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
101128
bindPaths.push(`${drive.toUpperCase()}:/${path}`);
102129

130+
const testFile = findTestFile(servicePath);
131+
103132
for (let i = 0; i < bindPaths.length; i++) {
104133
const bindPath = bindPaths[i];
105-
if (tryBindPath(bindPath)) {
134+
if (tryBindPath(serverless, bindPath, testFile)) {
106135
return bindPath;
107136
}
108137
}

lib/pip.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function installRequirements(
8787
serverless.cli.log(`Docker Image: ${dockerImage}`);
8888

8989
// Prepare bind path depending on os platform
90-
const bindPath = getBindPath(servicePath);
90+
const bindPath = getBindPath(serverless, servicePath);
9191

9292
cmdOptions = ['run', '--rm', '-v', `"${bindPath}:/var/task:z"`];
9393
if (options.dockerSsh) {

0 commit comments

Comments
 (0)