-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-install.js
125 lines (101 loc) · 3.19 KB
/
git-install.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
const fs = require('fs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const package = require('./package.json')
const { dependencies, gitDependencies } = package
const dir = 'git-deps'
let clc = {}
for (const [key, code] of Object.entries({
cyanBright: '\x1b[36m',
yellowBright: '\x1b[33m',
greenBright: '\x1b[32m',
})) {
clc[key] = (...args) => {
return code + [...args].join(' ') + '\x1b[0m'
}
}
const logDep = (color, ...args) => {
let msg = [' -', ...args]
if (color) {
msg = [clc[color](...msg)]
}
console.log(...msg)
}
async function main() {
const { argv } = process
if (argv[2] === '-c' || argv[2] === '--cleanup') {
const files = await fs.promises.readdir(dir)
for (const f of files) {
if (f !== '.gitignore') {
fs.rmSync(dir + '/' + f, { recursive: true, force: true })
}
}
return
}
if (!gitDependencies) {
console.log('No gitDependencies in package.json, so nothing to preinstall.')
return
}
const lockFile = 'git-install-lock'
if (fs.existsSync(lockFile)) {
console.error(lockFile, 'exists, so cannot run. It is for recursion protection.')
return
}
fs.writeFileSync(lockFile, '1')
console.log(clc.cyanBright('preinstalling deps...'))
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
process.chdir(dir)
const gitDeps = Object.entries(gitDependencies)
const deps = {}
for (const [ key, dep ] of gitDeps) {
const url = new URL(dep)
const { pathname } = url
const [ repo, branchFolder ] = pathname.split('/tree/')
let branch = '', folder = ''
if (branchFolder) {
const parts = branchFolder.split('/')
branch = ' -b ' + parts[0]
if (parts[1]) {
parts.shift()
folder = parts.join('/')
}
}
const commit = url.hash.replace('#', '')
const repoName = repo.split('/')[2]
deps[key] = { dep, repo, branch, folder, repoName, commit }
if (fs.existsSync(repoName)) {
logDep('yellowBright', repoName, 'already cloned, using as cache.')
continue
}
deps[key].cloned = true
const clone = 'git clone ' + url.origin + repo + branch
logDep('greenBright', clone)
await exec(clone)
if (commit) {
process.chdir(repoName)
const resetTo = 'git reset --hard ' + commit
logDep(null, '-', resetTo)
await exec(resetTo)
process.chdir('..')
}
}
console.log(' ')
console.log(clc.cyanBright('yarn-adding cloned deps (if not added)...'))
for (const [ key, dep ] of Object.entries(deps)) {
let path = './' + dir + '/' + dep.repoName
if (dep.folder) {
path += '/' + dep.folder
}
if (argv[2] === '-f' || dependencies[key] !== path) {
const add = 'yarn add ' + path
logDep('greenBright', add)
await exec(add)
}
}
console.log(clc.greenBright('ok'))
console.log('')
fs.unlinkSync('../' + lockFile)
}
main()