Skip to content

Commit 38242b1

Browse files
author
Bruno Agutoli
committed
1 parent f83ed9d commit 38242b1

File tree

8 files changed

+124
-34
lines changed

8 files changed

+124
-34
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ functions:
110110
| packagePath | `string` | package.json | `(DEPRECATED)`: Available for `<= 1.5.0`, for versions `>= 2.x` please use `compatibleRuntimes` |
111111
| dependenciesPath | `string` | package.json | Note: `>= 2.x` versions. You can specify custom path for your package.json |
112112
| compatibleRuntimes | `array` | `['nodejs']` | Possible values: nodejs, nodejs10.x, nodejs12.x |
113+
| layerOptimization.cleanupPatterns | `array` | [check](https://github.com/agutoli/serverless-layers/blob/master/src/runtimes/nodejs.js) | The pattern of files to cleanup in the layer artifact before uploading it. |
113114

114115
----------------------
115116

@@ -126,6 +127,7 @@ functions:
126127
| packageManager | `string` | bundle | Possible values: bundle |
127128
| dependenciesPath | `string` | Gemfile | Note: Available for `>= 2.x` versions. You can specify custom path for your requirements.txt |
128129
| compatibleRuntimes | `array` | `['ruby']` | Possible values: ruby2.5, ruby2.7 |
130+
| layerOptimization.cleanupPatterns | `array` | [check](https://github.com/agutoli/serverless-layers/blob/master/src/runtimes/ruby.js) | The pattern of files to cleanup in the layer artifact before uploading it. |
129131

130132
----------------------
131133

@@ -140,7 +142,8 @@ functions:
140142
| -------------- | --------- | ----------- | ----------- |
141143
| packageManager | `string` | pip | Possible values: pip |
142144
| dependenciesPath | `string` | requirements.txt | Note: Available for `>= 2.x` versions. You can specify custom path for your requirements.txt |
143-
| compatibleRuntimes | `array` | `['python']` | Possible values: python2.7, python3.6, python3.7 and python3.8 |
145+
| compatibleRuntimes | `array` | `['python']` | Possible values: python2.7, python3.x |
146+
| layerOptimization.cleanupPatterns | `array` | [check](https://github.com/agutoli/serverless-layers/blob/master/src/runtimes/python.js) | The pattern of files to cleanup in the layer artifact before uploading it. |
144147

145148
----------------------
146149

@@ -151,7 +154,7 @@ This plugin will setup follow options automatically if not specified at `serverl
151154
| Option | Type | Default |
152155
| -------------- | --------- | ----------- |
153156
| package.individually | `bool` | false |
154-
| package.exclude | `array` | `['node_modules/**']` |
157+
| package.patterns | `array` | `['node_modules/**']` |
155158
| package.excludeDevDependencies | `bool` | false |
156159

157160
## Mininal Policy permissions for CI/CD IAM users

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-layers",
3-
"version": "2.6.1",
3+
"version": "2.7.0",
44
"description": "",
55
"main": "lib/index.js",
66
"bugs": {

src/index.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,11 @@ class ServerlessLayers {
199199
return false;
200200
}
201201

202-
// by pass settings
203-
if (!this.settings.localDir) {
204-
return false;
205-
}
206-
207202
const manifest = '__meta__/manifest-settings.json';
208-
const currentSettings = JSON.stringify(this.settings);
203+
const currentSettings = JSON.stringify({
204+
...this.settings,
205+
patterns: this.service.package.patterns
206+
});
209207

210208
// settings checked
211209
this.hasSettingsVerified = true;
@@ -305,7 +303,7 @@ class ServerlessLayers {
305303
hasDepsChanges,
306304
hasFoldersChanges,
307305
hasSettingsChanges,
308-
hasCustomHashChanged
306+
hasCustomHashChanged,
309307
].some(x => x === true);
310308

311309
// merge package default options
@@ -423,26 +421,26 @@ class ServerlessLayers {
423421
}
424422

425423
mergePackageOptions() {
426-
const { packageExclude, artifact } = this.settings;
424+
const { packagePatterns, artifact } = this.settings;
427425
const pkg = this.service.package;
428426

429427
const opts = {
430428
individually: false,
431429
excludeDevDependencies: false,
432-
exclude: []
430+
patterns: []
433431
};
434432

435433
this.service.package = {...opts, ...pkg};
436434

437-
for (const excludeFile of packageExclude) {
438-
const hasRule = (this.service.package.exclude || '').indexOf(excludeFile);
435+
for (const excludeFile of packagePatterns) {
436+
const hasRule = (this.service.package.patterns || '').indexOf(excludeFile);
439437
if (hasRule === -1) {
440-
this.service.package.exclude.push(excludeFile);
438+
this.service.package.patterns.push(excludeFile);
441439
}
442440
}
443441

444442
if (artifact) {
445-
this.service.package.exclude.push(artifact);
443+
this.service.package.patterns.push(artifact);
446444
}
447445
}
448446

src/package/Dependencies.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Dependencies extends AbstractService {
4747
*
4848
* Reference: https://www.serverless.com/framework/docs/providers/aws/guide/packaging
4949
*/
50-
for (let pattern of this.plugin.service.package.patterns) {
50+
for (let pattern of this.plugin.settings.layerOptimization.cleanupPatterns) {
5151
if (pattern.startsWith('!')) {
5252
const resolvedFiles = await resolveFile(pattern.substr(1), {
5353
cwd: this.layersPackageDir
@@ -65,7 +65,9 @@ class Dependencies extends AbstractService {
6565
filesToExclude.forEach((filename) => {
6666
// check if folder or files are being ignored, and shouldn't be removed.
6767
const shouldBeIgnored = filesToIgnore.filter(x => x.startsWith(filename)).length > 0;
68+
6869
if (!shouldBeIgnored) {
70+
this.plugin.warn(`[layerOptimization.cleanupPatterns] Ignored: ${filename}`);
6971
fs.rmSync(path.join(this.layersPackageDir, filename), {force: true, recursive: true});
7072
}
7173
});
@@ -146,7 +148,16 @@ class Dependencies extends AbstractService {
146148
}
147149

148150
// cleanup files
149-
await this.excludePatternFiles();
151+
try {
152+
await this.excludePatternFiles();
153+
} catch(err) {
154+
if (!this.plugin.service.package.patterns) {
155+
this.plugin.warn(`[warning] package.patterns option is not set. @see https://www.serverless.com/framework/docs/providers/aws/guide/packaging`);
156+
} else {
157+
console.error(err);
158+
process.exit(1);
159+
}
160+
}
150161
}
151162
}
152163

