Skip to content

Commit

Permalink
feat: custom 'bumpFiles' and 'packageFiles' support
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bottigliero authored and jbottigliero committed Jul 18, 2019
1 parent 8d9656c commit 65e5708
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 28 deletions.
8 changes: 8 additions & 0 deletions command.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ const { START_OF_LAST_RELEASE_PATTERN } = require('./lib/lifecycles/changelog')

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
13 changes: 13 additions & 0 deletions defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ Object.keys(spec.properties).forEach(propertyKey => {
defaults[propertyKey] = property.default
})

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
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const printError = require('./lib/print-error')
const tag = require('./lib/lifecycles/tag')

module.exports = function standardVersion (argv) {
let defaults = require('./defaults')
/**
* `--message` (`-m`) support will be removed in the next major version.
*/
Expand All @@ -24,8 +25,9 @@ module.exports = function standardVersion (argv) {
}
}

let args = Object.assign({}, defaults, argv)
let pkg
bump.pkgFiles.forEach((filename) => {
args.packageFiles.forEach((filename) => {
if (pkg) return
let pkgPath = path.resolve(process.cwd(), filename)
try {
Expand All @@ -34,8 +36,6 @@ module.exports = function standardVersion (argv) {
} catch (err) {}
})
let newVersion
let defaults = require('./defaults')
let args = Object.assign({}, defaults, argv)

return Promise.resolve()
.then(() => {
Expand Down
58 changes: 33 additions & 25 deletions lib/lifecycles/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,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 @@ -153,31 +140,52 @@ 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) {
let configPath = path.resolve(process.cwd(), filename)
args.bumpFiles.forEach(function (bumpFile) {
if (typeof bumpFile !== 'object') {
bumpFile = {
filename: bumpFile
}
}

let configPath = path.resolve(process.cwd(), bumpFile.filename)
try {
if (dotgit.ignore(configPath)) return
let stat = fs.lstatSync(configPath)
if (stat.isFile()) {
let data = fs.readFileSync(configPath, 'utf8')
let indent = detectIndent(data).indent
let newline = detectNewline(data)
let 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))

let config
if (!bumpFile.replacer) {
config = JSON.parse(data)
checkpoint(args, 'bumping version in ' + bumpFile.filename + ' from %s to %s', [config.version, newVersion])
config.version = newVersion
writeFile(args, configPath, stringifyPackage(config, indent, newline))
} else {
let matches
let replacement = false
while (
(matches = bumpFile.replacer.exec(data)) !== null &&
matches[0] !== replacement
) {
if (!replacement) {
replacement = matches[0].replace(matches[1], newVersion)
}
data = data.replace(matches[1], newVersion)
}
writeFile(args, configPath, data)
}
// flag any config files that we modify the version # for
// as having been updated.
configsToUpdate[filename] = true
configsToUpdate[bumpFile.filename] = true
}
} catch (err) {
if (err.code !== 'ENOENT') console.warn(err.message)
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,28 @@ describe('standard-version', function () {
})
})

describe('custom `bumpFiles` support', function () {
it('mix.exs', function () {
// @todo This file path is relative to the `tmp` directory, which is a little confusing
fs.copyFileSync('../test/mocks/mix.exs', 'mix.exs')
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
return require('./index')({
silent: true,
bumpFiles: [
{
filename: 'mix.exs',
replacer: /version: "(.*)"/
}
]
})
.then(() => {
fs.readFileSync('mix.exs', 'utf-8').should.contain('version: "1.1.0"')
})
})
})

describe('npm-shrinkwrap.json support', function () {
beforeEach(function () {
writeNpmShrinkwrapJson('1.0.0')
Expand Down
12 changes: 12 additions & 0 deletions test/mocks/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule StandardVersion.MixProject do
use Mix.Project
def project do
[
app: :standard_version
version: "0.0.1",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
end

0 comments on commit 65e5708

Please sign in to comment.