concise-flow
Advanced tools
Comparing version 0.3.0 to 0.4.2
119
lib/index.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,66 +6,30 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.output = undefined; | ||
exports.output = void 0; | ||
var _regenerator = require('babel-runtime/regenerator'); | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _regenerator2 = _interopRequireDefault(_regenerator); | ||
var _lodash = _interopRequireDefault(require("lodash.upperfirst")); | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _lodash = require('lodash.upperfirst'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
/* eslint-disable prefer-template */ | ||
// ==================================== | ||
// Main | ||
// ==================================== | ||
const output = async (schema, options, utils) => { | ||
const raw = writeTypes(utils.preprocessedSchema); | ||
if (options.file) _fs.default.writeFileSync(options.file, raw, 'utf8'); | ||
return raw; | ||
}; // ==================================== | ||
// Flow writer | ||
// ==================================== | ||
/* -- | ||
Output-only. | ||
exports.output = output; | ||
Output options: | ||
* `file?` (`string`): if specified, output will be written to the specified path | ||
-- */ | ||
var output = function () { | ||
var _ref = _asyncToGenerator(_regenerator2.default.mark(function _callee(schema, options, utils) { | ||
var raw; | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
raw = writeTypes(utils.preprocessedSchema); | ||
if (options.file) _fs2.default.writeFileSync(options.file, raw, 'utf8'); | ||
return _context.abrupt('return', raw); | ||
case 3: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined); | ||
})); | ||
return function output(_x, _x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
// ==================================== | ||
// Flow writer | ||
// ==================================== | ||
var writeTypes = function writeTypes(_ref2) { | ||
var models = _ref2.models; | ||
var out = ''; | ||
Object.keys(models).forEach(function (modelName) { | ||
const writeTypes = ({ | ||
models | ||
}) => { | ||
let out = ''; | ||
Object.keys(models).forEach(modelName => { | ||
out += writeType(models, modelName); | ||
@@ -76,33 +40,31 @@ }); | ||
var writeType = function writeType(models, modelName) { | ||
var _models$modelName = models[modelName], | ||
description = _models$modelName.description, | ||
_models$modelName$fie = _models$modelName.fields, | ||
fields = _models$modelName$fie === undefined ? {} : _models$modelName$fie, | ||
_models$modelName$rel = _models$modelName.relations, | ||
relations = _models$modelName$rel === undefined ? {} : _models$modelName$rel; | ||
var upperModelName = (0, _lodash2.default)(modelName); | ||
var allSpecs = []; | ||
Object.keys(fields).forEach(function (fieldName) { | ||
const writeType = (models, modelName) => { | ||
const { | ||
description, | ||
fields = {}, | ||
relations = {} | ||
} = models[modelName]; | ||
const upperModelName = (0, _lodash.default)(modelName); | ||
const allSpecs = []; | ||
Object.keys(fields).forEach(fieldName => { | ||
allSpecs.push(writeField(fieldName, fields[fieldName])); | ||
}); | ||
Object.keys(relations).forEach(function (fieldName) { | ||
var relation = relations[fieldName]; | ||
Object.keys(relations).forEach(fieldName => { | ||
const relation = relations[fieldName]; | ||
if (relation.isInverse) return; | ||
allSpecs.push(writeField(relation.fkName, relation)); | ||
}); | ||
var contents = allSpecs.length ? '\n ' + allSpecs.join('\n ') + '\n' : ''; | ||
return '' + ('// ' + upperModelName + '\n') + (description ? '// ' + description + '\n' : '') + ('type ' + upperModelName + ' = {' + contents + '};\n\n'); | ||
const contents = allSpecs.length ? `\n ${allSpecs.join('\n ')}\n` : ''; | ||
return '' + `// ${upperModelName}\n` + (description ? `// ${description}\n` : '') + `type ${upperModelName} = {${contents}};\n\n`; | ||
}; | ||
var writeField = function writeField(name, specs) { | ||
var isRequired = specs.isRequired ? '' : '?'; | ||
var typeStr = writeFieldType(specs.type); | ||
if (specs.isPlural) typeStr = 'Array<' + typeStr + '>'; | ||
var comment = specs.description ? ' // ' + specs.description : ''; | ||
return '' + name + isRequired + ': ' + typeStr + ',' + comment; | ||
const writeField = (name, specs) => { | ||
const isRequired = specs.isRequired ? '' : '?'; | ||
let typeStr = writeFieldType(specs.type); | ||
if (specs.isPlural) typeStr = `Array<${typeStr}>`; | ||
const comment = specs.description ? ` // ${specs.description}` : ''; | ||
return `${name}${isRequired}: ${typeStr},${comment}`; | ||
}; | ||
var writeFieldType = function writeFieldType(type) { | ||
const writeFieldType = type => { | ||
if (type === 'uuid') return 'string'; | ||
@@ -112,7 +74,4 @@ if (type === 'json') return 'any'; | ||
return type; | ||
}; | ||
// ==================================== | ||
}; // ==================================== | ||
// Public | ||
// ==================================== | ||
exports.output = output; | ||
// ==================================== |
{ | ||
"name": "concise-flow", | ||
"version": "0.3.0", | ||
"version": "0.4.2", | ||
"author": "Guillermo Grau Panea", | ||
@@ -17,6 +17,6 @@ "license": "MIT", | ||
"lodash.upperfirst": "4.3.1", | ||
"concise-types": "*" | ||
"concise-types": "^0.4.2" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^1.12.1" | ||
"prettier": "^2.2.1" | ||
}, | ||
@@ -23,0 +23,0 @@ "description": "A tool belt for concise schemas", |
@@ -37,3 +37,3 @@ // @flow | ||
let out = ''; | ||
Object.keys(models).forEach(modelName => { | ||
Object.keys(models).forEach((modelName) => { | ||
out += writeType(models, modelName); | ||
@@ -48,6 +48,6 @@ }); | ||
const allSpecs = []; | ||
Object.keys(fields).forEach(fieldName => { | ||
Object.keys(fields).forEach((fieldName) => { | ||
allSpecs.push(writeField(fieldName, fields[fieldName])); | ||
}); | ||
Object.keys(relations).forEach(fieldName => { | ||
Object.keys(relations).forEach((fieldName) => { | ||
const relation = relations[fieldName]; | ||
@@ -74,3 +74,3 @@ if (relation.isInverse) return; | ||
const writeFieldType = type => { | ||
const writeFieldType = (type) => { | ||
if (type === 'uuid') return 'string'; | ||
@@ -77,0 +77,0 @@ if (type === 'json') return 'any'; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
1
8202
145
Updatedconcise-types@^0.4.2