Skip to content

Commit 91b147e

Browse files
authored
chore: remove fs-extra (#1271)
1 parent f1a9f1e commit 91b147e

File tree

15 files changed

+149
-139
lines changed

15 files changed

+149
-139
lines changed

package-lock.json

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"cookie": "^0.4.0",
9090
"copy-template-dir": "^1.4.0",
9191
"debug": "^4.1.1",
92+
"del": "^5.1.0",
9293
"dot-prop": "^5.1.0",
9394
"dotenv": "^8.2.0",
9495
"envinfo": "^7.3.1",
@@ -97,7 +98,6 @@
9798
"express-logging": "^1.1.1",
9899
"filter-obj": "^2.0.1",
99100
"find-up": "^4.1.0",
100-
"fs-extra": "^8.1.0",
101101
"fuzzy": "^0.1.3",
102102
"get-port": "^5.1.0",
103103
"gh-release-fetch": "^1.0.3",

renovate.json5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
'eslint',
2121
'execa',
2222
'find-up',
23-
'fs-extra',
2423
'globby',
2524
'log-symbols',
2625
'npm-packlist',

site/fs.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require('fs').promises
2+
const path = require('path')
3+
const rimraf = require('rimraf')
4+
const util = require('util')
5+
6+
const rimrafAsync = util.promisify(rimraf)
7+
8+
const copyDirRecursiveAsync = async (src, dest) => {
9+
try {
10+
fs.mkdir(dest, { recursive: true })
11+
} catch (_) {
12+
// ignore erros for mkdir
13+
}
14+
15+
const childrenItems = await fs.readdir(src)
16+
17+
await Promise.all(
18+
childrenItems.map(async item => {
19+
const srcPath = path.join(src, item)
20+
const destPath = path.join(dest, item)
21+
22+
const itemStat = await fs.lstat(srcPath)
23+
24+
if (itemStat.isFile()) {
25+
fs.copyFile(srcPath, destPath)
26+
} else {
27+
await copyDirRecursiveAsync(srcPath, destPath)
28+
}
29+
})
30+
)
31+
}
32+
33+
const ensureFilePathAsync = async filePath => {
34+
try {
35+
await fs.mkdir(path.dirname(filePath), { recursive: true })
36+
} catch (_) {
37+
// ignore any errors with mkdir - it will throw if the path already exists.
38+
}
39+
}
40+
41+
const removeRecursiveAsync = async path => {
42+
await rimrafAsync(path)
43+
}
44+
45+
module.exports = {
46+
copyDirRecursiveAsync,
47+
ensureFilePathAsync,
48+
removeRecursiveAsync,
49+
}

site/package-lock.json

Lines changed: 0 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
},
3535
"devDependencies": {
3636
"filter-obj": "^2.0.1",
37-
"fs-extra": "^9.0.1",
3837
"map-obj": "^4.1.0",
3938
"markdown-magic": "^1.0.0",
4039
"npm-run-all": "^4.1.5",

site/sync.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
const path = require('path')
2-
const fs = require('fs-extra')
2+
const fs = require('fs').promises
3+
34
const config = require('./config')
4-
const { promisify } = require('util')
5-
const readdirP = promisify(fs.readdir)
6-
const statP = promisify(fs.stat)
7-
const readFile = promisify(fs.readFile)
8-
const writeFile = promisify(fs.writeFile)
9-
const deleteFile = promisify(fs.unlink)
5+
const { copyDirRecursiveAsync } = require('./fs')
106

117
async function readDir(dir, allFiles = []) {
12-
const files = (await readdirP(dir)).map(f => path.join(dir, f))
8+
const files = (await fs.readdir(dir)).map(f => path.join(dir, f))
139
allFiles.push(...files)
14-
await Promise.all(files.map(async f => (await statP(f)).isDirectory() && readDir(f, allFiles)))
10+
await Promise.all(files.map(async f => (await fs.stat(f)).isDirectory() && readDir(f, allFiles)))
1511
return allFiles
1612
}
1713

1814
async function syncLocalContent() {
1915
const src = path.join(config.docs.srcPath)
2016
const destination = path.join(config.docs.outputPath)
2117

22-
await fs.copy(src, destination)
18+
await copyDirRecursiveAsync(src, destination)
2319
console.log(`Docs synced to ${destination}`)
2420

2521
const files = await readDir(destination)
@@ -35,18 +31,18 @@ async function syncLocalContent() {
3531
}
3632

3733
async function removeMarkDownLinks(filePath) {
38-
const content = await readFile(filePath, 'utf-8')
34+
const content = await fs.readFile(filePath, 'utf-8')
3935
const newContent = content.replace(/(\w+)\.md/gm, '$1').replace(/\/docs\/commands\//gm, '/commands/')
4036
// Rename README.md to index.md
4137
if (path.basename(filePath) === 'README.md') {
4238
const newPath = path.join(path.dirname(filePath), 'index.md')
4339
// Delete README.md from docs site
44-
await deleteFile(filePath)
40+
await fs.unlink(filePath)
4541
// Write index.md
46-
await writeFile(newPath, newContent)
42+
await fs.writeFile(newPath, newContent)
4743
return newPath
4844
}
49-
await writeFile(filePath, newContent)
45+
await fs.writeFile(filePath, newContent)
5046
return filePath
5147
}
5248

site/watch.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
/* Syncs blog content from repo to /site/blog */
22
const path = require('path')
33
const sane = require('sane')
4-
const fs = require('fs-extra')
4+
const fs = require('fs').promises
5+
56
const config = require('./config')
7+
const { ensureFilePathAsync, removeRecursiveAsync } = require('./fs')
8+
69
const watcher = sane(config.docs.srcPath, { glob: ['**/*.md'] })
710

811
/* Watch Files */
912
watcher.on('ready', function() {
1013
console.log(`Watching ${config.docs.srcPath} files for changes`)
1114
})
1215

13-
watcher.on('change', function(filepath) {
16+
watcher.on('change', async function(filepath) {
1417
console.log('file changed', filepath)
15-
syncFile(filepath).then(() => {
16-
// console.log('done')
17-
})
18+
await syncFile(filepath)
1819
})
1920

20-
watcher.on('add', function(filepath) {
21+
watcher.on('add', async function(filepath) {
2122
console.log('file added')
22-
syncFile(filepath).then(() => {
23-
// console.log('done')
24-
})
23+
await syncFile(filepath)
2524
})
2625

27-
watcher.on('delete', function(filepath) {
26+
watcher.on('delete', async function(filepath) {
2827
console.log('file deleted', filepath)
29-
deleteFile(filepath).then(() => {
30-
console.log('File deletion complete')
31-
})
28+
await deleteFile(filepath)
29+
console.log('File deletion complete')
3230
})
3331

3432
/* utils */
@@ -39,16 +37,15 @@ function getFullPath(filePath) {
3937
}
4038
}
4139

42-
function syncFile(filePath) {
40+
async function syncFile(filePath) {
4341
const { src, destination } = getFullPath(filePath)
44-
return fs.copy(src, destination).then(() => {
45-
console.log(`${filePath} synced to ${destination}`)
46-
})
42+
await ensureFilePathAsync(destination)
43+
await fs.copyFile(src, destination)
44+
console.log(`${filePath} synced to ${destination}`)
4745
}
4846

49-
function deleteFile(filePath) {
47+
async function deleteFile(filePath) {
5048
const { destination } = getFullPath(filePath)
51-
return fs.remove(destination).then(() => {
52-
console.log(`${filePath} removed from ${destination}`)
53-
})
49+
await removeRecursiveAsync(destination)
50+
console.log(`${filePath} removed from ${destination}`)
5451
}

src/commands/functions/create.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
const fs = require('fs-extra')
1+
const fs = require('fs')
22
const path = require('path')
33
const copy = require('copy-template-dir')
44
const { flags } = require('@oclif/command')
5-
const Command = require('../../utils/command')
65
const inquirer = require('inquirer')
7-
const { readRepoURL, validateRepoURL } = require('../../utils/read-repo-url')
8-
const { addEnvVariables } = require('../../utils/dev')
96
const fetch = require('node-fetch')
107
const cp = require('child_process')
118
const ora = require('ora')
129
const chalk = require('chalk')
10+
11+
const { mkdirRecursiveSync } = require('../../lib/fs')
12+
const Command = require('../../utils/command')
13+
const { readRepoURL, validateRepoURL } = require('../../utils/read-repo-url')
14+
const { addEnvVariables } = require('../../utils/dev')
1315
const {
1416
// NETLIFYDEV,
1517
NETLIFYDEVLOG,
@@ -215,7 +217,7 @@ async function downloadFromURL(flags, args, functionsDir) {
215217
}
216218

217219
try {
218-
fs.mkdirSync(fnFolder, { recursive: true })
220+
mkdirRecursiveSync(fnFolder)
219221
} catch (error) {
220222
// Ignore
221223
}

src/lib/exec-fetcher.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const fs = require('fs-extra')
21
const path = require('path')
32
const execa = require('execa')
43
const { fetchLatest, updateAvailable } = require('gh-release-fetch')
4+
55
const { NETLIFYDEVWARN } = require('../utils/logo')
6+
const fs = require('./fs')
67

78
const isWindows = () => {
89
return process.platform === 'win32'
@@ -26,12 +27,13 @@ const isExe = (mode, gid, uid) => {
2627
}
2728

2829
const execExist = async binPath => {
29-
const binExists = await fs.exists(binPath)
30-
if (!binExists) {
31-
return false
30+
try {
31+
const stat = await fs.statAsync(binPath)
32+
return stat.isFile() && isExe(stat.mode, stat.gid, stat.uid)
33+
} catch (error) {
34+
if (error.code === 'ENOENT') return false
35+
throw error
3236
}
33-
const stat = fs.statSync(binPath)
34-
return stat && stat.isFile() && isExe(stat.mode, stat.gid, stat.uid)
3537
}
3638

3739
const isVersionOutdated = async ({ packageName, currentVersion }) => {

src/lib/exec-fetcher.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
const test = require('ava')
22
const path = require('path')
3-
const fs = require('fs-extra')
43
const tempDirectory = require('temp-dir')
54
const { v4: uuid } = require('uuid')
5+
66
const { getExecName, shouldFetchLatestVersion, fetchLatestVersion } = require('./exec-fetcher')
7+
const fs = require('./fs')
78

89
test.beforeEach(t => {
910
const directory = path.join(tempDirectory, `netlify-cli-exec-fetcher`, uuid())
1011
t.context.binPath = directory
1112
})
1213

1314
test.afterEach(async t => {
14-
await fs.remove(t.context.binPath)
15+
await fs.rmdirRecursiveAsync(t.context.binPath)
1516
})
1617

1718
test(`should postix exec with .exe on windows`, t => {
@@ -59,7 +60,7 @@ packages.forEach(({ packageName, execName, execArgs, pattern, extension }) => {
5960
await fetchLatestVersion({ packageName, execName, destination: binPath, extension })
6061

6162
const execPath = path.join(binPath, getExecName({ execName }))
62-
const stats = await fs.stat(execPath)
63+
const stats = await fs.statAsync(execPath)
6364
t.is(stats.size >= 5000, true)
6465
})
6566
})

0 commit comments

Comments
 (0)