Socket
Socket
Sign inDemoInstall

appversion

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appversion - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

CHANGELOG.md

2

appversion.default.json

@@ -18,3 +18,3 @@ {

"config": {
"appversion": "1.3.0",
"appversion": "1.4.0",
"markdown": [],

@@ -21,0 +21,0 @@ "json": [],

/*
* Project: appversion
* Version: 1.3.0
* Version: 1.4.0
* Author: delvedor

@@ -5,0 +5,0 @@ * Twitter: @delvedor

@@ -1,6 +0,6 @@

#!/usr/bin/env node
#! /usr/bin/env node
/*
* Project: appversion
* Version: 1.3.0
* Version: 1.4.0
* Author: delvedor

@@ -14,23 +14,16 @@ * Twitter: @delvedor

const fs = require('fs')
const resolve = require('path').resolve
const exec = require('child_process').exec
const walk = require('walk')
const appversion = require('commander')
const check = require('type-check').typeCheck
const helpDocs = ` Semantic Versioning: http://semver.org
AppVersion documentation: https://github.com/delvedor/appversion`
const apvVersion = '1.3.0'
// Modules
const program = require('commander')
// apv parameters and functions
const update = require('./lib/update').update
const setVersion = require('./lib/set').setVersion
const setStatus = require('./lib/set').setStatus
const createBadge = require('./lib/badge').createBadge
const init = require('./lib/init').init
const checkUpdate = require('./lib/updater').checkUpdate
const apvVersion = require('./lib/parameters').apvVersion
const helpDocs = require('./lib/parameters').helpDocs
// Filenames
const JSON_FILE = 'appversion.json'
const JSON_FILE_DEFAULT = resolve(__dirname, 'appversion.default.json')
// Functional functions expressions
const shieldUrl = (part) => `https://img.shields.io/badge/${part}-brightgreen.svg?style=flat`
const mdCode = (tag, url) => `[![AppVersion-${tag}](${url})](https://github.com/delvedor/appversion?#${tag})`
const composeReadmeCode = (tag, part) => mdCode(tag, shieldUrl(part))
// commands arguments
appversion
program
.version(apvVersion)

@@ -41,330 +34,20 @@ .usage('<option> <param>')

.option('set-status <param>', 'Sets a specific status, the <param> stage can be stable|rc|beta|alpha and the number must be a number', setStatus)
.option('generate-version-badge', 'Generates the .md code of a shield badge with the version of your application', createVersionBadge)
.option('generate-status-badge', 'Generates the .md code of a shield badge with the status of your application', createStatusBadge)
.option('generate-badge <param>', 'Generates the .md code of a shield badge with the version of your application, <param> can be version|status', createBadge)
.option('init', 'Generates the appversion.json file', init)
.on('*', function (command) {
this.commands.some(function (command) {
return command._name === process.argv[0]
}) || this.help()
})
// Custom docs
appversion.on('--help', () => {
program.on('--help', () => {
console.log(helpDocs)
})
appversion.parse(process.argv)
program.parse(process.argv)
// Calls help() if there are no parameters
if (process.argv.length <= 2) appversion.help()
if (process.argv.length <= 2) program.help()
/**
* Calls the correct update function based on the parameter.
*/
function update (param) {
if (!check('String', param)) return
if (param === 'major' || param === 'minor' || param === 'patch') {
updateVersion(param)
} else if (param === 'build') {
updateBuild()
} else if (param === 'commit') {
updateCommit()
} else {
console.log('Error, type apv help to get more informations!\n')
}
}
/**
* Increase the major|minor|patch version number.
* @param {String} version [major|minor|patch]
*/
function updateVersion (param) {
let obj = getJsonObj(JSON_FILE)
obj.version[param]++
if (param === 'major') obj.version.minor = obj.version.patch = 0
if (param === 'minor') obj.version.patch = 0
// The build number is reset whenever we update the version number
obj.build.number = 0
writeJson(obj, `Version updated to ${obj.version.major}.${obj.version.minor}.${obj.version.patch}\n`)
writeOtherJson(`${obj.version.major}.${obj.version.minor}.${obj.version.patch}`)
createVersionBadge(true)
}
/**
* Increase the build number and updates the date.
*/
function updateBuild () {
let obj = getJsonObj(JSON_FILE)
// The date is a string representing the Date object
let date = (new Date()).toString()
obj.build.date = date
obj.build.number++
obj.build.total++
writeJson(obj, `Build updated to ${obj.build.number}/${obj.build.total}\n`)
}
/**
* Updates the commit code.
*/
function updateCommit () {
let obj = getJsonObj(JSON_FILE)
exec('git log --oneline', function (error, stdout) {
if (error) {
obj.commit = null
writeJson(obj, 'No Git repository found.\n')
} else {
obj.commit = stdout.substring(0, 7)
writeJson(obj, `Commit updated to ${stdout.substring(0, 7)}\n`)
}
})
}
/**
* Sets a specific version number.
* @param {String} newVersion [version number "x.y.z"]
*/
function setVersion (param) {
if (!check('String', param)) return
let obj = getJsonObj(JSON_FILE)
let version = param.split('.')
if (version.length !== 3) {
console.log('Insert a valid version number formatted in this way: "x.y.z"\n')
return
}
version[0] = Number(version[0])
version[1] = Number(version[1])
version[2] = Number(version[2])
if (!check('Number', version[0]) || !check('Number', version[1]) || !check('Number', version[2])) {
console.log('The major, minor and patch values must be Numbers\n')
return
}
obj.version.major = version[0]
obj.version.minor = version[1]
obj.version.patch = version[2]
obj.build.number = 0
writeJson(obj, `Version updated to ${version[0]}.${version[1]}.${version[2]}\n`)
writeOtherJson(`${version[0]}.${version[1]}.${version[2]}`)
createVersionBadge(true)
}
/**
* Sets a specific status.
* @param {String} newStatus [status string "stable|rc|beta|alpha"]
*/
function setStatus (param) {
if (!check('String', param)) return
let obj = getJsonObj(JSON_FILE)
let status = param.split('.')
if (status[1]) {
status[1] = Number(status[1])
if (!check('Number', status[1])) {
console.log('Insert a valid status.number number\n')
return
}
}
let match = ['Stable', 'stable', 'RC', 'rc', 'Beta', 'beta', 'Alpha', 'alpha']
if (match.indexOf(status[0]) === -1) {
console.log('Insert a valid status.stage string\n')
return
}
obj.status.stage = status[0]
// if there's not the status number, it's setted to zero
obj.status.number = status[1] || 0
writeJson(obj, `Status updated to ${status[0]}.${status[1] || 0}\n`)
createStatusBadge(true)
}
/**
* Generates the badge with the current version.
* @param {Boolean} updateMD [If this parameter is undefined means that the function was called by the user, so it outputs the badge code.]
*/
function createVersionBadge (updateMD) {
if (!check('Boolean | Undefined', updateMD)) return
let obj = getJsonObj(JSON_FILE)
if (updateMD) {
// compose the url
let url = shieldUrl(`AppVersion-${obj.version.major}.${obj.version.minor}.${obj.version.patch}`)
obj.config.markdown.map((file) => {
return appendBadgeToMD(url, file, '[![AppVersion-version]', '?#version')
})
} else {
// compose the badge .md code
let readmeCode = composeReadmeCode('version', `AppVersion-${obj.version.major}.${obj.version.minor}.${obj.version.patch}`)
console.log(`Version badge generated!
${readmeCode}
`)
}
}
/**
* Generates the badge with the current status.
* @param {Boolean} updateMD [If this parameter is undefined means that the function was called by the user, so it outputs the badge code.]
*/
function createStatusBadge (updateMD) {
if (!check('Boolean | Undefined', updateMD)) return
let obj = getJsonObj(JSON_FILE)
// If the status.number is zero is not displayed
let status = obj.status.number > 0 ? `${obj.status.stage}%20${obj.status.number}` : obj.status.stage
if (updateMD) {
// compose the url
let url = shieldUrl(`Status-${status}`)
obj.config.markdown.map((file) => {
return appendBadgeToMD(url, file, '[![AppVersion-status]', '?#status')
})
} else {
// compose the badge .md code
let readmeCode = composeReadmeCode('status', `Status-${status}`)
console.log(`Status badge generated!
${readmeCode}
`)
}
}
/**
* Search and updates the badge in a .md file.
* @param {String} url [The new url]
* @param {String} markdownFile [The name of the .md file]
* @param {String} tag [version or status tag]
* @param {String} getParam [parameter to put at the end of the url]
*/
function appendBadgeToMD (url, markdownFile, tag, getParam) {
if (!check('String', url) || !check('String', markdownFile) || !check('String', tag) || !check('String', getParam)) return
try {
fs.readFile(resolve('./', markdownFile), 'utf8', (err, data) => {
if (err) console.log(err)
// if the badge not exist in the .md file
if (data.substring(0, data.indexOf(tag)) === -1) return
// update .md file
let apvBadge = `${data.substring(0, data.indexOf(tag))}${tag}(${url})]${data.substring(data.indexOf('(https://github.com/delvedor/appversion' + getParam + ')'))}`
fs.writeFile(resolve('./', markdownFile), apvBadge, (err) => { if (err) console.log(err) })
})
} catch (err) {
console.log(err)
}
}
/**
* Creates the appversion file from the default file (one time use).
*/
function init () {
let obj = getJsonObj(JSON_FILE_DEFAULT)
try {
let fd = fs.openSync(resolve('./', JSON_FILE), 'wx+')
let json = `${JSON.stringify(obj, null, 2)}\n`
fs.writeFileSync(JSON_FILE, json)
fs.closeSync(fd)
console.log('appversion.json created, type apv help to get more informations!\n')
} catch (err) {
if (err.code === 'EEXIST') {
console.log('\nappversion.json already exists, type "apv -h" to get more informations!\n')
return
}
throw new Error(err)
}
}
/**
* Returns the appversion json content.
* @param {String} filename [name of the json]
* @return {Object} [content of the json]
*/
function getJsonObj (file) {
if (!check('String', file)) return
try {
let obj = JSON.parse(fs.readFileSync(resolve('./', file)))
// checks if the appversion.json is at the latest version
if (file === JSON_FILE && (!obj.config || obj.config.appversion !== apvVersion)) obj = updateAppversion(obj)
return obj
} catch (err) {
if (err.code === 'ENOENT') {
console.log('\nCould not find appversion.json\nType "apv init" for generate the file and start use AppVersion.\n')
process.exit(1)
} else {
throw new Error(err)
}
}
}
/**
* Wrote into the json the object passed as argument
* @param {Object} obj [Full object]
* @param {String} message [Optional message]
*/
function writeJson (obj, message) {
if (!check('Object', obj) || !check('String | Undefined', message)) return
let json = `${JSON.stringify(obj, null, 2)}\n`
try {
fs.writeFileSync(JSON_FILE, json)
if (message) console.log(message)
} catch (err) {
throw new Error(err)
}
}
/**
* Extension of the above function.
* Updates package.json, bower.json and all other json in appversion.json
* @param {String} version [version number x.y.z]
*/
function writeOtherJson (version) {
if (!check('String', version)) return
let obj = getJsonObj(JSON_FILE)
// ignore every subfolder in the project
if (obj.config.ignore.indexOf('*') > -1) return
// default ignored subfolders
obj.config.ignore.push('node_modules', 'bower_components', '.git')
// default json files
obj.config.json.push('package.json', 'bower.json')
let walker = walk.walk(resolve('./'), {followLinks: false, filters: obj.config.ignore})
walker.on('file', function (root, fileStats, next) {
// if the filename is inside the appversion's json array
if (obj.config.json.indexOf(fileStats.name) > -1) {
let fileObj
try {
fileObj = JSON.parse(fs.readFileSync(resolve(root, fileStats.name)))
} catch (err) {
return
}
// If the "version" field is not present in the json file we add it
fileObj.version = version
let json = `${JSON.stringify(fileObj, null, 2)}\n`
fs.writeFileSync(resolve(root, fileStats.name), json)
}
next()
})
}
/**
* This function updates the appversion.json to the latest appversion json structure.
* @param {Object} obj [actual appversion object]
* @return {Object} [correct appversion object]
*/
function updateAppversion (obj) {
if (!check('Object', obj)) return
// if the "config" filed is not present in the json we add it
if (!obj.config) obj.config = { appversion: apvVersion, ignore: [], markdown: [], json: [] }
// if the "ignore" filed is present in the json we move it to config
if (obj.ignore) {
obj.config.ignore = obj.ignore
delete obj.ignore
}
// if the "markdown" filed is present in the json we move it to config
if (obj.markdown) {
obj.config.markdown = obj.markdown
delete obj.markdown
}
// if the "markdown" filed is present in the json we move it to config
if (obj.json) {
obj.config.json = obj.json
delete obj.json
}
// if the "package.json" and "bower.json" are present in the "config.json" array field, we remove them
if (obj.config.json.indexOf('package.json') > -1) obj.config.json.splice(obj.config.json.indexOf('package.json'), 1)
if (obj.config.json.indexOf('bower.json') > -1) obj.config.json.splice(obj.config.json.indexOf('bower.json'), 1)
// Remove the appversion field
if (obj.appversion) delete obj.appversion
// updates the appversion.json version number
obj.config.appversion = apvVersion
console.log('appversion.json updated to the latest version.\n')
return obj
}
// Checks for an update
checkUpdate()
{
"name": "appversion",
"version": "1.3.0",
"version": "1.4.0",
"description": "An extension of npm version, useful for keep track of the version, build and commit of your javascript application.",

@@ -10,3 +10,3 @@ "main": "appversion.js",

"scripts": {
"pretest": "standard test && standard appversion.js && standard apv.js",
"pretest": "standard",
"test": "node test || true"

@@ -33,8 +33,11 @@ },

"devDependencies": {
"standard": "^6.0.7",
"tape": "^4.2.0"
"standard": "^6.0.8",
"tape": "^4.5.1"
},
"dependencies": {
"app-root-path": "^1.0.0",
"chalk": "^1.1.1",
"commander": "^2.9.0",
"request": "^2.69.0",
"semver": "^5.1.0",
"type-check": "^0.3.2",

@@ -41,0 +44,0 @@ "walk": "^2.3.9"

# AppVersion <a name="version"></a><a name="status"></a>
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![AppVersion-version](https://img.shields.io/badge/AppVersion-1.3.0-brightgreen.svg?style=flat)](https://github.com/delvedor/appversion?#version) [![AppVersion-status](https://img.shields.io/badge/Status-RC-brightgreen.svg?style=flat)](https://github.com/delvedor/appversion?#status)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![AppVersion-version](https://img.shields.io/badge/AppVersion-1.4.0-brightgreen.svg?style=flat)](https://github.com/delvedor/appversion?#version) [![AppVersion-status](https://img.shields.io/badge/Status-RC-brightgreen.svg?style=flat)](https://github.com/delvedor/appversion?#status)

@@ -7,3 +7,3 @@ **AppVersion** is intended as an extension of *npm version* and is a **cli tool** for keep track the *version*, *build*, *status* and *commit* of your javascript application.

Usually a project has different configuration/package-manager files, such as *package.json* and/or *bower.json*, and can be really tedious update the project number in every file.
Usually a project has different configuration/package-manager files, such as *package.json*, and can be really tedious update the project number in every file.
Here comes to help AppVersion, an easy to use command line tool who updates all the files for you.

@@ -79,4 +79,4 @@ In addition AppVersion keeps track of the build date and number.

| |
| generate-version-badge | | Generates the .md code of a shield badge with the version of your application.|
| generate-version-badge | | Generates the .md code of a shield badge with the status of your application.|
| generate-badge | version | Generates the .md code of a shield badge with the version of your application
| | status | Generates the .md code of a shield badge with the status of your application
| init | | Generates the appversion.json file.|

@@ -93,3 +93,3 @@ | |

By default, AppVersion updates the *"version"* field in the `package.json` and `bower.json` files; if you want to update the *"version"* field in more json files, just add the file name inside *appversion.json* in the json array field.
By default, AppVersion updates the *"version"* field in `package.json`; if you want to update the *"version"* field in more json files, just add the file name inside *appversion.json* in the json array field.

@@ -101,3 +101,4 @@ AppVersion looks recursively inside all the subfolders of your project for json files, by default it ignores `node_modules`, `bower_components` and `.git` folders; if you want to ignore more folders just add the folder name inside *appversion.json* in the ignore array field.

AppVersion can provide you a wonderful shield badge with the version of your application that you can put in you .md file, like what you see at the top of this file.
Generate the badge is very easy, just type ```apv generate-version-badge``` for the version badge and ```apv generate-status-badge``` for the status badge and copy the output inside your .md file, then add the name of the md file (with the extension) inside the markdown array field in *appversion.json*, from now AppVersion will keep updated the badges every time you update your application.
Generate the badge is very easy, just type ```apv generate-badge version``` for the version badge and ```apv generate-badge status``` for the status badge and copy the output inside your .md file, then add the name of the md file (with the extension) inside the markdown array field in *appversion.json*, from now AppVersion will keep updated the badges every time you update your application.
This feature make use of the amazing service [shields.io](http://shields.io/).

@@ -203,2 +204,5 @@ ### In app:

- [x] Move `json`, `markdown`, `ignore` and `appversion` inside `config` field
- [x] Implement "New version" message
- [x] Split the code in multiple files divided by function.
- [ ] When init is called, apv must create appversion.json with the same version number of package.json.
- [ ] Integration with GitHub

@@ -205,0 +209,0 @@ - [ ] SHA generator

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc