forked from Superalgos/Superalgos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupPlugins.js
151 lines (132 loc) · 6.6 KB
/
setupPlugins.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
run()
async function run() {
let username = process.argv[2]
let token = process.argv[3]
let ENVIRONMENT = require('./Environment.js')
let ENVIRONMENT_MODULE = ENVIRONMENT.newEnvironment()
global.env = ENVIRONMENT_MODULE
// Save github credentials for later
const credentials = {
"githubUsername": username,
"githubToken": token
}
const fs = require("fs")
let secretsDir = "./My-Secrets"
// Make sure My-Secrets has been created
if (!fs.existsSync(secretsDir)) {
fs.mkdirSync(secretsDir)
}
fs.writeFile(secretsDir + "/githubCredentials.json", JSON.stringify(credentials), function(err) {
if(err) {
console.log('[ERROR] Github Credentials were not saved correctly ' + err)
}
} )
await forkPluginRepos(username, token)
clonePluginRepos(username)
async function forkPluginRepos(username, token) {
return new Promise(promiseWork)
async function promiseWork(resolve, reject) {
if (username === undefined || token === undefined) {
console.log('[WARN] You need to provide your Github username and token in order for this script to Fork the Plugins repositories into your acccount for you.')
console.log('[WARN] Add your user name and token to the parameters of this script and try again please. ')
resolve()
return
}
const Octokit = require('@octokit/rest').Octokit
const octokit = new Octokit({
auth: token,
userAgent: 'Superalgos'
})
let reponsesCount = 0
for (const propertyName in global.env.PROJECT_PLUGIN_MAP) {
let repo = global.env.PROJECT_PLUGIN_MAP[propertyName].repo
await octokit.repos.get({
owner: username,
repo: repo,
})
.then(async err => {
console.log('[INFO] Dont need to fork plugin repo Superalgos/' + repo + ' because a fork already exists at the ' + username + ' account.')
reponsesCount++
if (reponsesCount === Object.keys(global.env.PROJECT_PLUGIN_MAP).length) { resolve() }
})
.catch(async err => {
await octokit.repos.createFork({
owner: 'Superalgos',
repo: repo
})
console.log('[INFO] Plugin repo Superalgos/' + repo + ' forked into the ' + username + ' account.')
reponsesCount++
if (reponsesCount === Object.keys(global.env.PROJECT_PLUGIN_MAP).length) { resolve() }
})
}
}
}
async function clonePluginRepos(username) {
if (username === undefined) {
console.log('[WARN] You need to provide your Github username for this script to Clone the Plugins repositories forks from your acccount.')
console.log('[WARN] Add your user name to the parameters of this script and try again please. ')
return
}
const { exec } = require("child_process")
const path = require("path")
const fs = require('fs');
/*
Here we will clone all the Plugins Repos if they do not exist.
*/
for (const propertyName in global.env.PROJECT_PLUGIN_MAP) {
let cloneDir = path.join(global.env.PATH_TO_PLUGINS, global.env.PROJECT_PLUGIN_MAP[propertyName].dir)
let repoURL = 'https://github.com/' + username + '/' + global.env.PROJECT_PLUGIN_MAP[propertyName].repo
if (fs.existsSync(cloneDir)) {
console.log(' ')
console.log('[INFO] Directory ' + cloneDir + ' already exists.')
console.log('[INFO] No need to clone repo ' + repoURL)
continue
}
console.log(' ')
console.log('[INFO] Cloning plugin repo from ' + repoURL + ' into ' + cloneDir)
await cloneTheRepo()
async function cloneTheRepo() {
return new Promise(promiseWork)
async function promiseWork(resolve, reject) {
exec('git clone ' + repoURL + ' ' + global.env.PROJECT_PLUGIN_MAP[propertyName].dir + ' --branch develop',
{
cwd: path.join(global.env.PATH_TO_PLUGINS)
},
async function (error) {
if (error) {
console.log('')
console.log("[ERROR] There was an error cloning the plugin this repo. ");
console.log('')
console.log(error)
} else {
console.log('[INFO] Cloning repo ' + global.env.PROJECT_PLUGIN_MAP[propertyName].repo + ' succeed.')
/*
Final step is to set the remote to the main Superalgos account.
*/
const simpleGit = require("simple-git")
const options = {
baseDir: cloneDir,
binary: 'git',
maxConcurrentProcesses: 6,
}
const git = simpleGit(options)
await git.addRemote('upstream', `https://github.com/Superalgos/${global.env.PROJECT_PLUGIN_MAP[propertyName].repo}`)
.then(remoteSetSuccesfully)
.catch(errorSettingRemote)
function remoteSetSuccesfully() {
console.log('[INFO] Setup of repo ' + global.env.PROJECT_PLUGIN_MAP[propertyName].repo + ' succeed.')
resolve()
}
function errorSettingRemote(err) {
console.log('[ERROR] Setup of repo ' + global.env.PROJECT_PLUGIN_MAP[propertyName].repo + ' failed. You will need to set the git remote manually.')
console.log('')
console.log(err)
resolve()
}
}
})
}
}
}
}
}