Skip to content

chore: remove fs-extra #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"cookie": "^0.4.0",
"copy-template-dir": "^1.4.0",
"debug": "^4.1.1",
"del": "^5.1.0",
"dot-prop": "^5.1.0",
"dotenv": "^8.2.0",
"envinfo": "^7.3.1",
Expand All @@ -97,7 +98,6 @@
"express-logging": "^1.1.1",
"filter-obj": "^2.0.1",
"find-up": "^4.1.0",
"fs-extra": "^8.1.0",
"fuzzy": "^0.1.3",
"get-port": "^5.1.0",
"gh-release-fetch": "^1.0.3",
Expand Down
1 change: 0 additions & 1 deletion renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
'eslint',
'execa',
'find-up',
'fs-extra',
'globby',
'log-symbols',
'npm-packlist',
Expand Down
49 changes: 49 additions & 0 deletions site/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require('fs').promises
const path = require('path')
const rimraf = require('rimraf')
const util = require('util')

const rimrafAsync = util.promisify(rimraf)

const copyDirRecursiveAsync = async (src, dest) => {
try {
fs.mkdir(dest, { recursive: true })
} catch (_) {
// ignore erros for mkdir
}

const childrenItems = await fs.readdir(src)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the potential to create a lot of promises.
I don't think we should optimise this yet since it's only used for our docs site that doesn't contain many of files.


await Promise.all(
childrenItems.map(async item => {
const srcPath = path.join(src, item)
const destPath = path.join(dest, item)

const itemStat = await fs.lstat(srcPath)

if (itemStat.isFile()) {
fs.copyFile(srcPath, destPath)
} else {
await copyDirRecursiveAsync(srcPath, destPath)
}
})
)
}

const ensureFilePathAsync = async filePath => {
try {
await fs.mkdir(path.dirname(filePath), { recursive: true })
} catch (_) {
// ignore any errors with mkdir - it will throw if the path already exists.
}
}

const removeRecursiveAsync = async path => {
await rimrafAsync(path)
}

