-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.ts
130 lines (104 loc) · 3.3 KB
/
git.ts
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
import { writeFileSync, existsSync, mkdirSync, rmSync, readFileSync } from 'fs'
import { join } from 'path'
import { spawnSync } from 'child_process'
import { basePath, log, replaceIndexLinesFromPatch } from './helper'
import { pluginGitignore } from './configuration/gitignore'
import { applyPatch } from './apply'
const createGitShell =
(cwd = join(basePath(), '.numic')) =>
(...args: string[]) =>
spawnSync('git', args, {
cwd,
env: { ...process.env, HOME: 'numic' },
maxBuffer: 1024 * 1024 * 100,
})
export const initializeRepository = () => {
const git = createGitShell()
git('init')
git('config', '--local', 'user.name', 'numic')
git('config', '--local', 'user.email', 'numic@reactnative.dev')
writeFileSync(join(basePath(), '.numic/.gitignore'), pluginGitignore())
git('add', '.')
git('commit', '--allow-empty', '-m', 'Initial commit (fresh native Android and iOS folders.')
}
// Remove uncommitted changes.
export const resetRepository = () => {
if (!existsSync(join(basePath(), '.numic', '.git'))) {
return
}
const git = createGitShell()
git('reset', 'HEAD', '--hard') // Without hard keeps changes unstaged.
}
// Commit changes from newly installed plugins.
export const commitChanges = () => {
if (!existsSync(join(basePath(), '.numic', '.git'))) {
return
}
const git = createGitShell()
git('add', '.')
git('commit', '--allow-empty', '-m', 'Possible changes.')
}
export const createPatch = () => {
const git = createGitShell()
git('add', '.') // Includes modifications, additions and removals.
const diffResult = git(
'diff',
'--cached',
'--no-color',
'--ignore-space-at-eol',
'--no-ext-diff',
'--binary'
)
const patchFileName = join(basePath(), 'patch/current.patch')
const patchContents = replaceIndexLinesFromPatch(diffResult.stdout.toString())
if (patchContents) {
const patchUpdated = existsSync(patchFileName)
if (patchUpdated) {
const existingPatchContents = readFileSync(patchFileName, 'utf-8')
if (existingPatchContents !== patchContents) {
writeFileSync(patchFileName, patchContents)
log('Patch updated in patch/current.patch')
}
} else {
const patchFolder = join(basePath(), 'patch')
if (!existsSync(patchFolder)) {
mkdirSync(patchFolder, { recursive: true })
}
writeFileSync(patchFileName, patchContents)
log('Patch created in patch/current.patch')
}
} else {
if (existsSync(patchFileName)) {
rmSync(patchFileName)
}
log('No changes to patch found')
}
resetRepository()
}
export const apply = ({
skipEmpty,
location = basePath(),
}: {
skipEmpty?: boolean
location?: string
}) => {
const git = createGitShell(location)
let temporaryGitCreated = false
if (!existsSync(join(basePath(), 'patch/current.patch'))) {
if (!skipEmpty) {
log('No patch found, run "numic patch" to create a patch', 'error')
}
return
}
const repositoryPath = join(basePath(), '.git')
if (!existsSync(repositoryPath)) {
git('init')
git('config', '--local', 'user.name', 'numic')
git('config', '--local', 'user.email', 'numic@reactnative.dev')
temporaryGitCreated = true
}
applyPatch(location, git)
if (temporaryGitCreated) {
rmSync(repositoryPath, { recursive: true })
}
}