Skip to content

Commit 3617fd8

Browse files
authored
Merge pull request nicka#9 from nicka/release/1.2.0
Release/1.2.0
2 parents 33bb1cc + 5c3acd2 commit 3617fd8

File tree

5 files changed

+332
-25
lines changed

5 files changed

+332
-25
lines changed

lib/__snapshots__/index.test.js.snap

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ exports[`serverless-plugin-write-env-vars diff successfully triggers diff runs d
99
`;
1010
1111
exports[`serverless-plugin-write-env-vars diff successfully triggers diff runs diff with custom localTemplate template 1`] = `
12-
"1c1
13-
< {\"foo\":\"custom\"}
14-
\\ No newline at end of file
15-
---
16-
> {\"foo\":\"foo\"}
17-
\\ No newline at end of file
12+
" {
13+
- foo: \"custom\"
14+
+ foo: \"foo\"
15+
}
1816
"
1917
`;
2018
2119
exports[`serverless-plugin-write-env-vars diff successfully triggers diff runs diff with defaults 1`] = `
22-
"1c1
23-
< {\"foo\":\"bar\"}
24-
\\ No newline at end of file
25-
---
26-
> {\"foo\":\"foo\"}
27-
\\ No newline at end of file
20+
" {
21+
- foo: \"bar\"
22+
+ foo: \"foo\"
23+
}
24+
"
25+
`;
26+
27+
exports[`serverless-plugin-write-env-vars diff successfully triggers diff runs diff without changes 1`] = `
28+
" undefined
2829
"
2930
`;
3031

lib/index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
const AWS = require('aws-sdk');
4-
const fs = require('fs-promise');
4+
const diff = require('json-diff').diffString;
55
const exec = require('child-process-promise').exec;
6+
const fs = require('fs-promise');
67
const Q = require('q');
78

89
class ServerlessPlugin {
@@ -45,8 +46,7 @@ class ServerlessPlugin {
4546
this.options.region = this.options.region
4647
|| (this.serverless.service.defaults && this.serverless.service.defaults.region)
4748
|| 'us-east-1';
48-
this.options.diffTool = this.options.diffTool
49-
|| 'diff';
49+
this.options.diffTool = this.options.diffTool;
5050
this.options.localTemplate = this.options.localTemplate
5151
|| '.serverless/cloudformation-template-update-stack.json';
5252
this.options.orgTemplate = this.options.localTemplate.replace('.json', '.org.json');
@@ -92,13 +92,27 @@ class ServerlessPlugin {
9292

9393
fs.stat(localTemplate)
9494
.then(() => {
95-
exec(`${diffTool} ${orgTemplate} ${localTemplate} || true`)
96-
.then((result) => {
97-
const diffData = result.stdout;
98-
console.log(diffData);
99-
deferred.resolve(diffData);
100-
})
101-
.catch(execErr => deferred.reject(execErr.message));
95+
if (typeof diffTool === 'undefined') {
96+
const orgTemplateJson = JSON.parse(fs.readFileSync(orgTemplate, 'utf8'));
97+
const localTemplateJson = JSON.parse(fs.readFileSync(localTemplate, 'utf8'));
98+
const differences = diff(orgTemplateJson, localTemplateJson) || {};
99+
100+
if (differences.trim() === 'undefined') {
101+
console.log('Resource templates are equal');
102+
} else {
103+
console.log(differences);
104+
}
105+
106+
deferred.resolve(differences);
107+
} else {
108+
exec(`${diffTool} ${orgTemplate} ${localTemplate} || true`)
109+
.then((result) => {
110+
const diffData = result.stdout;
111+
console.log(diffData);
112+
deferred.resolve(diffData);
113+
})
114+
.catch(execErr => deferred.reject(execErr.message));
115+
}
102116
})
103117
.catch((err) => {
104118
if (err.code === 'ENOENT') {

lib/index.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('serverless-plugin-write-env-vars', () => {
3232

3333
expect(plugin.options.stage).toBe('foo');
3434
expect(plugin.options.region).toBe('eu-west-1');
35-
expect(plugin.options.diffTool).toBe('diff');
35+
expect(plugin.options.diffTool).toBe(undefined);
3636
expect(plugin.options.localTemplate).toBe(`${templatePrefix}.json`);
3737
expect(plugin.options.orgTemplate).toBe(`${templatePrefix}.org.json`);
3838
});
@@ -125,6 +125,25 @@ describe('serverless-plugin-write-env-vars', () => {
125125
});
126126
});
127127

128+
describe('successfully triggers diff', () => {
129+
const customTemplate = `${templatePrefix}-foo.json`;
130+
const customOrgTemplate = `${templatePrefix}-foo.org.json`;
131+
132+
beforeEach(() =>
133+
fs.writeFile(customTemplate, '{"foo":"foo"}')
134+
.then(() => fs.writeFile(customOrgTemplate, '{"foo":"foo"}'))
135+
);
136+
137+
it('runs diff without changes', () => {
138+
const plugin = new Plugin(slsDefaults, {
139+
localTemplate: customTemplate,
140+
});
141+
142+
return plugin.diff()
143+
.then(data => expect(data).toMatchSnapshot());
144+
});
145+
});
146+
128147
describe('unsuccessfully triggers diff', () => {
129148
beforeEach(() => fs.unlink(exampleTemplate));
130149

npm-shrinkwrap.json

Lines changed: 271 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)