Skip to content
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

perf(ui): improve get folder list to use Promises instead of sync #3687

Merged
merged 3 commits into from Sep 1, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 41 additions & 16 deletions packages/@vue/cli-ui/apollo-server/connectors/folders.js
Expand Up @@ -13,7 +13,7 @@ const pkgCache = new LRU({

const cwd = require('./cwd')

function isDirectory (file) {
function isDirectorySync (file) {
pikax marked this conversation as resolved.
Show resolved Hide resolved
file = file.replace(/\\/g, path.sep)
try {
return fs.statSync(file).isDirectory()
Expand All @@ -23,6 +23,16 @@ function isDirectory (file) {
return false
}

function isDirectory (file) {
file = file.replace(/\\/g, path.sep)
try {
return fs.stat(file).then(x => x.isDirectory())
} catch (e) {
if (process.env.VUE_APP_CLI_UI_DEV) console.warn(e.message)
}
return false
}

async function list (base, context) {
let dir = base
if (isPlatformWindows) {
Expand All @@ -31,21 +41,37 @@ async function list (base, context) {
}
}
const files = await fs.readdir(dir, 'utf8')
return files.map(
file => {
const folderPath = path.join(base, file)
return {
path: folderPath,
name: file,
hidden: isHidden(folderPath)
}

const f = await Promise.all(files.map(async file => {
const folderPath = path.join(base, file)

const [directory, hidden] = await Promise.all([isDirectory(folderPath), isHidden(folderPath)])
if (!directory) {
return null
}
).filter(
file => isDirectory(file.path)
)
return {
path: folderPath,
name: file,
hidden
}
}))
return f.filter(x => !!x)
}

async function isHiddenWindows (file) {
const windowsFile = file.replace(/\\/g, '\\\\')
return (new Promise((resolve, reject) => {
winattr.get(windowsFile, (file, error) => {
if (error) {
return reject(error)
}
resolve(file)
})
})
.then(x => x.hidden))
}

function isHidden (file) {
async function isHidden (file) {
try {
const prefixed = path.basename(file).charAt(0) === hiddenPrefix
const result = {
Expand All @@ -54,8 +80,7 @@ function isHidden (file) {
}

if (isPlatformWindows) {
const windowsFile = file.replace(/\\/g, '\\\\')
result.windows = winattr.getSync(windowsFile).hidden
result.windows = await isHiddenWindows(file)
}

return (!isPlatformWindows && result.unix) || (isPlatformWindows && result.windows)
Expand Down Expand Up @@ -172,7 +197,7 @@ function createFolder (name, context) {
}

module.exports = {
isDirectory,
isDirectory: isDirectorySync,
getCurrent,
list,
open,
Expand Down