Skip to content

Commit

Permalink
Merge branch 'master' into feat-header-support
Browse files Browse the repository at this point in the history
  • Loading branch information
tommywo authored Dec 7, 2019
2 parents f3e6473 + bc606f8 commit 9d51ff1
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 135 deletions.
226 changes: 144 additions & 82 deletions README.md

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const defaults = require('./defaults')

const yargs = require('yargs')
.usage('Usage: $0 [options]')
.option('packageFiles', {
default: defaults.packageFiles,
array: true
})
.option('bumpFiles', {
default: defaults.bumpFiles,
array: true
})
.option('release-as', {
alias: 'r',
describe: 'Specify the release type manually (like npm version <major|minor|patch>)',
Expand Down
15 changes: 14 additions & 1 deletion defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaults = {
skip: {},
dryRun: false,
gitTagFallback: true,
preset: 'conventionalcommits'
preset: require.resolve('conventional-changelog-conventionalcommits')
}

/**
Expand All @@ -29,4 +29,17 @@ Object.keys(spec.properties).forEach(propertyKey => {
*/
defaults.header = '# Changelog\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n'

defaults.packageFiles = [
'package.json',
'bower.json',
'manifest.json',
'composer.json'
]

defaults.bumpFiles = defaults.packageFiles.concat([
'package-lock.json',
'npm-shrinkwrap.json',
'composer.lock'
])

module.exports = defaults
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const latestSemverTag = require('./lib/latest-semver-tag')
const path = require('path')
const printError = require('./lib/print-error')
const tag = require('./lib/lifecycles/tag')
const { resolveUpdaterObjectFromArgument } = require('./lib/updaters')

module.exports = function standardVersion (argv) {
const defaults = require('./defaults')
/**
* `--message` (`-m`) support will be removed in the next major version.
*/
Expand Down Expand Up @@ -35,19 +37,21 @@ module.exports = function standardVersion (argv) {
throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`)
}

const args = Object.assign({}, defaults, argv)
let pkg
bump.pkgFiles.forEach((filename) => {
args.packageFiles.forEach((packageFile) => {
if (pkg) return
const pkgPath = path.resolve(process.cwd(), filename)
const updater = resolveUpdaterObjectFromArgument(packageFile)
const pkgPath = path.resolve(process.cwd(), updater.filename)
try {
const data = fs.readFileSync(pkgPath, 'utf8')
pkg = JSON.parse(data)
const contents = fs.readFileSync(pkgPath, 'utf8')
pkg = {
version: updater.updater.readVersion(contents),
private: typeof updater.updater.isPrivate === 'function' ? updater.updater.isPrivate(contents) : false
}
} catch (err) {}
})
let newVersion
const defaults = require('./defaults')
const args = Object.assign({}, defaults, argv)

return Promise.resolve()
.then(() => {
if (!pkg && args.gitTagFallback) {
Expand Down
62 changes: 26 additions & 36 deletions lib/lifecycles/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
const chalk = require('chalk')
const checkpoint = require('../checkpoint')
const conventionalRecommendedBump = require('conventional-recommended-bump')
const detectIndent = require('detect-indent')
const detectNewline = require('detect-newline')
const figures = require('figures')
const fs = require('fs')
const DotGitignore = require('dotgitignore')
const path = require('path')
const presetLoader = require('../preset-loader')
const runLifecycleScript = require('../run-lifecycle-script')
const semver = require('semver')
const stringifyPackage = require('stringify-package')
const writeFile = require('../write-file')

const { resolveUpdaterObjectFromArgument } = require('../updaters')
let configsToUpdate = {}

function Bump (args, version) {
Expand Down Expand Up @@ -51,19 +48,6 @@ Bump.getUpdatedConfigs = function () {
return configsToUpdate
}

Bump.pkgFiles = [
'package.json',
'bower.json',
'manifest.json',
'composer.json'
]

Bump.lockFiles = [
'package-lock.json',
'npm-shrinkwrap.json',
'composer.lock'
]

function getReleaseType (prerelease, expectedReleaseType, currentVersion) {
if (isString(prerelease)) {
if (isInPrerelease(currentVersion)) {
Expand Down Expand Up @@ -154,32 +138,38 @@ function bumpVersion (releaseAs, currentVersion, args) {
}

/**
* attempt to update the version # in a collection of common config
* files, e.g., package.json, bower.json.
*
* attempt to update the version number in provided `bumpFiles`
* @param args config object
* @param newVersion version # to update to.
* @return {string}
* @param newVersion version number to update to.
* @return void
*/
function updateConfigs (args, newVersion) {
const dotgit = DotGitignore()
Bump.pkgFiles.concat(Bump.lockFiles).forEach(function (filename) {
const configPath = path.resolve(process.cwd(), filename)
args.bumpFiles.forEach(function (bumpFile) {
const updater = resolveUpdaterObjectFromArgument(bumpFile)
if (!updater) {
return
}
const configPath = path.resolve(process.cwd(), updater.filename)
try {
if (dotgit.ignore(configPath)) return
const stat = fs.lstatSync(configPath)
if (stat.isFile()) {
const data = fs.readFileSync(configPath, 'utf8')
const indent = detectIndent(data).indent
const newline = detectNewline(data)
const config = JSON.parse(data)
checkpoint(args, 'bumping version in ' + filename + ' from %s to %s', [config.version, newVersion])
config.version = newVersion
writeFile(args, configPath, stringifyPackage(config, indent, newline))
// flag any config files that we modify the version # for
// as having been updated.
configsToUpdate[filename] = true
}

if (!stat.isFile()) return
const contents = fs.readFileSync(configPath, 'utf8')
checkpoint(
args,
'bumping version in ' + updater.filename + ' from %s to %s',
[updater.updater.readVersion(contents), newVersion]
)
writeFile(
args,
configPath,
updater.updater.writeVersion(contents, newVersion)
)
// flag any config files that we modify the version # for
// as having been updated.
configsToUpdate[updater.filename] = true
} catch (err) {
if (err.code !== 'ENOENT') console.warn(err.message)
}
Expand Down
7 changes: 4 additions & 3 deletions lib/preset-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const spec = require('conventional-changelog-config-spec')

module.exports = (args) => {
let preset = args.preset || 'conventionalcommits'
if (preset === 'conventionalcommits') {
const defaultPreset = require.resolve('conventional-changelog-conventionalcommits')
let preset = args.preset || defaultPreset
if (preset === defaultPreset) {
preset = {
name: preset
name: defaultPreset
}
Object.keys(spec.properties).forEach(key => {
if (args[key] !== undefined) preset[key] = args[key]
Expand Down
59 changes: 59 additions & 0 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require('path')
const JSON_BUMP_FILES = require('../../defaults').bumpFiles
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt']

function getUpdaterByType (type) {
try {
return require(`./types/${type}`)
} catch (e) {
throw Error(`Unable to locate updated for provided type (${type}).`)
}
}

function getUpdaterByFilename (filename) {
if (JSON_BUMP_FILES.includes(path.basename(filename))) {
return getUpdaterByType('json')
}
if (PLAIN_TEXT_BUMP_FILES.includes(filename)) {
return getUpdaterByType('plain-text')
}
throw Error(
`Unsupported file (${filename}) provided for bumping.\n Please specifcy the updater \`type\` or use a custom \`updater\`.`
)
}

function getCustomUpdater (updater) {
return require(path.resolve(process.cwd(), updater))
}

module.exports.resolveUpdaterObjectFromArgument = function (arg) {
/**
* If an Object was not provided, we assume it's the path/filename
* of the updater.
*/
let updater = arg
if (typeof updater !== 'object') {
updater = {
filename: arg
}
}
try {
if (updater.updater) {
updater.updater = getCustomUpdater(updater.updater)
} else if (updater.type) {
updater.updater = getUpdaterByType(updater.type)
} else {
updater.updater = getUpdaterByFilename(updater.filename)
}
} catch (err) {
if (err.code !== 'ENOENT') console.warn(err.message)
}
/**
* We weren't able to resolve an updater for the argument.
*/
if (!updater.updater) {
return false
}

return updater
}
19 changes: 19 additions & 0 deletions lib/updaters/types/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const stringifyPackage = require('stringify-package')
const detectIndent = require('detect-indent')
const detectNewline = require('detect-newline')

module.exports.readVersion = function (contents) {
return JSON.parse(contents).version
}

module.exports.writeVersion = function (contents, version) {
const json = JSON.parse(contents)
const indent = detectIndent(contents).indent
const newline = detectNewline(contents)
json.version = version
return stringifyPackage(json, indent, newline)
}

module.exports.isPrivate = function (contents) {
return JSON.parse(contents).private
}
7 changes: 7 additions & 0 deletions lib/updaters/types/plain-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports.readVersion = function (contents) {
return contents
}

module.exports.writeVersion = function (_contents, version) {
return version
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,29 @@
"chalk": "2.4.2",
"conventional-changelog": "3.1.15",
"conventional-changelog-config-spec": "2.1.0",
"conventional-changelog-conventionalcommits": "4.2.3",
"conventional-recommended-bump": "6.0.5",
"detect-indent": "6.0.0",
"detect-newline": "3.0.0",
"detect-newline": "3.1.0",
"dotgitignore": "2.1.0",
"figures": "3.1.0",
"find-up": "4.1.0",
"fs-access": "1.0.1",
"git-semver-tags": "3.0.1",
"semver": "6.3.0",
"stringify-package": "1.0.1",
"yargs": "14.2.1"
"yargs": "15.0.2"
},
"devDependencies": {
"chai": "4.2.0",
"coveralls": "3.0.7",
"eslint": "6.2.2",
"coveralls": "3.0.9",
"eslint": "6.7.2",
"eslint-config-standard": "14.1.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-node": "9.2.0",
"eslint-plugin-node": "10.0.0",
"eslint-plugin-promise": "4.2.1",
"eslint-plugin-standard": "4.0.1",
"mocha": "6.2.0",
"mocha": "6.2.2",
"mock-git": "2.0.0",
"mockery": "2.1.0",
"nyc": "14.1.1",
Expand Down
Loading

0 comments on commit 9d51ff1

Please sign in to comment.