src/runtimes/nodejs.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,42 @@ class NodeJSRuntime {
1919
'yarn.lock',
2020
'package-lock.json'
2121
],
22-
packageExclude: [
23-
'node_modules/**',
24-
]
22+
packagePatterns: [
23+
'!node_modules/**',
24+
],
25+
layerOptimization: {
26+
cleanupPatterns: [
27+
"node_modules/aws-sdk/**",
28+
"node_modules/**/.github",
29+
"node_modules/**/.git/*",
30+
"node_modules/**/.lint",
31+
"node_modules/**/Gruntfile.js",
32+
"node_modules/**/.jshintrc",
33+
"node_modules/**/.nycrc",
34+
"node_modules/**/.nvmrc",
35+
"node_modules/**/.editorconfig",
36+
"node_modules/**/.npmignore",
37+
"node_modules/**/bower.json",
38+
"node_modules/**/.eslint*",
39+
"node_modules/**/.gitignore",
40+
"node_modules/**/README.*",
41+
"node_modules/**/LICENSE",
42+
"node_modules/**/LICENSE.md",
43+
"node_modules/**/CHANGES",
44+
"node_modules/**/HISTORY.md",
45+
"node_modules/**/CHANGES.md",
46+
"node_modules/**/CHANGELOG.md",
47+
"node_modules/**/sponsors.md",
48+
"node_modules/**/license.txt",
49+
"node_modules/**/tsconfig.json",
50+
"node_modules/**/*.test.js",
51+
"node_modules/**/*.spec.js",
52+
"node_modules/**/.travis.y*ml",
53+
"node_modules/**/yarn.lock",
54+
"node_modules/**/.package-lock.json",
55+
"node_modules/**/*.md",
56+
]
57+
}
2558
};
2659

2760
this.commands = {

src/runtimes/python.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ class PythonRuntime {
1616
compatibleRuntimes: [runtime],
1717
compatibleArchitectures: parent.compatibleArchitectures,
1818
copyBeforeInstall: [],
19-
packageExclude: [
20-
'package.json',
21-
'package-lock.json',
22-
'node_modules/**',
23-
]
19+
packagePatterns: [
20+
'!package.json',
21+
'!package-lock.json',
22+
'!node_modules/**',
23+
],
24+
layerOptimization: {
25+
cleanupPatterns: [
26+
"node_modules/**/*.pyc",
27+
"node_modules/**/*.md",
28+
]
29+
}
2430
};
2531

2632
this.commands = {

src/runtimes/ruby.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ class RubyRuntime {
2222
copyAfterInstall: [
2323
{ from: 'ruby', to: 'gems' }
2424
],
25-
packageExclude: [
26-
'node_modules/**',
27-
'package.json',
28-
'package-lock.json',
29-
'vendor/**',
30-
'.bundle'
31-
]
25+
packagePatterns: [
26+
'!node_modules/**',
27+
'!package.json',
28+
'!package-lock.json',
29+
'!vendor/**',
30+
'!.bundle'
31+
],
32+
layerOptimization: {
33+
cleanupPatterns: [
34+
"node_modules/**/*.pyc",
35+
"node_modules/**/*.md",
36+
]
37+
}
3238
};
3339

3440
this.commands = {

tests/fixtures/nodejsConfig.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,44 @@ module.exports = {
55
packageManagerExtraArgs: '',
66
libraryFolder: 'node_modules',
77
dependenciesPath: './fixtures/package.json',
8+
layerOptimization: {
9+
cleanupPatterns: [
10+
"node_modules/aws-sdk/**",
11+
"node_modules/**/.github",
12+
"node_modules/**/.git/*",
13+
"node_modules/**/.lint",
14+
"node_modules/**/Gruntfile.js",
15+
"node_modules/**/.jshintrc",
16+
"node_modules/**/.nycrc",
17+
"node_modules/**/.nvmrc",
18+
"node_modules/**/.editorconfig",
19+
"node_modules/**/.npmignore",
20+
"node_modules/**/bower.json",
21+
"node_modules/**/.eslint*",
22+
"node_modules/**/.gitignore",
23+
"node_modules/**/README.*",
24+
"node_modules/**/LICENSE",
25+
"node_modules/**/LICENSE.md",
26+
"node_modules/**/CHANGES",
27+
"node_modules/**/HISTORY.md",
28+
"node_modules/**/CHANGES.md",
29+
"node_modules/**/CHANGELOG.md",
30+
"node_modules/**/sponsors.md",
31+
"node_modules/**/license.txt",
32+
"node_modules/**/tsconfig.json",
33+
"node_modules/**/*.test.js",
34+
"node_modules/**/*.spec.js",
35+
"node_modules/**/.travis.y*ml",
36+
"node_modules/**/yarn.lock",
37+
"node_modules/**/.package-lock.json",
38+
"node_modules/**/*.md"
39+
]
40+
},
841
compatibleRuntimes: [ 'nodejs' ],
942
compatibleArchitectures: [
1043
"x86_64",
1144
"arm64"
1245
],
1346
copyBeforeInstall: [ '.npmrc', 'yarn.lock', 'package-lock.json' ],
14-
packageExclude: [ 'node_modules/**' ]
47+
packageExclude: [ '!node_modules/**' ]
1548
}

0 commit comments

Comments
 (0)