Skip to content

Commit 13fe80f

Browse files
author
bweigel
committed
- make some mehtods static
- fix logging
1 parent 67695d2 commit 13fe80f

File tree

1 file changed

+51
-50
lines changed

1 file changed

+51
-50
lines changed

lib/index.js

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,65 +50,37 @@ class ServerlessPlugin {
5050
return inputPath
5151
}
5252
};
53-
const source = getAbsPath(this.getFnSourceDir(functionObject) || functionName);
54-
const target = getAbsPath(this.getFnTarget(functionObject) || `${functionName}.zip`);
55-
const libs = getAbsPath(this.getLibDir(functionObject));
56-
const globs = this.getFnIncludeGlobs(functionObject);
53+
const source = getAbsPath(getFnSourceDir(functionObject) || functionName);
54+
const target = getAbsPath(getFnTarget(functionObject) || `${functionName}.zip`);
55+
const libs = getAbsPath(getLibDir(functionObject));
56+
const globs = getFnIncludeGlobs(functionObject);
5757

58-
const zipWithGlobs = globs => libs => source => target => this.zipSources(source, target, globs, libs);
58+
const zipWithGlobs = globs => libs => source => target => this.zipSources(source, target, globs, libs, this.serverless);
5959

6060
return await
61-
zipWithGlobs(globs)(libs)(source)(target).then((artifactPath) => {
62-
return artifactPath;
63-
});
64-
}
65-
66-
getFnIncludeGlobs(functionObject) {
67-
if ('package' in functionObject && 'include_globs' in functionObject.package) {
68-
return functionObject.package.include_globs
69-
}
70-
return '**/*.py'
71-
}
72-
73-
getFnSourceDir(functionObject) {
74-
if ('package' in functionObject && 'path' in functionObject.package) {
75-
return functionObject.package.path
76-
}
77-
return false
78-
}
79-
80-
getFnTarget(functionObject) {
81-
if ('package' in functionObject && 'artifact' in functionObject.package) {
82-
return functionObject.package.artifact
83-
}
84-
return false
85-
}
86-
87-
getLibDir(functionObject) {
88-
if ('package' in functionObject && 'libs' in functionObject.package) {
89-
return functionObject.package.libs
90-
}
91-
return false
61+
zipWithGlobs(globs)(libs)(source)(target).then((artifactPath) => {
62+
return artifactPath;
63+
});
9264
}
9365

9466
async createDirIfNotExists(dir) {
9567
if (!fs.existsSync(dir)) {
9668
await
97-
new Promise(async (resolve, reject) => {
98-
fs.mkdir(dir, err => {
99-
if (err) {
100-
resolve(false)
101-
}
102-
else {
103-
resolve(true)
104-
}
105-
})
106-
});
69+
new Promise(async (resolve, reject) => {
70+
fs.mkdirSync(dir, err => {
71+
if (err) {
72+
resolve(false)
73+
}
74+
else {
75+
resolve(true)
76+
}
77+
})
78+
});
10779
}
10880
return true
10981
}
11082

111-
async zipSources(source, target, globs, libs) {
83+
async zipSources(source, target, globs, libs, serverless) {
11284

11385
this.createDirIfNotExists(path.dirname(target));
11486

@@ -118,8 +90,7 @@ class ServerlessPlugin {
11890
this.log("Zipping " + source + " to " + target);
11991

12092
output.on('finish', function () {
121-
console.log(archive.pointer() + ' total bytes');
122-
console.log(target + ' finalized and closed.');
93+
serverless.cli.log(target + ' finalized and closed. (' + (archive.pointer() / 1024).toPrecision(3) + ' kB)');
12394
});
12495

12596
archive.on('error', function (err) {
@@ -155,11 +126,41 @@ class ServerlessPlugin {
155126

156127
return new Promise((resolve, reject) => {
157128
output.on('close', () => resolve(target));
158-
archive.on('error', (err) => reject(err));
129+
output.on('error', (err) => reject(err));
159130
});
160131

161132
}
162133

163134
}
164135

136+
137+
function getFnIncludeGlobs(functionObject) {
138+
if ('package' in functionObject && 'include_globs' in functionObject.package) {
139+
return functionObject.package.include_globs
140+
}
141+
return '**/*.py'
142+
}
143+
144+
function getFnSourceDir(functionObject) {
145+
if ('package' in functionObject && 'path' in functionObject.package) {
146+
return functionObject.package.path
147+
}
148+
return false
149+
}
150+
151+
function getFnTarget(functionObject) {
152+
if ('package' in functionObject && 'artifact' in functionObject.package) {
153+
return functionObject.package.artifact
154+
}
155+
return false
156+
}
157+
158+
function getLibDir(functionObject) {
159+
if ('package' in functionObject && 'libs' in functionObject.package) {
160+
return functionObject.package.libs
161+
}
162+
return false
163+
}
164+
165+
165166
module.exports = ServerlessPlugin;

0 commit comments

Comments
 (0)