only use json-cycles when opt-in, for state serialization

This commit is contained in:
Doug Moscrop 2018-06-06 14:26:43 -04:00
parent 543bed3cbd
commit 8d9f4a296b
7 changed files with 21 additions and 13 deletions

View File

@ -39,15 +39,15 @@ class Utils {
return fse.mkdirsSync(path.dirname(filePath));
}
writeFileSync(filePath, contents) {
return writeFileSync(filePath, contents);
writeFileSync(filePath, contents, cycles) {
return writeFileSync(filePath, contents, cycles);
}
writeFile(filePath, contents) {
writeFile(filePath, contents, cycles) {
const that = this;
return new BbPromise((resolve, reject) => {
try {
that.writeFileSync(filePath, contents);
that.writeFileSync(filePath, contents, cycles);
} catch (e) {
reject(e);
}

View File

@ -36,7 +36,7 @@ module.exports = {
},
};
this.serverless.utils.writeFileSync(serviceStateFilePath, state);
this.serverless.utils.writeFileSync(serviceStateFilePath, state, true);
return BbPromise.resolve();
},

View File

@ -63,7 +63,7 @@ describe('#saveServiceState()', () => {
};
expect(getServiceStateFileNameStub.calledOnce).to.equal(true);
expect(writeFileSyncStub.calledWithExactly(filePath, expectedStateFileContent))
expect(writeFileSyncStub.calledWithExactly(filePath, expectedStateFileContent, true))
.to.equal(true);
});
});
@ -97,7 +97,7 @@ describe('#saveServiceState()', () => {
};
expect(getServiceStateFileNameStub.calledOnce).to.equal(true);
expect(writeFileSyncStub.calledWithExactly(filePath, expectedStateFileContent))
expect(writeFileSyncStub.calledWithExactly(filePath, expectedStateFileContent, true))
.to.equal(true);
});
});

View File

@ -5,13 +5,17 @@ const path = require('path');
const jc = require('json-cycle');
const YAML = require('js-yaml');
function writeFile(filePath, conts) {
function writeFile(filePath, conts, cycles) {
let contents = conts || '';
return fse.mkdirsAsync(path.dirname(filePath))
.then(() => {
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = jc.stringify(contents, null, 2);
if (cycles) {
contents = jc.stringify(contents, null, 2);
} else {
contents = JSON.stringify(contents, null, 2);
}
}
const yamlFileExists = (filePath.indexOf('.yaml') !== -1);

View File

@ -49,7 +49,7 @@ describe('#writeFile()', function () {
bar.foo = bar;
const expected = '{\n "foo": {\n "$ref": "$"\n }\n}';
return writeFile(tmpFilePath, bar)
return writeFile(tmpFilePath, bar, true)
.then(() =>
expect(fse.readFileAsync(tmpFilePath, 'utf8')).to.eventually.equal(expected)
);

View File

@ -5,13 +5,17 @@ const path = require('path');
const jc = require('json-cycle');
const YAML = require('js-yaml');
function writeFileSync(filePath, conts) {
function writeFileSync(filePath, conts, cycles) {
let contents = conts || '';
fse.mkdirsSync(path.dirname(filePath));
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = jc.stringify(contents, null, 2);
if (cycles) {
contents = jc.stringify(contents, null, 2);
} else {
contents = JSON.stringify(contents, null, 2);
}
}
const yamlFileExists = (filePath.indexOf('.yaml') !== -1);

View File

@ -53,7 +53,7 @@ describe('#writeFileSync()', () => {
bar.foo = bar;
const expected = '{\n "foo": {\n "$ref": "$"\n }\n}';
writeFileSync(tmpFilePath, bar);
writeFileSync(tmpFilePath, bar, true);
return fse.readFileAsync(tmpFilePath, 'utf8').then((contents) => {
expect(contents).to.equal(expected);