-
Notifications
You must be signed in to change notification settings - Fork 26
/
repository.ts
387 lines (348 loc) · 9.61 KB
/
repository.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import fs from "fs"
import * as git from "isomorphic-git"
import flatten from "lodash/flatten"
import orderBy from "lodash/orderBy"
import zipWith from "lodash/zipWith"
import path from "path"
import pify from "pify"
const j = path.join
export type Repository = {
fs: any
dir: string
}
export type FileInfo = {
name: string
gitStatus: string
type: "file" | "dir"
}
export type FileNode =
| {
type: "dir"
pathname: string
children: FileNode[]
}
| {
type: "file"
pathname: string
}
// https://isomorphic-git.github.io/docs/log.html
export type CommitDescription = {
oid: string // SHA1 object id of this commit
message: string // Commit message
tree: string // SHA1 object id of corresponding file tree
parent: string[] // an array of zero or more SHA1 object ids
author: {
name: string // The author's name
email: string // The author's email
timestamp: number // UTC Unix timestamp in seconds
timezoneOffset: number // Timezone difference from UTC in minutes
}
committer: {
name: string // The committer's name
email: string // The committer's email
timestamp: number // UTC Unix timestamp in seconds
timezoneOffset: number // Timezone difference from UTC in minutes
}
gpgsig?: string // PGP signature (if present)
}
export type GitRepositoryStatus = {
currentBranch: string
branches: string[]
stagingStatus: GitStagingStatus
trackingStatus: GitTrackingStatus
history: CommitDescription[]
}
export type GitStagingStatus = {
added: string[]
staged: string[]
modified: string[]
unmodified: string[]
}
export type GitTrackingStatus = { tracked: string[]; untracked: string[] }
/* READ */
export async function readFileStats(
projectRoot: string,
dirpath: string
): Promise<FileInfo[]> {
const filenames: string[] = await pify(fs.readdir)(dirpath)
const ret: any = await Promise.all(
filenames.map(async name => {
const childPath = j(dirpath, name)
const stat = await pify(fs.stat)(childPath)
const relpath = path.relative(projectRoot, childPath)
// const gitStatus = await getGitStatusInRepository(projectRoot, relpath)
return {
gitStatus: "nop",
name,
type: stat.isDirectory() ? "dir" : "file"
}
})
)
return orderBy(ret, [(s: FileInfo) => s.type + "" + s.name])
}
export async function getProjectGitStatus(
projectRoot: string
): Promise<GitRepositoryStatus> {
const trackingStatus = await getGitTrackingStatus(projectRoot)
const { tracked } = trackingStatus
const fileStatusList: Array<{
relpath: string
status: string
}> = await Promise.all(
tracked.map(async relpath => {
const status = await getGitStatusInRepository(projectRoot, relpath)
return { relpath, status }
})
)
const stagingStatus: GitStagingStatus = fileStatusList.reduce(
(acc: GitStagingStatus, fileStatus) => {
console.log("status:", fileStatus.relpath, fileStatus.status)
switch (fileStatus.status) {
case "*modified": {
return { ...acc, modified: [...acc.modified, fileStatus.relpath] }
}
case "added": {
return { ...acc, added: [...acc.added, fileStatus.relpath] }
}
// added and modified
case "*added": {
return {
...acc,
added: [...acc.added, fileStatus.relpath],
modified: [...acc.modified, fileStatus.relpath]
}
}
case "modified": {
return { ...acc, staged: [...acc.staged, fileStatus.relpath] }
}
case "unmodified": {
return { ...acc, unmodified: [...acc.unmodified, fileStatus.relpath] }
}
default: {
return acc
}
}
},
{
added: [],
modified: [],
staged: [],
unmodified: []
}
)
const currentBranch = await git.currentBranch({ fs, dir: projectRoot })
const branches = await git.listBranches({ fs, dir: projectRoot })
const history = await getLogInRepository(projectRoot, { ref: currentBranch })
return {
branches,
currentBranch,
stagingStatus,
trackingStatus,
history
}
}
export async function getGitTrackingStatus(
projectRoot: string
): Promise<GitTrackingStatus> {
// buffer
const untracked = []
const tracked = []
const gitFiles: string[] = await git.listFiles({ fs, dir: projectRoot })
const gitFilepathIndexed: { [s: string]: boolean } = gitFiles.reduce(
(acc: { [s: string]: boolean }, pathname: string) => {
const relpath = pathname.replace(projectRoot, "")
return { ...acc, [relpath]: true }
},
{}
)
const allFiles = await getFilesRecursively(projectRoot)
const relativeFilepaths = allFiles.map(pathname =>
pathname.replace(projectRoot + "/", "")
)
for (const relpath of relativeFilepaths) {
if (gitFilepathIndexed[relpath]) {
tracked.push(relpath)
} else {
untracked.push(relpath)
}
}
return {
tracked,
untracked
}
}
export async function getFilesRecursively(rootPath: string): Promise<string[]> {
const node = await readRecursiveFileNode(rootPath)
return nodeToFileList(node)
}
function nodeToFileList(node: FileNode): string[] {
if (node.type === "file") {
return [node.pathname]
} else if (node.type === "dir") {
const ret = node.children.map(childNode => {
return nodeToFileList(childNode)
})
return flatten(ret)
} else {
return []
}
}
const IGNORE_PATTERNS = [".git"]
export async function readRecursiveFileNode(
pathname: string
): Promise<FileNode> {
// console.log("current node", pathname)
const stat = await pify(fs.stat)(pathname)
if (stat.isDirectory()) {
const pathList: string[] = await pify(fs.readdir)(pathname)
const children = await Promise.all(
pathList
.filter(childPath => !IGNORE_PATTERNS.includes(childPath))
.map(childPath => readRecursiveFileNode(path.join(pathname, childPath)))
)
return {
children,
pathname,
type: "dir"
}
} else {
return {
pathname,
type: "file"
}
}
}
export async function getLogInRepository(
projectRoot: string,
{ depth, ref = "master" }: { depth?: number; ref?: string }
): Promise<CommitDescription[]> {
return git.log({ fs, dir: projectRoot, depth, ref })
}
export async function readFile(filepath: string): Promise<string> {
const file = await pify(fs.readFile)(filepath)
return file.toString()
}
export function listBranches(projectRoot: string): Promise<string[]> {
return git.listBranches({ fs, dir: projectRoot })
}
export async function existsPath(aPath: string): Promise<boolean> {
try {
// NOTE: fs.access is not supported in browserfs
await pify(fs.stat)(aPath)
return true
} catch (e) {
return false
}
}
/* WRITE */
export async function ensureProjectRepository(repo: Repository) {
// ensure directory
if (await existsPath(repo.dir)) {
console.log("Project: already exists")
} else {
console.log("Project: creating...")
await mkdirInRepository(repo, "")
await mkdirInRepository(repo, "src")
await writeFileInRepository(repo, "README.md", "# Hello!")
await writeFileInRepository(repo, "src/index.js", "export default {}")
console.log("Project: creating done")
}
// ensure git
if (await existsPath(j(repo.dir, ".git"))) {
console.log(".git: already exists")
} else {
await git.init(repo)
}
}
export async function getGitStatusInRepository(
projectRoot: string,
relpath: string
): Promise<string> {
try {
const status = await git.status({ fs, dir: projectRoot, filepath: relpath })
return status
} catch (e) {
return "untracked"
}
}
export async function listGitFilesInRepository(
projectRoot: string
): Promise<
Array<{
filepath: string
gitStatus: string
}>
> {
const files: string[] = await git.listFiles({ fs, dir: projectRoot })
const statusList = await Promise.all(
files.map(f => getGitStatusInRepository(projectRoot, f))
)
return zipWith(files, statusList, (filepath, gitStatus) => ({
filepath,
gitStatus
}))
}
/* WRITE */
export async function writeFile(aPath: string, content: string): Promise<void> {
await pify(fs.writeFile)(aPath, content)
}
export async function writeFileInRepository(
repo: Repository,
filepath: string,
content: string
): Promise<void> {
const aPath = j(repo.dir, filepath)
await pify(fs.writeFile)(aPath, content)
}
export async function createBranch(
projectRoot: string,
newBranchName: string
): Promise<void> {
return git.branch({ fs, dir: projectRoot, ref: newBranchName })
}
export async function checkoutBranch(
projectRoot: string,
branchName: string
): Promise<void> {
return git.checkout({ fs, dir: projectRoot, ref: branchName })
}
export async function mkdir(dirpath: string): Promise<void> {
if (await existsPath(dirpath)) {
// Do nothing
console.info("mkdir: exists", dirpath)
} else {
await pify(fs.mkdir)(dirpath)
console.info("mkdir: done", dirpath)
}
}
export async function mkdirInRepository(
repo: Repository,
dirpath: string
): Promise<void> {
const aPath = j(repo.dir, dirpath)
return mkdir(aPath)
}
export function addFileInRepository(
projectRoot: string,
relpath: string
): Promise<any> {
return git.add({ fs, dir: projectRoot, filepath: relpath })
}
export function commitChanges(
projectRoot: string,
message: string = "Update",
author?: { name: string; email: string }
): Promise<string> {
return git.commit({
author: author || {
email: "dummy",
name: "anonymous"
},
dir: projectRoot,
fs,
message
})
}
export async function unlink(aPath: string): Promise<void> {
await pify(fs.unlink)(aPath)
}