UNPKG

1.29 kBJavaScriptView Raw
1const { dirname, extname, relative } = require('path')
2const fs = require('fs-extra')
3const glob = require('globby')
4
5const cwd = process.cwd()
6
7const relativeToCwd = filePath =>
8 relative(cwd, filePath)
9
10const extension = filePath =>
11 extname(filePath).replace(/^\./, '')
12
13const exists = filePath => {
14 try {
15 const stat = fs.statSync(filePath)
16 return stat.isFile() || stat.isDirectory()
17 } catch (err) {
18 return false
19 }
20}
21
22const isDirectory = filePath => {
23 try {
24 return fs.lstatSync(filePath).isDirectory()
25 } catch (err) {
26 return false
27 }
28}
29
30const invalidateRequireCache = filePath => {
31 delete require.cache[require.resolve(filePath)]
32}
33
34const requireUncached = filePath => {
35 invalidateRequireCache(filePath)
36 return require(filePath)
37}
38
39async function read (filePath) {
40 const string = await fs.readFile(filePath, 'utf8')
41 return string.trim()
42}
43
44async function write (filePath, content) {
45 await fs.outputFile(filePath, `${content.trim()}\n`)
46}
47
48async function copy (src, dst) {
49 const dir = dirname(dst)
50 fs.mkdirsSync(dir)
51 await fs.copy(src, dst, { dereference: true })
52}
53
54module.exports = {
55 remove: fs.remove,
56 read,
57 write,
58 copy,
59 glob,
60 extension,
61 exists,
62 isDirectory,
63 requireUncached,
64 invalidateRequireCache,
65 relativeToCwd
66}