forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolders.js
190 lines (166 loc) · 3.99 KB
/
folders.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
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
const path = require('path')
const fs = require('fs-extra')
const LRU = require('lru-cache')
const winattr = require('@akryum/winattr')
const hiddenPrefix = '.'
const isPlatformWindows = process.platform.indexOf('win') === 0
const pkgCache = new LRU({
max: 500,
maxAge: 1000 * 5
})
const cwd = require('./cwd')
function isDirectory (file) {
file = file.replace(/\\/g, path.sep)
try {
return fs.statSync(file).isDirectory()
} catch (e) {
if (process.env.VUE_APP_CLI_UI_DEV) console.warn(e.message)
}
return false
}
async function list (base, context) {
let dir = base
if (isPlatformWindows) {
if (base.match(/^([A-Z]{1}:)$/)) {
dir = path.join(base, '\\')
}
}
const files = await fs.readdir(dir, 'utf8')
return files.map(
file => {
const folderPath = path.join(base, file)
return {
path: folderPath,
name: file,
hidden: isHidden(folderPath)
}
}
).filter(
file => isDirectory(file.path)
)
}
function isHidden (file) {
try {
const prefixed = path.basename(file).charAt(0) === hiddenPrefix
const result = {
unix: prefixed,
windows: false
}
if (isPlatformWindows) {
const windowsFile = file.replace(/\\/g, '\\\\')
result.windows = winattr.getSync(windowsFile).hidden
}
return (!isPlatformWindows && result.unix) || (isPlatformWindows && result.windows)
} catch (e) {
if (process.env.VUE_APP_CLI_UI_DEV) {
console.log('file:', file)
console.error(e)
}
}
}
function generateFolder (file, context) {
return {
name: path.basename(file),
path: file
}
}
function getCurrent (args, context) {
const base = cwd.get()
return generateFolder(base, context)
}
function open (file, context) {
cwd.set(file, context)
return generateFolder(cwd.get(), context)
}
function openParent (file, context) {
const newFile = path.dirname(file)
cwd.set(newFile, context)
return generateFolder(cwd.get(), context)
}
function isPackage (file, context) {
try {
return fs.existsSync(path.join(file, 'package.json'))
} catch (e) {
console.warn(e.message)
}
return false
}
function readPackage (file, context, force = false) {
if (!force) {
const cachedValue = pkgCache.get(file)
if (cachedValue) {
return cachedValue
}
}
const pkgFile = path.join(file, 'package.json')
if (fs.existsSync(pkgFile)) {
const pkg = fs.readJsonSync(pkgFile)
pkgCache.set(file, pkg)
return pkg
}
}
function writePackage ({ file, data }, context) {
fs.outputJsonSync(path.join(file, 'package.json'), data, {
spaces: 2
})
invalidatePackage(file, context)
return true
}
function invalidatePackage (file, context) {
pkgCache.del(file)
return true
}
function isVueProject (file, context) {
if (!isPackage(file)) return false
try {
const pkg = readPackage(file, context)
return Object.keys(pkg.devDependencies || {}).includes('@vue/cli-service')
} catch (e) {
if (process.env.VUE_APP_CLI_UI_DEV) {
console.log(e)
}
}
return false
}
function listFavorite (context) {
return context.db.get('foldersFavorite').value().map(
file => generateFolder(file.id, context)
)
}
function isFavorite (file, context) {
return !!context.db.get('foldersFavorite').find({ id: file }).size().value()
}
function setFavorite ({ file, favorite }, context) {
const collection = context.db.get('foldersFavorite')
if (favorite) {
collection.push({ id: file }).write()
} else {
collection.remove({ id: file }).write()
}
return generateFolder(file, context)
}
async function deleteFolder (file) {
await fs.remove(file)
}
function createFolder (name, context) {
const file = path.join(cwd.get(), name)
fs.mkdirpSync(file)
return generateFolder(file, context)
}
module.exports = {
isDirectory,
getCurrent,
list,
open,
openParent,
isPackage,
readPackage,
writePackage,
invalidatePackage,
isVueProject,
isFavorite,
listFavorite,
setFavorite,
delete: deleteFolder,
create: createFolder
}