Skip to content

Commit 80bdb55

Browse files
author
Farley
committed
fixing linting
1 parent 899cb6a commit 80bdb55

File tree

5 files changed

+95
-78
lines changed

5 files changed

+95
-78
lines changed

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ class ServerlessPythonRequirements {
104104
lifecycleEvents: [
105105
'cleanStatic',
106106
],
107-
}
107+
},
108108
},
109109
},
110110
};
111-
111+
112112
const clean = () => BbPromise.bind(this)
113113
.then(cleanup)
114114
.then(removeVendorHelper);
115-
115+
116116
const before = () => BbPromise.bind(this)
117117
.then(pipfileToRequirements)
118118
.then(addVendorHelper)
@@ -125,11 +125,11 @@ class ServerlessPythonRequirements {
125125

126126
const invalidateCaches = () => {
127127
if (this.options.invalidateCaches) {
128-
return clean
128+
return clean;
129129
}
130130
return BbPromise.resolve();
131131
};
132-
132+
133133
const cleanStatic = () => BbPromise.bind(this)
134134
.then(cleanupStaticCache);
135135

lib/clean.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ function cleanup() {
3535
* @return {Promise}
3636
*/
3737
function cleanupStaticCache() {
38-
const staticCacheLocation = getStaticCachePath(this.options)
38+
const staticCacheLocation = getStaticCachePath(this.options);
3939
if (fse.existsSync(staticCacheLocation)) {
40-
this.serverless.cli.log(`Removing static caches at: ${staticCacheLocation}`)
41-
42-
// Only remove cache folders that we added, just incase someone accidentally puts a weird
40+
this.serverless.cli.log(`Removing static caches at: ${staticCacheLocation}`);
41+
42+
// Only remove cache folders that we added, just incase someone accidentally puts a weird
4343
// static cache location so we don't remove a bunch of personal stuff
44-
promises = []
45-
glob.sync([path.join(staticCacheLocation, '*')], { mark: true, dot: false }).forEach((file) => {
44+
promises = [];
45+
glob.sync([path.join(staticCacheLocation, '*')], {mark: true, dot: false}).forEach((file) => {
4646
if (file.endsWith('_slspyc/')) {
47-
promises.push( fse.removeAsync(file) )
47+
promises.push( fse.removeAsync(file) );
4848
}
4949
});
50-
return BbPromise.all( promises )
50+
return BbPromise.all( promises );
5151
} else {
52-
this.serverless.cli.log(`No static cache found`)
53-
return BbPromise.resolve()
52+
this.serverless.cli.log(`No static cache found`);
53+
return BbPromise.resolve();
5454
}
5555
}
5656

lib/inject.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ function injectAllRequirements() {
120120
});
121121
} else {
122122
// Non-individually packaged function
123-
const reqsFilePath = path.join('.serverless', '_requirements.txt')
123+
const reqsFilePath = path.join('.serverless', '_requirements.txt');
124124
if (!fse.existsSync(reqsFilePath) || fse.statSync(reqsFilePath).size == 0) {
125-
this.serverless.cli.log(`Skipping injection of empty/missing requirements.txt file from ${reqsFilePath}`);
126-
return
125+
this.serverless.cli.log(`Skipping empty requirements.txt file from ${reqsFilePath}`);
126+
return;
127127
}
128-
const reqChecksum = md5Path(reqsFilePath)
129-
const workingReqsFolder = getRequirementsWorkingPath(reqChecksum, this.servicePath, this.options)
128+
const reqChecksum = md5Path(reqsFilePath);
129+
const workingReqsFolder = getRequirementsWorkingPath(
130+
reqChecksum, this.servicePath, this.options);
130131

131132
injectRequirements(
132133
workingReqsFolder,

lib/pip.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const set = require('lodash.set');
66
const {spawnSync} = require('child_process');
77
const values = require('lodash.values');
88
const {buildImage, getBindPath, getDockerUid} = require('./docker');
9-
const {checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, md5Path} = require('./shared');
9+
const {
10+
checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, md5Path,
11+
} = require('./shared');
1012

1113
/**
12-
+ * Just generate the requirements file in the .serverless folder
14+
* Just generate the requirements file in the .serverless folder
1315
* @param {string} requirementsPath
1416
* @param {string} targetFile
1517
* @param {Object} serverless
@@ -44,9 +46,8 @@ function installRequirementsFile(requirementsPath, targetFile, serverless, servi
4446
* @return {undefined}
4547
*/
4648
function installRequirements(targetFolder, serverless, options) {
47-
4849
const targetRequirementsTxt = path.join(targetFolder, 'requirements.txt');
49-
50+
5051
serverless.cli.log(`Installing requirements from ${targetRequirementsTxt} ...`);
5152

5253
let cmd;
@@ -77,7 +78,7 @@ function installRequirements(targetFolder, serverless, options) {
7778
pipCmd.push('--system');
7879
}
7980
}
80-
81+
8182
// If we are dockerizing pip
8283
if (options.dockerizePip) {
8384
cmd = 'docker';
@@ -154,8 +155,10 @@ function generateRequirementsFile(source, target, options) {
154155
const noDeploy = new Set(options.noDeploy || []);
155156
const requirements = fse.readFileSync(source, {encoding: 'utf-8'}).split(/\r?\n/);
156157
const filteredRequirements = requirements.filter((req) => {
157-
req = req.trim()
158-
if (req.length == 0 || req[0] == '#') { return false; } // Skip empty lines and comments
158+
req = req.trim();
159+
if (req.length == 0 || req[0] == '#') {
160+
return false;
161+
}
159162
return !noDeploy.has(req.split(/[=<> \t]/)[0].trim());
160163
});
161164
filteredRequirements.sort(); // Sort them alphabetically
@@ -187,8 +190,6 @@ function copyVendors(vendorFolder, targetFolder, serverless) {
187190
}
188191

189192

190-
191-
192193
/**
193194
* This evaluates if requirements are actually needed to be installed, but fails
194195
* gracefully if no req file is found intentionally. It also assists with code
@@ -200,13 +201,12 @@ function copyVendors(vendorFolder, targetFolder, serverless) {
200201
* @return {string}
201202
*/
202203
function installRequirementsIfNeeded(servicePath, modulePath, options, serverless) {
203-
204204
// Our source requirements, under our service path, and our module path (if specified)
205-
const fileName = path.join(servicePath, modulePath, options.fileName)
206-
207-
// First, generate the requirements file to our local .serverless folder
205+
const fileName = path.join(servicePath, modulePath, options.fileName);
206+
207+
// First, generate the requirements file to our local .serverless folder
208208
fse.ensureDirSync(path.join(servicePath, '.serverless'));
209-
const slsReqsTxt = path.join(servicePath, '.serverless', 'requirements.txt')
209+
const slsReqsTxt = path.join(servicePath, '.serverless', 'requirements.txt');
210210
// Incase it's laying around from a previous package (when individually/failed packaging)
211211
if (fse.existsSync(slsReqsTxt)) {
212212
fse.removeSync(slsReqsTxt);
@@ -218,29 +218,30 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
218218
servicePath,
219219
options
220220
);
221-
221+
222222
// If no requirements file or an empty requirements file, then do nothing
223223
if (!fse.existsSync(slsReqsTxt) || fse.statSync(slsReqsTxt).size == 0) {
224224
serverless.cli.log(`Skipping empty output requirements.txt file from ${slsReqsTxt}`);
225225
return false;
226226
}
227-
227+
228228
// Copy our requirements to another filename in .serverless (incase of individually packaged)
229-
destinationFile = path.join(servicePath, '.serverless', modulePath + "_requirements.txt")
229+
destinationFile = path.join(servicePath, '.serverless', modulePath + '_requirements.txt');
230230
serverless.cli.log(`Copying from ${slsReqsTxt} into ${destinationFile} ...`);
231-
fse.copySync(slsReqsTxt, destinationFile)
232-
231+
fse.copySync(slsReqsTxt, destinationFile);
232+
233233
// Then generate our MD5 Sum of this requirements file to determine where it should "go" to and/or pull cache from
234-
const reqChecksum = md5Path(slsReqsTxt)
235-
234+
const reqChecksum = md5Path(slsReqsTxt);
235+
236236
// Then figure out where this cache should be, if we're caching, etc
237-
const workingReqsFolder = getRequirementsWorkingPath(reqChecksum, servicePath, options)
238-
237+
const workingReqsFolder = getRequirementsWorkingPath(reqChecksum, servicePath, options);
238+
239239
// Check if our static cache is present and is valid
240240
if (fse.existsSync(workingReqsFolder)) {
241241
if (fse.existsSync(path.join(workingReqsFolder, '.completed_requirements'))) {
242242
serverless.cli.log(`Using static cache of requirements found at ${workingReqsFolder} ...`);
243-
fse.utimesSync(workingReqsFolder, new Date(), new Date()) // We'll "touch" the folder, as to bring it to the start of the FIFO cache
243+
// We'll "touch" the folder, as to bring it to the start of the FIFO cache
244+
fse.utimesSync(workingReqsFolder, new Date(), new Date());
244245
fse.removeSync(slsReqsTxt);
245246
return workingReqsFolder;
246247
}
@@ -249,13 +250,13 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
249250
rimraf.sync(workingReqsFolder);
250251
}
251252
}
252-
253+
253254
// Ensuring the working reqs folder exists
254255
fse.ensureDirSync(workingReqsFolder);
255-
256+
256257
// Copy our requirements.txt into our working folder...
257258
fse.copySync(slsReqsTxt, path.join(workingReqsFolder, 'requirements.txt'));
258-
259+
259260
// Then install our requirements from this folder
260261
installRequirements(
261262
workingReqsFolder,
@@ -270,13 +271,13 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
270271
serverless
271272
);
272273
}
273-
274+
274275
// Then touch our ".completed_requirements" file so we know we can use this for static cache
275276
if (options.useStaticCache) {
276277
fse.closeSync(fse.openSync(path.join(workingReqsFolder, '.completed_requirements'), 'w'));
277278
}
278279
fse.removeSync(slsReqsTxt); // Clean up after ourselves
279-
return workingReqsFolder
280+
return workingReqsFolder;
280281
}
281282

282283

@@ -285,9 +286,8 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
285286
* @return {undefined}
286287
*/
287288
function installAllRequirements() {
288-
289289
// First, check and delete cache versions, if enabled
290-
checkForAndDeleteMaxCacheVersions(this.options, this.serverless)
290+
checkForAndDeleteMaxCacheVersions(this.options, this.serverless);
291291

292292
// Then if we're going to package functions individually...
293293
if (this.serverless.service.package.individually) {
@@ -300,19 +300,26 @@ function installAllRequirements() {
300300
}
301301
// If we didn't already process a module (functions can re-use modules)
302302
if (!doneModules.includes(f.module)) {
303-
const reqsInstalledAt = installRequirementsIfNeeded(this.servicePath, f.module, this.options, this.serverless);
303+
const reqsInstalledAt = installRequirementsIfNeeded(
304+
this.servicePath, f.module, this.options, this.serverless
305+
);
304306
// Add symlinks into .serverless for each module so it's easier for injecting and for users to see where reqs are
305307
if (reqsInstalledAt) {
306-
fse.symlink(reqsInstalledAt, path.join(this.servicePath, '.serverless', `${f.module}_requirements`))
308+
fse.symlink(
309+
reqsInstalledAt,
310+
path.join(this.servicePath, '.serverless', `${f.module}_requirements`)
311+
);
307312
}
308313
doneModules.push(f.module);
309314
}
310315
});
311316
} else {
312-
const reqsInstalledAt = installRequirementsIfNeeded(this.servicePath, '', this.options, this.serverless);
317+
const reqsInstalledAt = installRequirementsIfNeeded(
318+
this.servicePath, '', this.options, this.serverless
319+
);
313320
// Add symlinks into .serverless for so it's easier for injecting and for users to see where reqs are
314321
if (reqsInstalledAt) {
315-
fse.symlink(reqsInstalledAt, path.join(this.servicePath, '.serverless', `_requirements`))
322+
fse.symlink(reqsInstalledAt, path.join(this.servicePath, '.serverless', `_requirements`));
316323
}
317324
}
318325
};

lib/shared.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
const appdir = require('appdirectory')
1+
const Appdir = require('appdirectory');
22
const rimraf = require('rimraf');
3-
const md5File = require('md5-file')
3+
const md5File = require('md5-file');
44
const glob = require('glob-all');
55
const path = require('path');
66
const fse = require('fs-extra');
77

88

99
/**
10-
* This helper will check if we're using static cache and have max
10+
* This helper will check if we're using static cache and have max
1111
* versions enabled and will delete older versions in a fifo fashion
1212
* @param {Object} options
1313
* @param {Object} serverless
1414
* @return {undefined}
1515
*/
1616
function checkForAndDeleteMaxCacheVersions(options, serverless) {
1717
// If we're using the static cache, and we have static cache max versions enabled
18-
if (options.useStaticCache && options.staticCacheMaxVersions && parseInt(options.staticCacheMaxVersions) > 0) {
18+
if (options.useStaticCache &&
19+
options.staticCacheMaxVersions &&
20+
parseInt(options.staticCacheMaxVersions) > 0) {
1921
// Get the list of our cache files
20-
files = glob.sync([path.join(getStaticCachePath(options), '*_slspyc/')], { mark: true, dot: false })
22+
files = glob.sync([path.join(getStaticCachePath(options), '*_slspyc/')], {mark: true});
2123
// Check if we have too many
2224
if (files.length >= options.staticCacheMaxVersions) {
2325
// Sort by modified time
2426
files.sort(function(a, b) {
2527
return fse.statSync(a).mtime.getTime() - fse.statSync(b).mtime.getTime();
2628
});
2729
// Remove the older files...
28-
var i = 0;
30+
const i = 0;
2931
for ( i = 0; i < (files.length - options.staticCacheMaxVersions) + 1; i++ ) {
30-
rimraf.sync(files[i])
32+
rimraf.sync(files[i]);
3133
}
3234
// Log the number of cache files flushed
33-
serverless.cli.log(`Removed ${i} items from cache because of staticCacheMaxVersions`)
35+
serverless.cli.log(`Removed ${i} items from cache because of staticCacheMaxVersions`);
3436
}
3537
}
3638
}
@@ -41,49 +43,56 @@ function checkForAndDeleteMaxCacheVersions(options, serverless) {
4143
* @param {string} subfolder
4244
* @param {string} servicePath
4345
* @param {Object} options
44-
* @return string
46+
* @return {string}
4547
*/
4648
function getRequirementsWorkingPath(subfolder, servicePath, options) {
4749
// If we want to use the static cache
4850
if (options && options.useStaticCache) {
49-
if (subfolder) { subfolder = subfolder + "_slspyc" }
51+
if (subfolder) {
52+
subfolder = subfolder + '_slspyc';
53+
}
5054
// If we have max number of cache items...
5155

52-
return path.join( getStaticCachePath(options), subfolder)
56+
return path.join( getStaticCachePath(options), subfolder);
5357
}
54-
58+
5559
// If we don't want to use the static cache, then fallback to the way things used to work
56-
return path.join(servicePath, '.serverless', 'requirements')
60+
return path.join(servicePath, '.serverless', 'requirements');
5761
}
5862

5963

6064
/**
6165
* The static cache path that will be used for this system + options, used if static cache is enabled
6266
* @param {Object} options
63-
* @return string
67+
* @return {string}
6468
*/
6569
function getStaticCachePath(options) {
6670
// If we've manually set the static cache location
6771
if (options && options.staticCacheLocation) {
68-
return path.join(options.staticCacheLocation)
72+
return path.join(options.staticCacheLocation);
6973
}
7074

7175
// Otherwise, find/use the python-ey appdirs cache location
72-
var dirs = new appdir({
73-
appName: "serverless-python-requirements",
74-
appAuthor: "UnitedIncome"
75-
})
76-
return dirs.userCache()
76+
const dirs = new Appdir({
77+
appName: 'serverless-python-requirements',
78+
appAuthor: 'UnitedIncome',
79+
});
80+
return dirs.userCache();
7781
}
7882

7983

8084
/**
8185
* Helper to get the md5 a a file's contents to determine if a requirements has a static cache
8286
* @param {string} fullpath
83-
* @return string
87+
* @return {string}
8488
*/
8589
function md5Path(fullpath) {
86-
return md5File.sync(fullpath)
90+
return md5File.sync(fullpath);
8791
}
8892

89-
module.exports = {checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, getStaticCachePath, md5Path};
93+
module.exports = {
94+
checkForAndDeleteMaxCacheVersions,
95+
getRequirementsWorkingPath,
96+
getStaticCachePath,
97+
md5Path,
98+
};

0 commit comments

Comments
 (0)