Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit e2a2300

Browse files
WliuWliu
Wliu
authored and
Wliu
committed
file.coffee -> file.js
1 parent d2a576d commit e2a2300

File tree

2 files changed

+90
-69
lines changed

2 files changed

+90
-69
lines changed

lib/file.coffee

Lines changed: 0 additions & 69 deletions
This file was deleted.

lib/file.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const path = require('path')
2+
const fs = require('fs-plus')
3+
const {CompositeDisposable, Emitter} = require('atom')
4+
const {repoForPath} = require('./helpers')
5+
6+
module.exports =
7+
class File {
8+
constructor({name, fullPath, symlink, realpathCache, useSyncFS, stats}) {
9+
this.name = name
10+
this.symlink = symlink
11+
this.stats = stats
12+
this.destroyed = false
13+
this.emitter = new Emitter()
14+
this.subscriptions = new CompositeDisposable()
15+
16+
this.path = fullPath
17+
this.realPath = this.path
18+
19+
this.subscribeToRepo()
20+
this.updateStatus()
21+
22+
if (useSyncFS) {
23+
this.realPath = fs.realpathSync(this.path)
24+
} else {
25+
fs.realpath(this.path, realpathCache, (error, realPath) => {
26+
if (this.destroyed) return
27+
if (realPath && realPath !== this.path) {
28+
this.realPath = realPath
29+
this.updateStatus()
30+
}
31+
})
32+
}
33+
}
34+
35+
destroy() {
36+
this.destroyed = true
37+
this.subscriptions.dispose()
38+
this.emitter.emit('did-destroy')
39+
}
40+
41+
onDidDestroy(callback) {
42+
return this.emitter.on('did-destroy', callback)
43+
}
44+
45+
onDidStatusChange(callback) {
46+
return this.emitter.on('did-status-change', callback)
47+
}
48+
49+
// Subscribe to the project's repo for changes to the Git status of this file.
50+
subscribeToRepo() {
51+
const repo = repoForPath(this.path)
52+
if (repo == null) return
53+
54+
this.subscriptions.add(repo.onDidChangeStatus(event => {
55+
if (this.isPathEqual(event.path)) {
56+
this.updateStatus(repo)
57+
}
58+
}))
59+
this.subscriptions.add(repo.onDidChangeStatuses(() => {
60+
this.updateStatus(repo)
61+
}))
62+
}
63+
64+
// Update the status property of this directory using the repo.
65+
updateStatus() {
66+
const repo = repoForPath(this.path)
67+
if (repo == null) return
68+
69+
let newStatus = null
70+
if (repo.isPathIgnored(this.path)) {
71+
newStatus = 'ignored'
72+
} else {
73+
const status = repo.getCachedPathStatus(this.path)
74+
if (repo.isStatusModified(status)) {
75+
newStatus = 'modified'
76+
} else if (repo.isStatusNew(status)) {
77+
newStatus = 'added'
78+
}
79+
}
80+
81+
if (newStatus !== this.status) {
82+
this.status = newStatus
83+
this.emitter.emit('did-status-change', newStatus)
84+
}
85+
}
86+
87+
isPathEqual(pathToCompare) {
88+
return this.path === pathToCompare || this.realPath === pathToCompare
89+
}
90+
}

0 commit comments

Comments
 (0)