This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
executable file
·224 lines (217 loc) · 5.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
'use strict'
const cp = require('child_process')
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
const yargs = require('yargs')
if (require.main === module) {
parseArgs()
}
function parseArgs () {
return yargs
.command(
'install',
'Set up the merge driver in the current git repository.',
{
legacy: {
type: 'boolean',
default: true,
description:
'when the merge driver errors, it will retry the command after resolving the merge conflict with --theirs'
},
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',
description:
'string to install as the driver in the git configuration'
},
'driver-name': {
type: 'string',
default: 'npm-merge-driver',
description:
'String to use as the merge driver name in your configuration.'
},
files: {
description: 'Filenames that will trigger this driver.',
type: 'array',
default: ['npm-shrinkwrap.json', 'package-lock.json']
}
},
install
)
.command(
'uninstall',
'Remove a previously configured driver',
{
global: {
type: 'boolean',
default: false,
description: 'install to your user-level git configuration'
},
'driver-name': {
type: 'string',
default: 'npm-merge-driver',
description:
'String to use as the merge driver name in your configuration.'
}
},
uninstall
)
.command(
'merge <%A> <%O> <%B> <%P>',
'Check for lockfile conflicts and correct them if necessary.',
{
command: {
alias: 'c',
description: 'Command to execute to resolve conflicts.',
type: 'string',
default: 'npm install --package-lock-only'
},
legacy: {
type: 'boolean',
default: true,
description:
'If <command> errors, it will be re-run after checking out the --theirs version of the file, with no granular merging.'
}
},
merge
)
.version(require('./package.json').version)
.alias('version', 'v')
.help()
.alias('help', 'h')
.epilogue('For the full documentation, see npm-merge-driver(1)')
.demandCommand().argv
}
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 "automatically merge npm lockfiles"`
)
cp.execSync(
`git config ${opts} merge."${argv.driverName}".driver "${argv.driver}${!argv.legacy
? ' --no-legacy'
: ''}"`
)
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')
} catch (e) {}
if (attrContents && !attrContents.match(/[\n\r]$/g)) {
attrContents = '\n'
}
attrContents += argv.files
.map(f => `${f} merge=${argv.driverName}`)
.join('\n')
attrContents += '\n'
fs.writeFileSync(attrFile, attrContents)
console.error(
'npm-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
}
}
let currAttrs
try {
currAttrs = fs.readFileSync(attrFile, 'utf8').split('\n')
} 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)
}
}
function findAttributes (argv) {
let attrFile
if (argv.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 {
const gitDir = cp
.execSync(`git rev-parse --git-dir`, {
encoding: 'utf8'
})
.trim()
attrFile = path.join(gitDir, 'info', 'attributes')
}
return attrFile
}
function merge (argv) {
console.error('npm-merge-driver: merging', argv['%P'])
const ret = cp.spawnSync(
'git',
['merge-file', '-p', argv['%A'], argv['%O'], argv['%B']],
{
stdio: [0, 'pipe', 2]
}
)
fs.writeFileSync(argv['%P'], ret.stdout)
try {
cp.execSync(argv.command, {
stdio: 'inherit',
cwd: path.dirname(argv['%P'])
})
} catch (e) {
if (!argv.legacy) {
throw e
}
fs.writeFileSync(argv['%P'], fs.readFileSync(argv['%B']))
console.error(
'npm-merge-driver: --legacy enabled. Checking out --theirs and retrying merge.'
)
console.error('npm-merge-driver: !!!SOME CHANGES MAY BE LOST!!!')
cp.execSync(argv.command, {
stdio: 'inherit',
cwd: path.dirname(argv['%P'])
})
}
fs.writeFileSync(argv['%A'], fs.readFileSync(argv['%P']))
console.error('npm-merge-driver:', argv['%P'], 'successfully merged.')
}