@ghii/yaml-loader
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,41 +0,60 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
import yamlLoader from '../yaml-loader'; | ||
import * as fs from 'fs'; | ||
import path from 'path'; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const yaml_loader_1 = __importDefault(require("../yaml-loader")); | ||
const fs = __importStar(require("fs")); | ||
const path_1 = __importDefault(require("path")); | ||
describe('Ghii Yaml Loader', () => { | ||
it('export a function', () => { | ||
expect(typeof yamlLoader).toBe('function'); | ||
expect(typeof yaml_loader_1.default).toBe('function'); | ||
}); | ||
describe('to create a loader', () => { | ||
it('create a file loader from yaml file - check if is a function', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const yamlFileLoader = yamlLoader({ throwOnError: false }, __dirname, 'test.yaml'); | ||
yield expect(typeof yamlFileLoader).toBe('function'); | ||
})); | ||
it('attempt to read not existent file throw Error', () => __awaiter(void 0, void 0, void 0, function* () { | ||
it('create a file loader from yaml file - check if is a function', async () => { | ||
const yamlFileLoader = (0, yaml_loader_1.default)({ throwOnError: false }, __dirname, 'test.yaml'); | ||
await expect(typeof yamlFileLoader).toBe('function'); | ||
}); | ||
it('attempt to read not existent file throw Error', async () => { | ||
expect(() => { | ||
yamlLoader({ throwOnError: true }, __dirname, 'test_not_exist.yaml'); | ||
(0, yaml_loader_1.default)({ throwOnError: true }, __dirname, 'test_not_exist.yaml'); | ||
}).toThrow(); | ||
})); | ||
it('attempt to read not existent file with no throw Error options', () => __awaiter(void 0, void 0, void 0, function* () { | ||
}); | ||
it('attempt to read not existent file with no throw Error options', async () => { | ||
expect(() => { | ||
yamlLoader({ throwOnError: false }, __dirname, 'test_not_exist.yaml'); | ||
(0, yaml_loader_1.default)({ throwOnError: false }, __dirname, 'test_not_exist.yaml'); | ||
}).not.toThrow(); | ||
})); | ||
it('attempt to read a folder throw Error', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect(yamlLoader({ throwOnError: true }, __dirname)).rejects.toBeInstanceOf(Error); | ||
})); | ||
it('attempt to read a folder with no throw Error', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect(yield yamlLoader({ throwOnError: false }, __dirname)()).toStrictEqual({}); | ||
})); | ||
it('create a file loader from yaml file', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const content = yield yamlLoader({ throwOnError: true }, __dirname, 'test.yaml')(); | ||
yield expect(content).toStrictEqual({ | ||
}); | ||
it('attempt to read a folder throw Error', async () => { | ||
expect((0, yaml_loader_1.default)({ throwOnError: true }, __dirname)).rejects.toBeInstanceOf(Error); | ||
}); | ||
it('attempt to read a folder with no throw Error', async () => { | ||
expect(await (0, yaml_loader_1.default)({ throwOnError: false }, __dirname)()).toStrictEqual({}); | ||
}); | ||
it('create a file loader from yaml file', async () => { | ||
const content = await (0, yaml_loader_1.default)({ throwOnError: true }, __dirname, 'test.yaml')(); | ||
await expect(content).toStrictEqual({ | ||
foo: { | ||
@@ -45,25 +64,25 @@ ciao: 'mondo', | ||
}); | ||
})); | ||
it('create a file loader from yaml file removed after init', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const src = path.join(__dirname, '../__test__/test.yaml'); | ||
const copy = path.join(__dirname, '../__test__/test-temp-copy.yaml'); | ||
}); | ||
it('create a file loader from yaml file removed after init', async () => { | ||
const src = path_1.default.join(__dirname, '../__test__/test.yaml'); | ||
const copy = path_1.default.join(__dirname, '../__test__/test-temp-copy.yaml'); | ||
if (fs.existsSync(copy)) | ||
fs.rmSync(copy); | ||
fs.copyFileSync(src, copy); | ||
const fileLoader = yield yamlLoader({ throwOnError: true }, __dirname, 'test-temp-copy.yaml'); | ||
const fileLoader = await (0, yaml_loader_1.default)({ throwOnError: true }, __dirname, 'test-temp-copy.yaml'); | ||
fs.rmSync(copy); | ||
yield expect(fileLoader()).rejects.toBeInstanceOf(Error); | ||
})); | ||
it('create a file loader from yaml file removed after init with no throw', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const src = path.join(__dirname, '../__test__/test.yaml'); | ||
const copy = path.join(__dirname, '../__test__/test-temp-copy.yaml'); | ||
await expect(fileLoader()).rejects.toBeInstanceOf(Error); | ||
}); | ||
it('create a file loader from yaml file removed after init with no throw', async () => { | ||
const src = path_1.default.join(__dirname, '../__test__/test.yaml'); | ||
const copy = path_1.default.join(__dirname, '../__test__/test-temp-copy.yaml'); | ||
if (fs.existsSync(copy)) | ||
fs.rmSync(copy); | ||
fs.copyFileSync(src, copy); | ||
const fileLoader = yield yamlLoader({ throwOnError: false }, __dirname, 'test-temp-copy.yaml'); | ||
const fileLoader = await (0, yaml_loader_1.default)({ throwOnError: false }, __dirname, 'test-temp-copy.yaml'); | ||
fs.rmSync(copy); | ||
yield expect(fileLoader()).resolves.toStrictEqual({}); | ||
})); | ||
await expect(fileLoader()).resolves.toStrictEqual({}); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=yaml-loader.test.js.map |
@@ -1,18 +0,38 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
import yaml from 'js-yaml'; | ||
import * as fs from 'fs'; | ||
import { promisify } from 'util'; | ||
import path from 'path'; | ||
const stat = promisify(fs.stat); | ||
const readFile = promisify(fs.readFile); | ||
export default function yamlLoader({ throwOnError, logger = (err, message) => console.log(message, err), }, ...filePathToken) { | ||
const sourcePath = path.join(...filePathToken); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.yamlLoader = void 0; | ||
const js_yaml_1 = __importDefault(require("js-yaml")); | ||
const fs = __importStar(require("fs")); | ||
const util_1 = require("util"); | ||
const path_1 = __importDefault(require("path")); | ||
const stat = (0, util_1.promisify)(fs.stat); | ||
const readFile = (0, util_1.promisify)(fs.readFile); | ||
function yamlLoader({ throwOnError, logger = (err, message) => console.log(message, err), }, ...filePathToken) { | ||
const sourcePath = path_1.default.join(...filePathToken); | ||
if (!fs.existsSync(sourcePath)) { | ||
@@ -23,28 +43,27 @@ logger({ msg: 'not exist' }, `${sourcePath} 404`); | ||
} | ||
return function yamlFileLoader() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const fstat = yield stat(sourcePath); | ||
if (fstat.isFile()) { | ||
const yamlContent = yield readFile(sourcePath, { encoding: 'utf8' }); | ||
return yaml.load(yamlContent); | ||
} | ||
const msg = `Source ${sourcePath} is not a file`; | ||
logger({ msg }, msg); | ||
if (throwOnError) { | ||
throw new Error(msg); | ||
} | ||
return {}; | ||
return async function yamlFileLoader() { | ||
try { | ||
const fstat = await stat(sourcePath); | ||
if (fstat.isFile()) { | ||
const yamlContent = await readFile(sourcePath, { encoding: 'utf8' }); | ||
return js_yaml_1.default.load(yamlContent); | ||
} | ||
catch (err) { | ||
const msg = `FILE DELETED OR A DIRECTORY-> ${sourcePath} 404: ${err instanceof Error ? err.message : ''}`; | ||
logger(err, msg); | ||
if (throwOnError) | ||
throw new Error(msg); | ||
return {}; | ||
const msg = `Source ${sourcePath} is not a file`; | ||
logger({ msg }, msg); | ||
if (throwOnError) { | ||
throw new Error(msg); | ||
} | ||
}); | ||
return {}; | ||
} | ||
catch (err) { | ||
const msg = `FILE DELETED OR A DIRECTORY-> ${sourcePath} 404: ${err instanceof Error ? err.message : ''}`; | ||
logger(err, msg); | ||
if (throwOnError) | ||
throw new Error(msg); | ||
return {}; | ||
} | ||
}; | ||
} | ||
export { yamlLoader }; | ||
exports.default = yamlLoader; | ||
exports.yamlLoader = yamlLoader; | ||
//# sourceMappingURL=yaml-loader.js.map |
{ | ||
"name": "@ghii/yaml-loader", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A Funny yaml loader for ghii configuration manager ", | ||
@@ -5,0 +5,0 @@ "maintainers": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
15637
160