-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepoMethods.js
126 lines (107 loc) · 2.96 KB
/
repoMethods.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
const util = require("util");
const fs = require("fs");
const exec = util.promisify(require("child_process").exec);
const _ = require("lodash");
const logger = require("pino")({
prettyPrint: { colorize: true },
translateTime: false
});
const hasYarn = require("has-yarn");
const {
fileExists,
getRepoFolder,
executeCommand,
executeCommandSync,
executeCommandGetOutput
} = require("./utilities");
/**
* repoMethods
*
* Contains all methods related with direct handling of repositories.
*
* @author Luis Cardoso ([email protected])
* @author Henrique Dias ([email protected])
*/
/**
* Clones the given `repo` using git into the tmp folder.
*
* @param {Object} repo
*/
async function cloneRepo(repo) {
const repoUrl = repo.gitRepoUrl;
const branch = repo.targetBranch;
const repoFolder = getRepoFolder(repo);
const folderExists = await fileExists(repoFolder);
if (!fs.existsSync("./tmp")) {
fs.mkdirSync("./tmp");
}
let command;
if (folderExists) {
command = `cd ${repoFolder} && git checkout -f && git checkout ${branch} && git pull --rebase`;
} else {
command = `cd tmp && git clone ${repoUrl} --branch ${branch}`;
}
const { stdout, stderr } = await exec(command);
// TODO improve logger output
if (stdout) {
logger.info(`${repo.label} cloned successfully`);
} else if (stderr) {
logger.error(`error cloning ${repo.label}`);
}
}
/**
* Calculates all the metrics for the given `repo`
*
* @param {Object} repo
* @returns {Object}
*/
async function installRepo(repo) {
const folder = getRepoFolder(repo);
let result;
if (hasYarn(folder)) {
result = await executeCommand(folder, "yarn install");
} else {
result = await executeCommand(folder, "npm install");
}
if (!result) {
logger.error(`Error installing ${repo.label} or already installed`);
return null;
}
logger.info(`${repo.label} installed successfully`);
return true;
}
/**
* Install the given `repo`
* @param {object} repo
* @param {string} folder name
*/
async function installRepoSync(repo, folder) {
let result;
if (hasYarn(folder)) {
result = await executeCommandSync(folder, "rm -rf node_modules && yarn install");
} else {
result = executeCommandSync(folder, "npm install");
}
if (!result) {
logger.error(`Error installing ${repo.label} or already installed`);
return null;
}
logger.info(`${repo.label} installed successfully`);
return true;
}
/**
* Returns the repository name
* @param {string} location
* @returns {string}
*/
function getRepoName(location) {
return executeCommandGetOutput(location, "git config --get remote.origin.url").then((name) => {
return _.upperFirst(_.last(name.split("/")).replace(".git", "")).replace("\n", "");
});
}
module.exports = {
cloneRepo,
installRepo,
installRepoSync,
getRepoName
};