Skip to content

Commit

Permalink
perf(ui): improve get folder list to use Promises instead of sync (#3687
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pikax committed Sep 1, 2020
1 parent 34b58c0 commit 10d5ae4
Showing 1 changed file with 37 additions and 14 deletions.
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

0 comments on commit 10d5ae4

Please sign in to comment.