Skip to content

Commit

Permalink
feat: auto install if yarn or npm is used
Browse files Browse the repository at this point in the history
  • Loading branch information
jayree committed Nov 22, 2019
1 parent c673f7b commit a3813fc
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 122 deletions.
171 changes: 101 additions & 70 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict'

const cp = require('child_process')
var shell = require('shelljs')
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
Expand All @@ -13,6 +13,9 @@ const builder = new xml2js.Builder({
xmlns: true
})

const PackageJson = require('./utils/package-json')
const pjson = new PackageJson()

// eslint-disable-next-line no-extend-native
String.prototype.replaceAll = function (search, replacement) {
return this.split(search).join(replacement)
Expand Down Expand Up @@ -93,84 +96,110 @@ function parseArgs () {
}

function install (argv) {
const attrFile = findAttributes(argv).replace(
/^\s*~\//,
process.env.HOME + '/'
)
const opts = argv.global ? '--global' : '--local'
cp.execSync(
`git config ${opts} merge."${
argv.driverName
}".name "A custom merge driver for Salesforce profiles"`
)
cp.execSync(
`git config ${opts} merge."${argv.driverName}".driver "${argv.driver}"`
)
cp.execSync(`git config ${opts} merge."${argv.driverName}".recursive binary`)
mkdirp.sync(path.dirname(attrFile))
let attrContents = ''
try {
const RE = new RegExp(`.* merge\\s*=\\s*${argv.driverName}$`)
attrContents = fs
.readFileSync(attrFile, 'utf8')
.split(/\r?\n/)
.filter(line => !line.match(RE))
if (pjson.name !== 'sfdx-md-merge-driver') {
const attrFile = path.join(
pjson.path,
findAttributes(argv).replace(/^\s*~\//, process.env.HOME + '/')
)
const opts = argv.global ? '--global' : '--local'
shell.exec(
`git config ${opts} merge."${
argv.driverName
}".name "A custom merge driver for Salesforce profiles"`,
{
cwd: pjson.path
}
)
shell.exec(
`git config ${opts} merge."${argv.driverName}".driver "${argv.driver}"`,
{
cwd: pjson.path
}
)
shell.exec(
`git config ${opts} merge."${argv.driverName}".recursive binary`,
{
cwd: pjson.path
}
)
mkdirp.sync(path.dirname(attrFile))
let attrContents = ''
try {
const RE = new RegExp(`.* merge\\s*=\\s*${argv.driverName}$`)
attrContents = fs
.readFileSync(attrFile, 'utf8')
.split(/\r?\n/)
.filter(line => !line.match(RE))
.join('\n')
// eslint-disable-next-line no-empty
} catch (e) {}
if (attrContents && !attrContents.match(/[\n\r]$/g)) {
attrContents = '\n'
}
attrContents += argv.files
.map(f => `${f} merge=${argv.driverName}`)
.join('\n')
// eslint-disable-next-line no-empty
} catch (e) {}
if (attrContents && !attrContents.match(/[\n\r]$/g)) {
attrContents = '\n'
attrContents += '\n'
fs.writeFileSync(attrFile, attrContents)
console.error(
'sfdx-md-merge-driver:',
argv.driverName,
'installed to `git config',
opts + '`',
'and',
attrFile
)
}
attrContents += argv.files
.map(f => `${f} merge=${argv.driverName}`)
.join('\n')
attrContents += '\n'
fs.writeFileSync(attrFile, attrContents)
console.error(
'sfdx-md-merge-driver:',
argv.driverName,
'installed to `git config',
opts + '`',
'and',
attrFile
)
}

function uninstall (argv) {
const attrFile = findAttributes(argv)
const opts = argv.global ? '--global' : '--local'
try {
cp.execSync(
`git config ${opts} --remove-section merge."${argv.driverName}"`
)
} catch (e) {
if (!e.message.match(/no such section/gi)) {
throw e
if (pjson.name !== 'sfdx-md-merge-driver') {
const attrFile = path.join(pjson.path, findAttributes(argv))
const opts = argv.global ? '--global' : '--local'
try {
shell.exec(
`git config ${opts} --remove-section merge."${argv.driverName}"`,
{
cwd: pjson.path,
silent: true
}
)
} catch (e) {}
let currAttrs
try {
currAttrs = fs.readFileSync(attrFile, 'utf8').split('\n')
// eslint-disable-next-line no-empty
} catch (e) {}
if (currAttrs) {
let newAttrs = ''
currAttrs.forEach(attr => {
const match = attr.match(/ merge=(.*)$/i)
if (!match || match[1].trim() !== argv.driverName) {
newAttrs += attr + '\n'
}
})
fs.writeFileSync(attrFile, newAttrs.trim())
}
}
let currAttrs
try {
currAttrs = fs.readFileSync(attrFile, 'utf8').split('\n')
// eslint-disable-next-line no-empty
} catch (e) {}
if (currAttrs) {
let newAttrs = ''
currAttrs.forEach(attr => {
const match = attr.match(/ merge=(.*)$/i)
if (!match || match[1].trim() !== argv.driverName) {
newAttrs += attr + '\n'
}
})
fs.writeFileSync(attrFile, newAttrs.trim())
console.error(
'sfdx-md-merge-driver:',
argv.driverName,
'uninstalled from `git config',
opts + '`',
'and',
attrFile
)
}
}

function findAttributes (argv) {
let attrFile
if (argv.global) {
try {
attrFile = cp
.execSync('git config --global core.attributesfile')
attrFile = shell
.exec('git config --global core.attributesfile', {
cwd: pjson.path,
silent: true
})
.toString('utf8')
.trim()
// eslint-disable-next-line no-empty
Expand All @@ -183,9 +212,11 @@ function findAttributes (argv) {
}
}
} else {
const gitDir = cp
.execSync('git rev-parse --git-dir', {
encoding: 'utf8'
const gitDir = shell
.exec('git rev-parse --git-dir', {
encoding: 'utf8',
cwd: pjson.path,
silent: true
})
.trim()
attrFile = path.join(gitDir, 'info', 'attributes')
Expand All @@ -194,7 +225,7 @@ function findAttributes (argv) {
}

function merge (argv) {
const md = new (require('./lib/metadataMerger'))(
const md = new (require('./utils/metadataMerger'))(
argv['%O'],
argv['%A'],
argv['%B']
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"files": [
"*.js",
"sfdx-md-merge-driver.1",
"/lib",
"/utils",
"/conf"
],
"man": "./sfdx-md-merge-driver.1",
Expand All @@ -17,7 +17,9 @@
"postrelease": "npm publish && git push --follow-tags",
"pretest": "standard",
"release": "standard-version",
"test": "tap -J --coverage test/*.js"
"test": "tap -J --coverage test/*.js",
"install": "node ./index install",
"uninstall": "node ./index uninstall"
},
"repository": "https://github.com/jayree/sfdx-md-merge-driver",
"keywords": [
Expand All @@ -35,6 +37,7 @@
"license": "ISC",
"dependencies": {
"mkdirp": "^0.5.1",
"pkg-dir": "4.2.0",
"shelljs": "0.8.3",
"xml2js": "0.4.22",
"yargs": "^15.0.2"
Expand All @@ -54,7 +57,7 @@
"prettier-standard": "^15.0.1",
"standard": "^14.3.1",
"standard-version": "^7.0.1",
"tap": "^14.9.2"
"tap": "^14.10.1"
},
"config": {
"nyc": {
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions utils/package-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path')
const pkgDir = require('pkg-dir')

class PackageJson {
constructor () {
this.path =
pkgDir.sync(path.join(__dirname, '..', '..')) ||
process.env.INIT_CWD ||
process.cwd()
this.name = require(path.join(this.path, 'package.json')).name
}
}

module.exports = PackageJson
Loading

0 comments on commit a3813fc

Please sign in to comment.