@everymundo/generate-microservice-name
Advanced tools
Comparing version 1.1.1 to 1.1.2
52
index.js
// this will set the global.__rootdir in cases it is not yet set | ||
require('@everymundo/global-root-dir').setGlobalRootDir(); | ||
require('@everymundo/global-root-dir').setGlobalRootDir() | ||
const { getPackageJSON } = require('./lib/get-package-json'); | ||
const { getPackageJSON } = require('./lib/get-package-json') | ||
const nameRegExp = /^[-\w]+-v\d+$/; | ||
const nameRegExp = /^[-\w]+-v\d+$/ | ||
const getMajorVersionNumber = (appPackageJson = getPackageJSON()) => { | ||
const { version } = appPackageJson; | ||
return +version.substr(0, version.indexOf('.')); | ||
}; | ||
const { version } = appPackageJson | ||
return +version.substr(0, version.indexOf('.')) | ||
} | ||
const getPrefixedMajorVersionNumber = (appPackageJson = getPackageJSON()) => { | ||
const { version } = appPackageJson; | ||
const majorVersionNumber = getMajorVersionNumber({ version }); | ||
return `v${majorVersionNumber}`; | ||
}; | ||
const { version } = appPackageJson | ||
const majorVersionNumber = getMajorVersionNumber({ version }) | ||
return `v${majorVersionNumber}` | ||
} | ||
const getCleanAppName = (appPackageJson = getPackageJSON()) => { | ||
const { name } = appPackageJson; | ||
return name.substr(name.lastIndexOf('/') + 1); | ||
}; | ||
const { name } = appPackageJson | ||
return name.substr(name.lastIndexOf('/') + 1) | ||
} | ||
const generateMicroserviceNameFrom = ({ name, version }) => { | ||
// const majorVersionNumber = +version.substr(0, version.indexOf('.')); | ||
const prefixedMajorVersionNumber = getPrefixedMajorVersionNumber({ version }); | ||
const prefixedMajorVersionNumber = getPrefixedMajorVersionNumber({ version }) | ||
// const cleanName = name.substr(name.lastIndexOf('/') + 1); | ||
const cleanName = getCleanAppName({ name }); | ||
const cleanName = getCleanAppName({ name }) | ||
// const appName = `${cleanName}-v${majorVersionNumber}`; | ||
const appName = `${cleanName}-${prefixedMajorVersionNumber}`; | ||
const appName = `${cleanName}-${prefixedMajorVersionNumber}` | ||
return appName; | ||
}; | ||
return appName | ||
} | ||
const itHasAValidCustomMicroserviceName = ({ config } = {}) => | ||
Boolean(config) && nameRegExp.test(config.customMicroserviceName); | ||
Boolean(config) && nameRegExp.test(config.customMicroserviceName) | ||
const getMicroserviceName = () => { | ||
const appPackageJson = getPackageJSON(); | ||
const appPackageJson = getPackageJSON() | ||
if (itHasAValidCustomMicroserviceName(appPackageJson)) { | ||
return appPackageJson.config.customMicroserviceName; | ||
return appPackageJson.config.customMicroserviceName | ||
} | ||
return generateMicroserviceNameFrom(appPackageJson); | ||
}; | ||
return generateMicroserviceNameFrom(appPackageJson) | ||
} | ||
@@ -59,3 +57,3 @@ module.exports = { | ||
getPackageJSON, | ||
nameRegExp, | ||
}; | ||
nameRegExp | ||
} |
@@ -1,7 +0,7 @@ | ||
require('@everymundo/global-root-dir').setGlobalRootDir(); | ||
require('@everymundo/global-root-dir').setGlobalRootDir() | ||
const path = require('path'); | ||
const path = require('path') | ||
const getPackageJSON = () => require(path.join(global.__rootdir, 'package.json')); | ||
const getPackageJSON = () => require(path.join(global.__rootdir, 'package.json')) | ||
module.exports = { getPackageJSON }; | ||
module.exports = { getPackageJSON } |
{ | ||
"name": "@everymundo/generate-microservice-name", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "It generates the microservice name by reading the data from the projects package.json", | ||
"main": "index.js", | ||
"scripts": { | ||
"cover": "NODE_ENV=test istanbul cover -x '*test.js' _mocha -- -R spec", | ||
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"check-lint": "eslint *.js lib/*.js test/*.js routes/*.js", | ||
"fix-lint": "eslint --fix *.js lib/*.js test/*.js routes/*.js", | ||
"test": "NODE_ENV=test LOG_LEVEL=test mocha -R spec $@" | ||
"cover": "LOG_LEVEL=trace nyc -x test --reporter=lcov --reporter=text mocha test --recursive", | ||
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"check-lint": "standard --env=mocha *.js lib/*.js spec/*.js spec/lib/*.js", | ||
"fix-lint": "standard --env=mocha --fix *.js lib/*.js test/*.js", | ||
"test": "NODE_ENV=test LOG_LEVEL=test mocha --recursive $@" | ||
}, | ||
@@ -31,12 +31,13 @@ "config": { | ||
"dependencies": { | ||
"@everymundo/global-root-dir": "^1.1.0" | ||
"@everymundo/global-root-dir": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"@everymundo/cleanrequire": "^1.1.1", | ||
"chai": "^4.1.2", | ||
"ghooks": "^2.0.2", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"mocha": "^5.0.4", | ||
"sinon": "^4.4.8" | ||
"@everymundo/cleanrequire": "^1.2.1", | ||
"chai": "^4.2.0", | ||
"ghooks": "^2.0.4", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"sinon": "^7.3.2", | ||
"standard": "^12.0.1" | ||
} | ||
} |
@@ -1,101 +0,100 @@ | ||
'require strict'; | ||
'require strict' | ||
/* eslint-disable no-unused-expressions */ | ||
const | ||
sinon = require('sinon'), | ||
path = require('path'), | ||
cleanrequire = require('@everymundo/cleanrequire'), | ||
{ expect } = require('chai'); | ||
const sinon = require('sinon') | ||
const path = require('path') | ||
const { expect } = require('chai') | ||
const cleanrequire = require('@everymundo/cleanrequire') | ||
require('@everymundo/global-root-dir').setGlobalRootDir(path.dirname(__dirname)); | ||
require('@everymundo/global-root-dir').setGlobalRootDir(path.dirname(__dirname)) | ||
describe('index.js', () => { | ||
let box; | ||
beforeEach(() => { box = sinon.sandbox.create(); }); | ||
let box | ||
beforeEach(() => { box = sinon.createSandbox() }) | ||
// retores the sandbox | ||
afterEach(() => { box.restore(); }); | ||
afterEach(() => { box.restore() }) | ||
describe('#getPackageJSON', () => { | ||
it('should return this package.json', () => { | ||
const { getPackageJSON } = require('../index'); | ||
const { getPackageJSON } = require('../index') | ||
const expected = require('../package.json'); | ||
const res = getPackageJSON(); | ||
const expected = require('../package.json') | ||
const res = getPackageJSON() | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
expect(res).to.equal(expected) | ||
}) | ||
}) | ||
describe('#getMajorVersionNumber', () => { | ||
const { getMajorVersionNumber } = require('../index'); | ||
const { getMajorVersionNumber } = require('../index') | ||
context('passing a version', () => { | ||
it('should load package.json', () => { | ||
const res = getMajorVersionNumber({version: '2.3.4'}); | ||
const res = getMajorVersionNumber({ version: '2.3.4' }) | ||
expect(res).to.equal(2); | ||
}); | ||
}); | ||
expect(res).to.equal(2) | ||
}) | ||
}) | ||
context('passing NO arguments', () => { | ||
it('should load package.json', () => { | ||
const expected = +require('../package.json').version.split('.').shift(); | ||
const res = getMajorVersionNumber(); | ||
const expected = +require('../package.json').version.split('.').shift() | ||
const res = getMajorVersionNumber() | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
}); | ||
expect(res).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
describe('#getPrefixedMajorVersionNumber', () => { | ||
const { getPrefixedMajorVersionNumber } = require('../index'); | ||
const { getPrefixedMajorVersionNumber } = require('../index') | ||
context('passing a version', () => { | ||
it('should load package.json', () => { | ||
const res = getPrefixedMajorVersionNumber({version: '2.3.4'}); | ||
const res = getPrefixedMajorVersionNumber({ version: '2.3.4' }) | ||
expect(res).to.equal('v2'); | ||
}); | ||
}); | ||
expect(res).to.equal('v2') | ||
}) | ||
}) | ||
context('passing NO arguments', () => { | ||
it('should load package.json', () => { | ||
const expected = `v${require('../package.json').version.split('.').shift()}`; | ||
const res = getPrefixedMajorVersionNumber(); | ||
const expected = `v${require('../package.json').version.split('.').shift()}` | ||
const res = getPrefixedMajorVersionNumber() | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
}); | ||
expect(res).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
describe('#getCleanAppName', () => { | ||
const { getCleanAppName } = require('../index'); | ||
const { getCleanAppName } = require('../index') | ||
context('passing a simple name', () => { | ||
it('should load package.json', () => { | ||
const res = getCleanAppName({name: 'simple-app'}); | ||
const res = getCleanAppName({ name: 'simple-app' }) | ||
expect(res).to.equal('simple-app'); | ||
}); | ||
}); | ||
expect(res).to.equal('simple-app') | ||
}) | ||
}) | ||
context('passing a name with namespace', () => { | ||
it('should load package.json', () => { | ||
const res = getCleanAppName({name: '@namespace/namespaced-name'}); | ||
const res = getCleanAppName({ name: '@namespace/namespaced-name' }) | ||
expect(res).to.equal('namespaced-name'); | ||
}); | ||
}); | ||
expect(res).to.equal('namespaced-name') | ||
}) | ||
}) | ||
context('passing NO arguments', () => { | ||
it('should load package.json', () => { | ||
const expected = require('../package.json').name.split('/').pop(); | ||
const res = getCleanAppName(); | ||
const expected = require('../package.json').name.split('/').pop() | ||
const res = getCleanAppName() | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
}); | ||
expect(res).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
@@ -105,49 +104,49 @@ describe('#generateMicroserviceNameFrom', () => { | ||
it('should return the correct generated name', () => { | ||
const { generateMicroserviceNameFrom } = require('../index'); | ||
const { generateMicroserviceNameFrom } = require('../index') | ||
const input = { name: '@namespace/app-name', version: '2.3.4' }; | ||
const res = generateMicroserviceNameFrom(input); | ||
const expected = 'app-name-v2'; | ||
const input = { name: '@namespace/app-name', version: '2.3.4' } | ||
const res = generateMicroserviceNameFrom(input) | ||
const expected = 'app-name-v2' | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
expect(res).to.equal(expected) | ||
}) | ||
}) | ||
context('when name does NOT HAVE a workspace', () => { | ||
it('should return the correct generated name', () => { | ||
const { generateMicroserviceNameFrom } = require('../index'); | ||
const { generateMicroserviceNameFrom } = require('../index') | ||
const input = { name: 'some-app-name', version: '1.2.3.4' }; | ||
const res = generateMicroserviceNameFrom(input); | ||
const expected = 'some-app-name-v1'; | ||
const input = { name: 'some-app-name', version: '1.2.3.4' } | ||
const res = generateMicroserviceNameFrom(input) | ||
const expected = 'some-app-name-v1' | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
}); | ||
expect(res).to.equal(expected) | ||
}) | ||
}) | ||
}) | ||
describe('#itHasAValidCustomMicroserviceName', () => { | ||
const { itHasAValidCustomMicroserviceName } = require('../index'); | ||
const { itHasAValidCustomMicroserviceName } = require('../index') | ||
context('when it DOES NOT have a custom name', () => { | ||
it('should return false', () => { | ||
const res = itHasAValidCustomMicroserviceName(); | ||
const res = itHasAValidCustomMicroserviceName() | ||
expect(res).to.be.false; | ||
}); | ||
expect(res).to.be.false | ||
}) | ||
it('should return false', () => { | ||
const input = {}; | ||
const res = itHasAValidCustomMicroserviceName(input); | ||
const input = {} | ||
const res = itHasAValidCustomMicroserviceName(input) | ||
expect(res).to.be.false; | ||
}); | ||
expect(res).to.be.false | ||
}) | ||
it('should return false', () => { | ||
const input = {config: {}}; | ||
const res = itHasAValidCustomMicroserviceName(input); | ||
const input = { config: {} } | ||
const res = itHasAValidCustomMicroserviceName(input) | ||
expect(res).to.be.false; | ||
}); | ||
}); | ||
expect(res).to.be.false | ||
}) | ||
}) | ||
@@ -159,11 +158,11 @@ context('when it has a custom name', () => { | ||
config: { | ||
customMicroserviceName: 'app-name-v2', | ||
}, | ||
}; | ||
customMicroserviceName: 'app-name-v2' | ||
} | ||
} | ||
const res = itHasAValidCustomMicroserviceName(input); | ||
const res = itHasAValidCustomMicroserviceName(input) | ||
expect(res).to.be.true; | ||
}); | ||
}); | ||
expect(res).to.be.true | ||
}) | ||
}) | ||
@@ -174,16 +173,16 @@ context('but it is INVALID', () => { | ||
config: { | ||
customMicroserviceName: 'this is not a valid name', | ||
}, | ||
}; | ||
customMicroserviceName: 'this is not a valid name' | ||
} | ||
} | ||
const res = itHasAValidCustomMicroserviceName(input); | ||
const res = itHasAValidCustomMicroserviceName(input) | ||
expect(res).to.be.false; | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(res).to.be.false | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('#getMicroserviceName', () => { | ||
const packageLib = require('../lib/get-package-json'); | ||
const packageLib = require('../lib/get-package-json') | ||
@@ -193,12 +192,12 @@ context('when it DOES NOT have a custom name', () => { | ||
box.stub(packageLib, 'getPackageJSON') | ||
.callsFake(() => ({name: 'my-app', version: '1.2.0'})); | ||
}); | ||
.callsFake(() => ({ name: 'my-app', version: '1.2.0' })) | ||
}) | ||
it('should return my-app-v1', () => { | ||
const { getMicroserviceName } = cleanrequire('../'); | ||
const res = getMicroserviceName(); | ||
const { getMicroserviceName } = cleanrequire('../') | ||
const res = getMicroserviceName() | ||
expect(res).to.equal('my-app-v1'); | ||
}); | ||
}); | ||
expect(res).to.equal('my-app-v1') | ||
}) | ||
}) | ||
@@ -212,16 +211,16 @@ context('when it has a custom name', () => { | ||
config: { | ||
customMicroserviceName: 'my-custom-name-v2', | ||
}, | ||
}; | ||
customMicroserviceName: 'my-custom-name-v2' | ||
} | ||
} | ||
box.stub(packageLib, 'getPackageJSON') | ||
.callsFake(() => packageObject); | ||
}); | ||
.callsFake(() => packageObject) | ||
}) | ||
it('should return the value of config.customMicroserviceName', () => { | ||
const { getMicroserviceName } = cleanrequire('../'); | ||
const res = getMicroserviceName(); | ||
const { getMicroserviceName } = cleanrequire('../') | ||
const res = getMicroserviceName() | ||
expect(res).to.equal('my-custom-name-v2'); | ||
}); | ||
}); | ||
expect(res).to.equal('my-custom-name-v2') | ||
}) | ||
}) | ||
@@ -234,18 +233,18 @@ context('but it is INVALID', () => { | ||
config: { | ||
customMicroserviceName: 'my-custom-name', | ||
}, | ||
}; | ||
customMicroserviceName: 'my-custom-name' | ||
} | ||
} | ||
box.stub(packageLib, 'getPackageJSON') | ||
.callsFake(() => packageObject); | ||
}); | ||
.callsFake(() => packageObject) | ||
}) | ||
it('should return the value compose the name', () => { | ||
const { getMicroserviceName } = cleanrequire('../'); | ||
const res = getMicroserviceName(); | ||
const { getMicroserviceName } = cleanrequire('../') | ||
const res = getMicroserviceName() | ||
expect(res).to.equal('my-app-v1'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(res).to.equal('my-app-v1') | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12886
7
239
1