Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
feat(global): add --global flag for global installation
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Dec 3, 2017
1 parent 00d0f15 commit b9c131f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -8,6 +8,11 @@

To start using it right away:

```
$ npx npm-merge-driver install --global
```

**Or** install it locally, per-project:
```
$ cd /path/to/git/repository
$ npx npm-merge-driver install
Expand Down
46 changes: 38 additions & 8 deletions index.js
Expand Up @@ -3,6 +3,7 @@

const cp = require('child_process')
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
const yargs = require('yargs')

Expand All @@ -16,6 +17,11 @@ function parseArgs () {
'install',
'Set up the merge driver in the current git repository.',
{
global: {
type: 'boolean',
default: false,
description: 'install to your user-level git configuration'
},
driver: {
type: 'string',
default: 'npx npm-merge-driver merge %A %O %B %P',
Expand Down Expand Up @@ -58,17 +64,41 @@ function parseArgs () {
}

function configureGit (argv) {
let attrFile
let opts = ''
if (argv.global) {
opts = '--global'
try {
attrFile = cp
.execSync(`git config --global core.attributesfile`)
.toString('utf8')
.trim()
} catch (e) {}
if (!attrFile) {
if (process.env.XDG_CONFIG_HOME) {
attrFile = path.join(process.env.XDG_CONFIG_HOME, 'git', 'attributes')
} else {
attrFile = path.join(process.env.HOME, '.config', 'git', 'attributes')
}
}
} else {
opts = '--local'
const gitDir = cp
.execSync(`git rev-parse --git-dir`, {
encoding: 'utf8'
})
.trim()
attrFile = path.join(gitDir, 'info', 'attributes')
}
cp.execSync(
`git config ${opts} merge."${argv.driverName}".name "automatically merge npm lockfiles"`
)
cp.execSync(
`git config merge."${argv.driverName}".name "automatically merge npm lockfiles"`
`git config ${opts} merge."${argv.driverName}".driver "${argv.driver}"`
)
cp.execSync(`git config merge."${argv.driverName}".driver "${argv.driver}"`)
const gitDir = cp
.execSync(`git rev-parse --git-dir`, {
encoding: 'utf8'
})
.trim()
mkdirp.sync(path.dirname(attrFile))
fs.appendFileSync(
path.join(gitDir, 'info', 'attributes'),
attrFile,
'\n' + argv.files.map(f => `${f} merge=${argv.driverName}`).join('\n')
)
}
Expand Down
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -42,6 +42,7 @@
},
"license": "ISC",
"dependencies": {
"mkdirp": "^0.5.1",
"yargs": "^10.0.3"
},
"devDependencies": {
Expand Down Expand Up @@ -71,6 +72,7 @@
]
},
"bundleDependencies": [
"yargs"
"yargs",
"mkdirp"
]
}

0 comments on commit b9c131f

Please sign in to comment.