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 all commits
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
51 changes: 37 additions & 14 deletions packages/@vue/cli-ui/apollo-server/connectors/folders.js
Expand Up @@ -16,7 +16,7 @@ const cwd = require('./cwd')
function isDirectory (file) {
file = file.replace(/\\/g, path.sep)
try {
return fs.statSync(file).isDirectory()
return fs.stat(file).then((x) => x.isDirectory())
} catch (e) {
if (process.env.VUE_APP_CLI_UI_DEBUG) console.warn(e.message)
}
Expand All @@ -31,21 +31,41 @@ async function list (base, context) {
}
}
const files = await fs.readdir(dir, 'utf8')
return files.map(
file => {

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
}
return {
path: folderPath,
name: file,
hidden: isHidden(folderPath)
hidden
}
}
).filter(
file => isDirectory(file.path)
})
)
return f.filter((x) => !!x)
}

function isHidden (file) {
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)
}

async function isHidden (file) {
try {
const prefixed = path.basename(file).charAt(0) === hiddenPrefix
const result = {
Expand All @@ -54,11 +74,13 @@ 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)
return (
(!isPlatformWindows && result.unix) ||
(isPlatformWindows && result.windows)
)
} catch (e) {
if (process.env.VUE_APP_CLI_UI_DEBUG) {
console.log('file:', file)
Expand Down Expand Up @@ -142,9 +164,10 @@ function isVueProject (file, context) {
}

function listFavorite (context) {
return context.db.get('foldersFavorite').value().map(
file => generateFolder(file.id, context)
)
return context.db
.get('foldersFavorite')
.value()
.map((file) => generateFolder(file.id, context))
}

function isFavorite (file, context) {
Expand Down