Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not update/commit files in .gitignore #230

Merged
merged 8 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ npm-debug.log

# coverage
coverage
package-lock.json
24 changes: 24 additions & 0 deletions lib/gitignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const Dotignore = require('@bcoe/dotignore')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Node 4 support, this file needs 'use strict' at the top. This fixes the failing tests for me locally.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nexdrew I guess I'll need to spin up a Windows VM ... grumble.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nexdrew smart 👍

const readFileSync = require('fs').readFileSync
const pathLib = require('path')

let matcher = null
module.exports = (path) => {
try {
if (!matcher) {
const gitignore = readFileSync(
pathLib.resolve(process.cwd(), '.gitignore'), 'utf8'
)
matcher = Dotignore.createMatcher(gitignore)
matcher.delimiter = pathLib.sep
}
return matcher.shouldIgnore(path)
} catch (err) {
if (err.code !== 'ENOENT') {
console.warn(err.message)
}
return false
}
}
2 changes: 2 additions & 0 deletions lib/lifecycles/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const checkpoint = require('../checkpoint')
const conventionalRecommendedBump = require('conventional-recommended-bump')
const figures = require('figures')
const fs = require('fs')
const Gitignore = require('../gitignore')
const path = require('path')
const runLifecycleScript = require('../run-lifecycle-script')
const semver = require('semver')
Expand Down Expand Up @@ -140,6 +141,7 @@ function updateConfigs (args, newVersion) {
configsToUpdate[path.resolve(process.cwd(), './bower.json')] = false
Object.keys(configsToUpdate).forEach(function (configPath) {
try {
if (Gitignore(configPath)) return
var stat = fs.lstatSync(configPath)
if (stat.isFile()) {
var config = require(configPath)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"homepage": "https://github.com/conventional-changelog/standard-version#readme",
"dependencies": {
"@bcoe/dotignore": "^0.1.1",
"chalk": "^1.1.3",
"conventional-changelog": "^1.1.0",
"conventional-recommended-bump": "^1.0.0",
Expand Down
73 changes: 73 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,32 @@ describe('cli', function () {
var pkgJson = fs.readFileSync('package.json', 'utf-8')
pkgJson.should.equal(['{', ' "version": "1.1.0"', '}', ''].join('\n'))
})

it('exits with error code if "scripts" is not an object', () => {
writePackageJson('1.0.0', {
'standard-version': {
scripts: 'echo hello'
}
})

commit('feat: first commit')
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/scripts must be an object/)
})

it('exits with error code if "skip" is not an object', () => {
writePackageJson('1.0.0', {
'standard-version': {
skip: true
}
})

commit('feat: first commit')
var result = execCli()
result.code.should.equal(1)
result.stderr.should.match(/skip must be an object/)
})
})

describe('standard-version', function () {
Expand Down Expand Up @@ -765,4 +791,51 @@ describe('standard-version', function () {
})
})
})

describe('.gitignore', () => {
const libGitignorePath = path.join(__dirname, 'lib', 'gitignore.js')

beforeEach(function () {
writeBowerJson('1.0.0')
})

it('does not update files present in .gitignore', (done) => {
fs.writeFileSync('.gitignore', 'bower.json', 'utf-8')

commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({silent: true})
.then(() => {
JSON.parse(fs.readFileSync('bower.json', 'utf-8')).version.should.equal('1.0.0')
getPackageVersion().should.equal('1.1.0')
return done()
})
})

it('silently ignores a missing .gitignore file', () => {
shell.rm('-f', '.gitignore')
delete require.cache[require.resolve(libGitignorePath)]
const consoleWarn = console.warn
let capturedWarning = '<NOTHING>'
console.warn = msg => { capturedWarning = msg }
const shouldBeFalse = require(libGitignorePath)(libGitignorePath)
console.warn = consoleWarn
shouldBeFalse.should.equal(false)
capturedWarning.should.equal('<NOTHING>')
})

it('logs a warning if .gitignore is a directory (code coverage)', () => {
shell.mkdir('.gitignore')
delete require.cache[require.resolve(libGitignorePath)]
const consoleWarn = console.warn
let capturedWarning = ''
console.warn = msg => { capturedWarning = msg }
const shouldBeFalse = require(libGitignorePath)(libGitignorePath)
console.warn = consoleWarn
shell.rm('-rf', '.gitignore')
shouldBeFalse.should.equal(false)
capturedWarning.should.match(/EISDIR/)
})
})
})