module.exports = {
copyDirRecursiveAsync,
ensureFilePathAsync,
removeRecursiveAsync,
}
36 changes: 0 additions & 36 deletions site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
},
"devDependencies": {
"filter-obj": "^2.0.1",
"fs-extra": "^9.0.1",
"map-obj": "^4.1.0",
"markdown-magic": "^1.0.0",
"npm-run-all": "^4.1.5",
Expand Down
24 changes: 10 additions & 14 deletions site/sync.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
const path = require('path')
const fs = require('fs-extra')
const fs = require('fs').promises

const config = require('./config')
const { promisify } = require('util')
const readdirP = promisify(fs.readdir)
const statP = promisify(fs.stat)
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const deleteFile = promisify(fs.unlink)
const { copyDirRecursiveAsync } = require('./fs')

async function readDir(dir, allFiles = []) {
const files = (await readdirP(dir)).map(f => path.join(dir, f))
const files = (await fs.readdir(dir)).map(f => path.join(dir, f))
allFiles.push(...files)
await Promise.all(files.map(async f => (await statP(f)).isDirectory() && readDir(f, allFiles)))
await Promise.all(files.map(async f => (await fs.stat(f)).isDirectory() && readDir(f, allFiles)))
return allFiles
}

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

await fs.copy(src, destination)
await copyDirRecursiveAsync(src, destination)
console.log(`Docs synced to ${destination}`)

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

async function removeMarkDownLinks(filePath) {
const content = await readFile(filePath, 'utf-8')
const content = await fs.readFile(filePath, 'utf-8')
const newContent = content.replace(/(\w+)\.md/gm, '$1').replace(/\/docs\/commands\//gm, '/commands/')
// Rename README.md to index.md
if (path.basename(filePath) === 'README.md') {
const newPath = path.join(path.dirname(filePath), 'index.md')
// Delete README.md from docs site
await deleteFile(filePath)
await fs.unlink(filePath)
// Write index.md
await writeFile(newPath, newContent)
await fs.writeFile(newPath, newContent)
return newPath
}
await writeFile(filePath, newContent)
await fs.writeFile(filePath, newContent)
return filePath
}

Expand Down
39 changes: 18 additions & 21 deletions site/watch.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
/* Syncs blog content from repo to /site/blog */
const path = require('path')
const sane = require('sane')
const fs = require('fs-extra')
const fs = require('fs').promises

const config = require('./config')
const { ensureFilePathAsync, removeRecursiveAsync } = require('./fs')

const watcher = sane(config.docs.srcPath, { glob: ['**/*.md'] })

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

watcher.on('change', function(filepath) {
watcher.on('change', async function(filepath) {
console.log('file changed', filepath)
syncFile(filepath).then(() => {
// console.log('done')
})
await syncFile(filepath)
})

watcher.on('add', function(filepath) {
watcher.on('add', async function(filepath) {
console.log('file added')
syncFile(filepath).then(() => {
// console.log('done')
})
await syncFile(filepath)
})

watcher.on('delete', function(filepath) {
watcher.on('delete', async function(filepath) {
console.log('file deleted', filepath)
deleteFile(filepath).then(() => {
console.log('File deletion complete')
})
await deleteFile(filepath)
console.log('File deletion complete')
})

/* utils */
Expand All @@ -39,16 +37,15 @@ function getFullPath(filePath) {
}
}

function syncFile(filePath) {
async function syncFile(filePath) {
const { src, destination } = getFullPath(filePath)
return fs.copy(src, destination).then(() => {
console.log(`${filePath} synced to ${destination}`)
})
await ensureFilePathAsync(destination)
await fs.copyFile(src, destination)
console.log(`${filePath} synced to ${destination}`)
}

function deleteFile(filePath) {
async function deleteFile(filePath) {
const { destination } = getFullPath(filePath)
return fs.remove(destination).then(() => {
console.log(`${filePath} removed from ${destination}`)
})
await removeRecursiveAsync(destination)
console.log(`${filePath} removed from ${destination}`)
}
12 changes: 7 additions & 5 deletions src/commands/functions/create.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const fs = require('fs-extra')
const fs = require('fs')
const path = require('path')
const copy = require('copy-template-dir')
const { flags } = require('@oclif/command')
const Command = require('../../utils/command')
const inquirer = require('inquirer')
const { readRepoURL, validateRepoURL } = require('../../utils/read-repo-url')
const { addEnvVariables } = require('../../utils/dev')
const fetch = require('node-fetch')
const cp = require('child_process')
const ora = require('ora')
const chalk = require('chalk')

const { mkdirRecursiveSync } = require('../../lib/fs')
const Command = require('../../utils/command')
const { readRepoURL, validateRepoURL } = require('../../utils/read-repo-url')
const { addEnvVariables } = require('../../utils/dev')
const {
// NETLIFYDEV,
NETLIFYDEVLOG,
Expand Down Expand Up @@ -215,7 +217,7 @@ async function downloadFromURL(flags, args, functionsDir) {
}

try {
fs.mkdirSync(fnFolder, { recursive: true })
mkdirRecursiveSync(fnFolder)
} catch (error) {
// Ignore
}
Expand Down
14 changes: 8 additions & 6 deletions src/lib/exec-fetcher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require('fs-extra')
const path = require('path')
const execa = require('execa')
const { fetchLatest, updateAvailable } = require('gh-release-fetch')

const { NETLIFYDEVWARN } = require('../utils/logo')
const fs = require('./fs')

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

const execExist = async binPath => {
const binExists = await fs.exists(binPath)
if (!binExists) {
return false
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup!

const stat = await fs.statAsync(binPath)
return stat.isFile() && isExe(stat.mode, stat.gid, stat.uid)
} catch (error) {
if (error.code === 'ENOENT') return false
throw error
}
const stat = fs.statSync(binPath)
return stat && stat.isFile() && isExe(stat.mode, stat.gid, stat.uid)
}

const isVersionOutdated = async ({ packageName, currentVersion }) => {
Expand Down
7 changes: 4 additions & 3 deletions src/lib/exec-fetcher.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const test = require('ava')
const path = require('path')
const fs = require('fs-extra')
const tempDirectory = require('temp-dir')
const { v4: uuid } = require('uuid')

const { getExecName, shouldFetchLatestVersion, fetchLatestVersion } = require('./exec-fetcher')
const fs = require('./fs')

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

test.afterEach(async t => {
await fs.remove(t.context.binPath)
await fs.rmdirRecursiveAsync(t.context.binPath)
})

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

const execPath = path.join(binPath, getExecName({ execName }))
const stats = await fs.stat(execPath)
const stats = await fs.statAsync(execPath)
t.is(stats.size >= 5000, true)
})
})
Loading