Skip to content

Commit

Permalink
perf: drop readFileSync (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 19, 2021
1 parent 296e608 commit 12af43d
Showing 1 changed file with 53 additions and 50 deletions.
103 changes: 53 additions & 50 deletions packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,72 +230,75 @@ async function getDiagnostics(
disposeSuppressConsole = suppressConsole()
}

for (const absFilePath of absFilePaths) {
const fileText = fs.readFileSync(absFilePath, 'utf-8')
clientConnection.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: {
languageId: 'vue',
uri: URI.file(absFilePath).toString(),
version: DOC_VERSION.init,
text: fileText,
},
})

// logout in build mode
if (!options.watch) {
try {
let diagnostics = (await clientConnection.sendRequest('$/getDiagnostics', {
await Promise.all(
absFilePaths.map(async (absFilePath) => {
const fileText = await fs.promises.readFile(absFilePath, 'utf-8')
clientConnection.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: {
languageId: 'vue',
uri: URI.file(absFilePath).toString(),
version: DOC_VERSION.init,
})) as Diagnostic[]

/**
* Ignore eslint errors for now
*/
diagnostics = diagnostics
.filter((r) => r.source !== 'eslint-plugin-vue')
.filter((r) => r.severity && r.severity <= severity)

if (diagnostics.length > 0) {
logChunk +=
os.EOL +
diagnostics
.map((d) =>
diagnosticToTerminalLog(
normalizeLspDiagnostic({
diagnostic: d,
absFilePath,
fileText,
}),
'VLS'
text: fileText,
},
})

// logout in build mode
if (!options.watch) {
try {
let diagnostics = (await clientConnection.sendRequest('$/getDiagnostics', {
uri: URI.file(absFilePath).toString(),
version: DOC_VERSION.init,
})) as Diagnostic[]

/**
* Ignore eslint errors for now
*/
diagnostics = diagnostics
.filter((r) => r.source !== 'eslint-plugin-vue')
.filter((r) => r.severity && r.severity <= severity)

if (diagnostics.length > 0) {
logChunk +=
os.EOL +
diagnostics
.map((d) =>
diagnosticToTerminalLog(
normalizeLspDiagnostic({
diagnostic: d,
absFilePath,
fileText,
}),
'VLS'
)
)
)
.join(os.EOL)

diagnostics.forEach((d) => {
if (d.severity === DiagnosticSeverity.Error) {
initialErrCount++
}
})
.join(os.EOL)

diagnostics.forEach((d) => {
if (d.severity === DiagnosticSeverity.Error) {
initialErrCount++
}
})
}
} catch (err) {
console.error(err.stack)
}
} catch (err) {
console.error(err.stack)
}
}
}
})
)

// watched diagnostics report
if (options.watch) {
Checker.watcher.add(workspaceUri.fsPath)
Checker.watcher.on('all', (event, path) => {
Checker.watcher.on('all', async (event, path) => {
if (!path.endsWith('.vue')) return
const fileContent = await fs.promises.readFile(path, 'utf-8')
// TODO: watch js change
clientConnection.sendNotification(DidChangeTextDocumentNotification.type, {
textDocument: {
uri: URI.file(path).toString(),
version: Date.now(),
},
contentChanges: [{ text: fs.readFileSync(path, 'utf-8') }],
contentChanges: [{ text: fileContent }],
})
})
}
Expand Down

0 comments on commit 12af43d

Please sign in to comment.