UNPKG

4.85 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.add = add;
7exports.cloneInto = cloneInto;
8exports.commit = commit;
9exports.findLatestFileCommitHash = findLatestFileCommitHash;
10exports.getDefinitionsDiff = getDefinitionsDiff;
11exports.init = init;
12exports.rebaseRepoMainline = rebaseRepoMainline;
13exports.setLocalConfig = setLocalConfig;
14
15var _which = _interopRequireDefault(require("which"));
16
17var _simpleGit = _interopRequireDefault(require("simple-git"));
18
19var _node = require("./node");
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23const mainlineBranch = 'main';
24
25const isMainlineBranch = async cacheDirPath => {
26 const git = (0, _simpleGit.default)().cwd(cacheDirPath);
27 return (await git.branch()).current === mainlineBranch;
28};
29
30const checkoutToMainlineBranch = async cacheDirPath => {
31 const git = (0, _simpleGit.default)().cwd(cacheDirPath);
32
33 try {
34 await git.fetch('origin', 'main');
35 await git.checkout('main');
36 } catch (e) {
37 throw new Error(`Error checking out the \`${mainlineBranch}\` branch of the following repo:\n` + `${cacheDirPath}\n\n${e}`);
38 }
39};
40
41async function getGitPath() {
42 try {
43 return await which('git');
44 } catch (e) {
45 throw new Error(`Unable to find ${'`'}git${'`'} installed on this system: ${e.message}`);
46 }
47}
48
49function which(executable) {
50 return new Promise((res, rej) => {
51 (0, _which.default)(executable, (err, resolvedPath) => {
52 if (err) {
53 rej(err);
54 } else {
55 res(resolvedPath);
56 }
57 });
58 });
59}
60
61async function add(repoPath, pathToAdd) {
62 const gitPath = await getGitPath();
63
64 try {
65 await _node.child_process.spawnP(gitPath, ['add', pathToAdd], {
66 cwd: repoPath
67 });
68 } catch (e) {
69 throw new Error(`Error adding staged file(s) to git repo: ${e.message}`);
70 }
71}
72
73async function commit(dirPath, message) {
74 const gitPath = await getGitPath();
75
76 try {
77 await _node.child_process.spawnP(gitPath, ['commit', '-a', '-m', message], {
78 cwd: dirPath
79 });
80 } catch (e) {
81 console.error(e); //throw new Error(`Error creating a commit in git repo: ${e.message}`);
82 }
83}
84
85async function setLocalConfig(dirPath, name, value) {
86 const gitPath = await getGitPath();
87
88 try {
89 await _node.child_process.spawnP(gitPath, ['config', name, JSON.stringify(value)], {
90 cwd: dirPath
91 });
92 } catch (e) {
93 console.error(e); //throw new Error(`Error creating a commit in git repo: ${e.message}`);
94 }
95}
96
97async function getDefinitionsDiff() {
98 const gitPath = await getGitPath();
99
100 try {
101 // const {stdout: branchName} = await child_process.spawnP(gitPath, [
102 // 'rev-parse',
103 // '--abbrev-ref',
104 // 'HEAD',
105 // ]);
106 let {
107 stdout
108 } = await _node.child_process.spawnP(gitPath, ['diff', `origin/${mainlineBranch}`, '--name-only']);
109 console.log(stdout);
110
111 if (stdout.split('\n').filter(o => o.startsWith('definitions/')).length === 0) {
112 // We are probably already on mainline, so compare to the last commit.
113 const {
114 stdout: headDiff
115 } = await _node.child_process.spawnP(gitPath, ['diff', 'HEAD~1', '--name-only']);
116 stdout = headDiff;
117 }
118
119 return stdout.split('\n');
120 } catch (e) {
121 console.error('Unable to find diffs!');
122 console.error(e);
123 return [];
124 }
125}
126
127async function cloneInto(gitURL, destDirPath) {
128 const gitPath = await getGitPath();
129
130 try {
131 await _node.child_process.spawnP(gitPath, ['clone', gitURL, destDirPath]);
132
133 if (!(await isMainlineBranch(destDirPath))) {
134 await checkoutToMainlineBranch(destDirPath);
135 }
136 } catch (e) {
137 throw new Error(`Error cloning repo: ${e.message}`);
138 }
139}
140
141async function init(dirPath) {
142 const gitPath = await getGitPath();
143
144 try {
145 await _node.child_process.spawnP(gitPath, ['init'], {
146 cwd: dirPath
147 });
148 } catch (e) {
149 throw new Error(`Error init-ing git repo: ${e.message}`);
150 }
151}
152
153async function findLatestFileCommitHash(repoPath, filePath) {
154 const gitPath = await getGitPath();
155
156 try {
157 const {
158 stdout
159 } = await _node.child_process.spawnP(gitPath, ['log', '--pretty=%H', filePath], {
160 cwd: repoPath
161 });
162 return stdout.trim();
163 } catch (e) {
164 throw new Error(`Error finding latest commit hash for ${filePath}: ${e.message}`);
165 }
166}
167
168async function rebaseRepoMainline(repoDirPath) {
169 const gitPath = await getGitPath();
170
171 if (!(await isMainlineBranch(repoDirPath))) {
172 await checkoutToMainlineBranch(repoDirPath);
173 }
174
175 try {
176 await _node.child_process.execFileP(gitPath, ['pull', '--rebase'], {
177 cwd: repoDirPath
178 });
179 } catch (e) {
180 const {
181 stderr
182 } = e;
183 throw new Error(`Error rebasing the \`${mainlineBranch}\` branch of the following repo:\n` + `${repoDirPath}\n\n${stderr}`);
184 }
185}
\No newline at end of file