-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.js
229 lines (194 loc) · 5.36 KB
/
files.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
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
const path = require('path')
const log = require('@dhis2/cli-helpers-engine').reporter
const { spawn } = require('@dhis2/cli-helpers-engine').exec
const fg = require('fast-glob')
const fs = require('fs-extra')
const mm = require('micromatch')
const { CONSUMING_ROOT, PROJECT_ROOT } = require('./paths.js')
// blacklists for files
const blacklist = [
'.git',
'.d2',
'.yarn',
'node_modules',
'build',
'dist',
'target',
'vendor',
'dest',
'CHANGELOG.md',
]
// whitelists for files
const whitelists = {
css: ['.css'],
js: ['.js', '.jsx', '.ts'],
json: ['.json'],
all: ['.js', '.json', '.css', '.scss', '.md', '.jsx', '.ts'],
}
function whitelisted(whitelist) {
return function (file) {
return whitelist.includes(path.extname(file))
}
}
function cssFiles(arr) {
const whitelist = whitelisted(whitelists.css)
return arr.filter(whitelist)
}
function jsFiles(arr) {
const whitelist = whitelisted(whitelists.js)
return arr.filter(whitelist)
}
function jsonFiles(arr) {
const whitelist = whitelisted(whitelists.json)
return arr.filter(whitelist)
}
function readFile(fp) {
try {
const text = fs.readFileSync(fp, 'utf8')
return text
} catch (error) {
log.error('Reading failed', fp, error)
return null
}
}
function writeFile(fp, content) {
try {
fs.writeFileSync(fp, content, 'utf8')
return true
} catch (error) {
log.error('Writing failed', fp, error)
return false
}
}
function copy(from, to, { overwrite = false, backup = false }) {
try {
const exists = fs.existsSync(to)
const empty = exists ? fs.statSync(to).size === 0 : false
const replace = empty ? true : overwrite
fs.ensureDirSync(path.dirname(to))
if (exists) {
if (backup && !replace) {
const toNew = to.concat('.new')
log.debug(
'Existing config found, use --overwrite or manually merge with:'
)
log.print(`${path.relative(CONSUMING_ROOT, toNew)}`)
fs.copySync(from, toNew, { overwrite: true })
return
}
if (replace) {
log.debug('Overwriting existing configuration:')
log.print(`${path.relative(CONSUMING_ROOT, to)}`)
fs.copySync(from, to, { overwrite: true })
return
} else {
log.print(
`Skip existing config file: ${path.relative(
CONSUMING_ROOT,
to
)}`
)
return
}
} else {
log.debug('Configuration file added:')
log.print(`${path.relative(CONSUMING_ROOT, to)}`)
fs.copySync(from, to, { overwrite: replace })
}
} catch (err) {
log.error(`Failed to install configuration file: ${to}`, err)
}
}
function deleteFile(fp) {
try {
log.debug(`Deleting file: ${fp}`)
fs.removeSync(fp)
return true
} catch (error) {
log.error('File deletion failed', fp, error)
return false
}
}
function selectFiles(files, pattern, staged) {
let codeFiles = []
codeFiles = fg.sync(pattern, {
absolute: true,
baseNameMatch: true,
dot: true,
globstar: true,
onlyFiles: true,
ignore: blacklist.map((b) => `**/${b}/**`),
cwd: PROJECT_ROOT,
})
log.debug(`Using pattern: ${pattern}`)
log.debug(`Matched files: ${codeFiles.join(', ')}`)
if (files.length > 0) {
codeFiles = codeFiles
.filter((f) => mm.contains(f, files))
.map((f) => path.resolve(f))
}
if (staged) {
codeFiles = stagedFiles(codeFiles)
}
return codeFiles
}
const stagedFiles = (files = []) => {
const cmd = 'git'
const args = [
'diff',
'--cached',
'--name-only',
'--relative',
'--diff-filter=d',
]
const result = spawn(cmd, args, {
encoding: 'utf8',
stdio: 'pipe',
})
const output = result.stdout.trim()
if (output) {
const staged = output
.split('\n')
.map((f) => path.resolve(CONSUMING_ROOT, f))
return files.filter((f) => staged.includes(f))
}
return []
}
const pickFirstExists = (files = [], customRoot) => {
for (const file of files) {
const fp = customRoot
? path.join(customRoot, file)
: path.join(CONSUMING_ROOT, file)
const exists = fileExists(fp)
if (exists) {
log.debug(`Using ${fp} as the common ignore file.`)
return fp
}
}
return null
}
const fileExists = (fp) => fs.existsSync(fp) && fs.statSync(fp).size !== 0
const dirExists = (fp) => fs.existsSync(fp) && fs.statSync(fp).isDirectory()
const resolveIgnoreFile = (ignoreFiles = []) => {
return pickFirstExists([...ignoreFiles, '.d2styleignore', '.gitignore'])
}
const relativePath = (fp) => path.relative(CONSUMING_ROOT, fp)
module.exports = {
copy,
cssFiles,
deleteFile,
jsFiles,
jsonFiles,
readFile,
selectFiles,
stagedFiles,
writeFile,
whitelisted,
whitelists,
blacklist,
pickFirstExists,
resolveIgnoreFile,
fileExists,
dirExists,
relativePath,
}