Comparing version 1.0.7 to 1.0.8
@@ -20,5 +20,6 @@ declare module launcher | ||
}; | ||
export function readYamlSync(pathOrData: string | Buffer, parent?: ParentNode): any; | ||
type Path = string; | ||
export function readYamlSync(pathOrData: Path | Buffer, parent?: ParentNode): any; | ||
export default readYamlSync; | ||
} |
@@ -35,3 +35,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
/******/ // eslint-disable-next-line no-unused-vars | ||
/******/ var hotCurrentHash = "8c4100ea24fc9c72be7e"; | ||
/******/ var hotCurrentHash = "a4ce695ee9fec434bb8e"; | ||
/******/ var hotRequestTimeout = 10000; | ||
@@ -991,2 +991,5 @@ /******/ var hotCurrentModuleData = {}; | ||
} | ||
else if (ext === "json") { | ||
return JSON.parse(fs.readFileSync(resolvedPath, "utf-8")); | ||
} | ||
else { | ||
@@ -1163,16 +1166,16 @@ return fs.readFileSync(resolvedPath, "utf-8"); | ||
} | ||
function cast(value) { | ||
switch (value) { | ||
case "undefined": | ||
return undefined; | ||
case "null": | ||
return null; | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
default: | ||
return value; | ||
} | ||
} | ||
// function cast(value: any) { | ||
// switch (value) { | ||
// case "undefined": | ||
// return undefined; | ||
// case "null": | ||
// return null; | ||
// case "true": | ||
// return true; | ||
// case "false": | ||
// return false; | ||
// default: | ||
// return value; | ||
// } | ||
// } | ||
function print(node, basePath, parentName) { | ||
@@ -1191,3 +1194,3 @@ if (!node) { | ||
var combined = prefix + result.value + suffix; | ||
return cast(combined); | ||
return combined; | ||
} | ||
@@ -1209,19 +1212,20 @@ else { | ||
case NodeKind.FUNCTION: { | ||
var func = functions[node.value.name]; | ||
var _b = node.value, name_3 = _b.name, _arguments = _b.arguments; | ||
var func = functions[name_3]; | ||
if (func) { | ||
var result = func.apply(void 0, node.value.arguments.concat([basePath, parentName])); | ||
var _b = node.scope, start = _b.start, end = _b.end; | ||
var result = func.apply(void 0, _arguments.concat([basePath, parentName])); | ||
var _c = node.scope, start = _c.start, end = _c.end; | ||
return new Result(result, start, end); | ||
} | ||
throw new Error(YamlError.UnknonwReference(name)); | ||
throw new Error(YamlError.UnknonwReference(name_3)); | ||
} | ||
case NodeKind.VARIABLE: { | ||
var name_3 = node.value.name; | ||
var func = functions[name_3]; | ||
var _d = node.value, name_4 = _d.name, _arguments = _d.arguments; | ||
var func = functions[name_4]; | ||
if (func) { | ||
var result = func.apply(void 0, node.value.arguments); | ||
var _c = node.scope, start = _c.start, end = _c.end; | ||
var result = func.apply(void 0, _arguments); | ||
var _e = node.scope, start = _e.start, end = _e.end; | ||
return new Result(result, start, end); | ||
} | ||
throw new Error(YamlError.UnknonwReference(name_3)); | ||
throw new Error(YamlError.UnknonwReference(name_4)); | ||
} | ||
@@ -1256,10 +1260,6 @@ } | ||
}); | ||
return root; | ||
} | ||
else if (typeof content === "string") { | ||
return print(parseToken(content), basePath, ""); | ||
} | ||
else { | ||
return content; | ||
} | ||
return root; | ||
// Convert all non-objects to string | ||
return print(parseToken(content.toString()), basePath, ""); | ||
} | ||
@@ -1296,22 +1296,17 @@ function compile(_a) { | ||
var _a; | ||
try { | ||
var data = void 0, basePath = "./"; | ||
if (pathOrData instanceof Buffer) { | ||
data = pathOrData; | ||
} | ||
else if (typeof pathOrData === "string") { | ||
basePath = pathOrData.substring(0, pathOrData.lastIndexOf("/")); | ||
data = fs.readFileSync(pathOrData, "utf8"); | ||
} | ||
var doc = yaml.safeLoad(data); | ||
var globalObj = doc; | ||
if (parent) { | ||
globalObj = tslib__WEBPACK_IMPORTED_MODULE_0__["__assign"]({}, parent.self, (_a = {}, _a[parent.name] = doc, _a)); | ||
} | ||
var compiledDoc = Object(_sls_yaml_compiler__WEBPACK_IMPORTED_MODULE_1__["compile"])({ doc: doc, globalObj: globalObj, basePath: basePath }); | ||
return compiledDoc; | ||
var data, basePath = "./"; | ||
if (typeof pathOrData === "string") { | ||
basePath = pathOrData.substring(0, pathOrData.lastIndexOf("/")); | ||
data = fs.readFileSync(pathOrData, "utf8"); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
if (pathOrData instanceof Buffer) { | ||
data = pathOrData.toString(); | ||
} | ||
var doc = yaml.safeLoad(data); | ||
var globalObj = doc; | ||
if (parent) { | ||
globalObj = tslib__WEBPACK_IMPORTED_MODULE_0__["__assign"]({}, parent.self, (_a = {}, _a[parent.name] = doc, _a)); | ||
} | ||
var compiledDoc = Object(_sls_yaml_compiler__WEBPACK_IMPORTED_MODULE_1__["compile"])({ doc: doc, globalObj: globalObj, basePath: basePath }); | ||
return compiledDoc; | ||
} | ||
@@ -1318,0 +1313,0 @@ /* harmony default export */ __webpack_exports__["default"] = (readYamlSync); |
@@ -9,3 +9,3 @@ { | ||
"global": { | ||
"branches": 50, | ||
"branches": 80, | ||
"functions": 100, | ||
@@ -12,0 +12,0 @@ "lines": 100, |
{ | ||
"name": "sls-yaml", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Serverless framework yaml extension parser", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.js", | ||
"precommit": "lint-staged", | ||
"test": "jest --config=jest.json" | ||
"test": "NODE_ENV=test jest --ci --config=./jest.json --coverage --reporters=default --reporters=jest-junit" | ||
}, | ||
@@ -40,2 +40,3 @@ "lint-staged": { | ||
"jest-cli": "24.8.0", | ||
"jest-junit": "6.4.0", | ||
"ts-jest": "24.0.2", | ||
@@ -42,0 +43,0 @@ "ts-loader": "^6.0.4", |
## Serverless YAML extension parser | ||
[![CircleCI](https://circleci.com/gh/01alchemist/sls-yaml/tree/master.svg?style=svg)](https://circleci.com/gh/01alchemist/sls-yaml/tree/master) | ||
[![CircleCI](https://circleci.com/gh/01alchemist/sls-yaml/tree/master.svg?style=svg)](https://circleci.com/gh/01alchemist/sls-yaml/tree/master) [![Maintainability](https://api.codeclimate.com/v1/badges/d3b19c4c45ebf451faf3/maintainability)](https://codeclimate.com/github/01alchemist/sls-yaml/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/d3b19c4c45ebf451faf3/test_coverage)](https://codeclimate.com/github/01alchemist/sls-yaml/test_coverage) | ||
@@ -4,0 +4,0 @@ This tiny library will parse YAML extensions used in serverless framework. |
@@ -28,2 +28,4 @@ const fs = require("fs"); | ||
return ymlObj; | ||
} else if (ext === "json") { | ||
return JSON.parse(fs.readFileSync(resolvedPath, "utf-8")); | ||
} else { | ||
@@ -202,16 +204,16 @@ return fs.readFileSync(resolvedPath, "utf-8"); | ||
function cast(value: any) { | ||
switch (value) { | ||
case "undefined": | ||
return undefined; | ||
case "null": | ||
return null; | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
default: | ||
return value; | ||
} | ||
} | ||
// function cast(value: any) { | ||
// switch (value) { | ||
// case "undefined": | ||
// return undefined; | ||
// case "null": | ||
// return null; | ||
// case "true": | ||
// return true; | ||
// case "false": | ||
// return false; | ||
// default: | ||
// return value; | ||
// } | ||
// } | ||
@@ -232,3 +234,3 @@ function print(node: Node | null, basePath: string, parentName: string): any { | ||
const combined = prefix + result.value + suffix; | ||
return cast(combined); | ||
return combined; | ||
} else { | ||
@@ -248,5 +250,6 @@ return result.value; | ||
case NodeKind.FUNCTION: { | ||
const func = functions[node.value.name]; | ||
const { name, arguments: _arguments } = node.value; | ||
const func = functions[name]; | ||
if (func) { | ||
const result = func(...node.value.arguments, basePath, parentName); | ||
const result = func(..._arguments, basePath, parentName); | ||
const { start, end } = node.scope; | ||
@@ -258,6 +261,6 @@ return new Result(result, start, end); | ||
case NodeKind.VARIABLE: { | ||
const name = node.value.name; | ||
const { name, arguments: _arguments } = node.value; | ||
const func = functions[name]; | ||
if (func) { | ||
const result = func(...node.value.arguments); | ||
const result = func(..._arguments); | ||
const { start, end } = node.scope; | ||
@@ -294,8 +297,6 @@ return new Result(result, start, end); | ||
}); | ||
} else if (typeof content === "string") { | ||
return print(parseToken(content), basePath, ""); | ||
} else { | ||
return content; | ||
return root; | ||
} | ||
return root; | ||
// Convert all non-objects to string | ||
return print(parseToken(content.toString()), basePath, ""); | ||
} | ||
@@ -302,0 +303,0 @@ |
@@ -11,4 +11,18 @@ import yaml from "./sls-yaml"; | ||
}); | ||
describe("YAML extended test suite", () => { | ||
describe("When passing a file reference", () => { | ||
describe("When passing yaml file path", () => { | ||
it("Should load yaml from path", () => { | ||
const doc = yaml("src/__mocks__/file.yml"); | ||
expect(doc).toEqual({ key: "value" }); | ||
}); | ||
}); | ||
describe("When passing yaml file buffer", () => { | ||
it("Should load yaml from path", () => { | ||
const doc = yaml("src/__mocks__/file.yml"); | ||
expect(doc).toEqual({ key: "value" }); | ||
}); | ||
}); | ||
describe("When passing a yaml file reference", () => { | ||
it("Should replace value with file content", () => { | ||
@@ -20,3 +34,4 @@ const content = Buffer.from("config: ${file(src/__mocks__/file.yml)}"); | ||
}); | ||
describe("When passing a file reference without key", () => { | ||
describe("When passing a yaml file reference without key", () => { | ||
it("Should replace value with file content", () => { | ||
@@ -28,2 +43,19 @@ const content = Buffer.from("${file(src/__mocks__/file.yml)}"); | ||
}); | ||
describe("When passing a json file reference", () => { | ||
it("Should replace value with file's content", () => { | ||
const content = Buffer.from("json: ${file(src/__mocks__/file.json)}"); | ||
const doc = yaml(content); | ||
expect(doc).toEqual({ json: { name: "Json", value: 100 } }); | ||
}); | ||
}); | ||
describe("When passing a text file reference", () => { | ||
it("Should replace value with file's content", () => { | ||
const content = Buffer.from("text: ${file(src/__mocks__/file.txt)}"); | ||
const doc = yaml(content); | ||
expect(doc).toEqual({ text: "This is external text content\n" }); | ||
}); | ||
}); | ||
describe("When passing a env reference", () => { | ||
@@ -66,6 +98,5 @@ it("Should replace env var with it's value", () => { | ||
it("Should replace self var with it's value", () => { | ||
const content = Buffer.from(` | ||
version: 1 | ||
config: version-\${self:version} | ||
`); | ||
const content = Buffer.from( | ||
["version: 1", "config: version-${self:version}"].join("\n") | ||
); | ||
const doc = yaml(content); | ||
@@ -77,6 +108,5 @@ expect(doc).toEqual({ version: 1, config: "version-1" }); | ||
it("Should return prefix plus replace self var with null", () => { | ||
const content = Buffer.from(` | ||
version: null | ||
config: version-\${self:version} | ||
`); | ||
const content = Buffer.from( | ||
["version: null", "config: version-${self:version}"].join("\n") | ||
); | ||
const doc = yaml(content); | ||
@@ -86,6 +116,5 @@ expect(doc).toEqual({ version: null, config: "version-null" }); | ||
it("Should replace self var with null", () => { | ||
const content = Buffer.from(` | ||
version: null | ||
config: \${self:version} | ||
`); | ||
const content = Buffer.from( | ||
["version: null", "config: ${self:version}"].join("\n") | ||
); | ||
const doc = yaml(content); | ||
@@ -128,2 +157,3 @@ expect(doc).toEqual({ version: null, config: null }); | ||
}); | ||
describe("When passing a file reference with dynamic self references within a file reference", () => { | ||
@@ -141,3 +171,86 @@ it("Should replace value with file content", () => { | ||
}); | ||
describe("When passing boolean values", () => { | ||
it("Should cast boolean:true values properly", () => { | ||
const content = Buffer.from( | ||
[ | ||
"is-enabled: true", | ||
"isEnabled: ${self:is-enabled}", | ||
"title: Feature enabled=${self:is-enabled}" | ||
].join("\n") | ||
); | ||
const expected = { | ||
"is-enabled": true, | ||
isEnabled: true, | ||
title: "Feature enabled=true" | ||
}; | ||
const doc = yaml(content); | ||
expect(doc).toEqual(expected); | ||
}); | ||
it("Should cast boolean:false values properly", () => { | ||
const content = Buffer.from( | ||
[ | ||
"is-enabled: false", | ||
"isEnabled: ${self:is-enabled}", | ||
"title: Feature enabled=${self:is-enabled}" | ||
].join("\n") | ||
); | ||
const expected = { | ||
"is-enabled": false, | ||
isEnabled: false, | ||
title: "Feature enabled=false" | ||
}; | ||
const doc = yaml(content); | ||
expect(doc).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
describe("YAML extended exception test suite", () => { | ||
describe("When passing an unknown function reference", () => { | ||
it("Should throw unknonw reference error", () => { | ||
const content = Buffer.from( | ||
"config: ${unknown(src/__mocks__/file.yml)}" | ||
); | ||
expect(() => { | ||
yaml(content); | ||
}).toThrowError( | ||
`Unknonw reference error, "unknown" is not a known reference name` | ||
); | ||
}); | ||
}); | ||
describe("When passing integer values", () => { | ||
it("Should cast integer values properly", () => { | ||
const content = Buffer.from( | ||
[ | ||
"replicas: 3", | ||
"numReplicas: ${self:replicas}", | ||
"title: No of replicas=${self:replicas}" | ||
].join("\n") | ||
); | ||
const expected = { | ||
replicas: 3, | ||
numReplicas: 3, | ||
title: "No of replicas=3" | ||
}; | ||
const doc = yaml(content); | ||
expect(doc).toEqual(expected); | ||
}); | ||
}); | ||
describe("When passing an unknown variable reference", () => { | ||
it("Should throw unknonw reference error", () => { | ||
const content = Buffer.from("config: ${unknown:my.var}"); | ||
expect(() => { | ||
yaml(content); | ||
}).toThrowError( | ||
`Unknonw reference error, "unknown" is not a known reference name` | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -10,28 +10,29 @@ import { compile } from "./sls-yaml-compiler"; | ||
export function readYamlSync(pathOrData: string | Buffer, parent?: ParentNode) { | ||
try { | ||
let data, | ||
basePath = "./"; | ||
if (pathOrData instanceof Buffer) { | ||
data = pathOrData; | ||
} else if (typeof pathOrData === "string") { | ||
basePath = pathOrData.substring(0, pathOrData.lastIndexOf("/")); | ||
data = fs.readFileSync(pathOrData, "utf8"); | ||
} | ||
const doc = yaml.safeLoad(data); | ||
let globalObj = doc; | ||
if (parent) { | ||
globalObj = { | ||
...parent.self, | ||
[parent.name]: doc | ||
}; | ||
} | ||
type Path = string; | ||
const compiledDoc = compile({ doc, globalObj, basePath }); | ||
return compiledDoc; | ||
} catch (e) { | ||
console.log(e); | ||
export function readYamlSync(pathOrData: Path | Buffer, parent?: ParentNode) { | ||
let data, | ||
basePath = "./"; | ||
if (typeof pathOrData === "string") { | ||
basePath = pathOrData.substring(0, pathOrData.lastIndexOf("/")); | ||
data = fs.readFileSync(pathOrData, "utf8"); | ||
} | ||
if (pathOrData instanceof Buffer) { | ||
data = pathOrData.toString(); | ||
} | ||
const doc = yaml.safeLoad(data); | ||
let globalObj = doc; | ||
if (parent) { | ||
globalObj = { | ||
...parent.self, | ||
[parent.name]: doc | ||
}; | ||
} | ||
const compiledDoc = compile({ doc, globalObj, basePath }); | ||
return compiledDoc; | ||
} | ||
export default readYamlSync; |
Sorry, the diff of this file is not supported yet
134880
16
2055
16