1 | const { dirname, extname, relative } = require('path')
|
2 | const fs = require('fs-extra')
|
3 | const glob = require('globby')
|
4 |
|
5 | const cwd = process.cwd()
|
6 |
|
7 | const relativeToCwd = filePath =>
|
8 | relative(cwd, filePath)
|
9 |
|
10 | const extension = filePath =>
|
11 | extname(filePath).replace(/^\./, '')
|
12 |
|
13 | const 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 |
|
22 | const isDirectory = filePath => {
|
23 | try {
|
24 | return fs.lstatSync(filePath).isDirectory()
|
25 | } catch (err) {
|
26 | return false
|
27 | }
|
28 | }
|
29 |
|
30 | const invalidateRequireCache = filePath => {
|
31 | delete require.cache[require.resolve(filePath)]
|
32 | }
|
33 |
|
34 | const requireUncached = filePath => {
|
35 | invalidateRequireCache(filePath)
|
36 | return require(filePath)
|
37 | }
|
38 |
|
39 | async function read (filePath) {
|
40 | const string = await fs.readFile(filePath, 'utf8')
|
41 | return string.trim()
|
42 | }
|
43 |
|
44 | async function write (filePath, content) {
|
45 | await fs.outputFile(filePath, `${content.trim()}\n`)
|
46 | }
|
47 |
|
48 | async function copy (src, dst) {
|
49 | const dir = dirname(dst)
|
50 | fs.mkdirsSync(dir)
|
51 | await fs.copy(src, dst, { dereference: true })
|
52 | }
|
53 |
|
54 | module.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 | }
|