UNPKG

1.21 kBJavaScriptView Raw
1const child_process = require('child_process');
2const path = require('path');
3
4
5function getBranch(tag, git) {
6 let branch = '';
7 if (tag === 'auto') {
8 try {
9 branch = child_process.execSync(
10 `git tag --sort -version:refname`,
11 { cwd: git }
12 ).toString().split('\n')[1];
13 } catch (e) {
14 throw new Error('Failed to get tag automatically, please specify manually by `tag:tagName` in `option.tag`');
15 }
16 } else if (/^(tag|branch):/.test(tag)) {
17 branch = tag.replace(/^(tag|branch):/, '');
18 }
19 return branch;
20}
21
22exports = module.exports = function createDiff(option) {
23 let branch = getBranch(option.tag, option.git);
24 if (!branch) {
25 throw new Error('Invalid argument: `option.tag` must be auto|tag:tagName|branch:branchName');
26 }
27
28 try {
29 child_process.execSync(
30 `git clone -l -b ${branch} ${option.git} ${option.dir} >& /dev/null`
31 );
32 } catch (e) {
33 throw new Error(`Git branch ${branch} not exist.`);
34 }
35
36 console.log(`diff branch: ${branch}`);
37 return path.join(
38 option.dir,
39 option.root.replace(option.git, '')
40 );
41};