feat(app/metrics): use linguist-js file content for single files [skip ci]
This commit is contained in:
@@ -171,38 +171,12 @@ export async function chartist() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**Language analyzer (single file) */
|
/**Language analyzer (single file) */
|
||||||
export async function language({filename, patch, prefix = "", timeout = 20 * 1000}) {
|
export async function language({filename, patch}) {
|
||||||
const path = paths.join(os.tmpdir(), `${prefix}-${Math.random()}`.replace(/[^\w-]/g, ""))
|
console.debug(`metrics/language > ${filename}`)
|
||||||
return new Promise(async (solve, reject) => {
|
const {languages:{results}} = await linguist(filename, {fileContent:patch})
|
||||||
setTimeout(() => {
|
const result = (Object.keys(results).shift() ?? "unknown").toLocaleLowerCase()
|
||||||
console.debug(`metrics/language > ${filename} > timeout`)
|
console.debug(`metrics/language > ${filename} > result: ${result}`)
|
||||||
reject("timeout")
|
return result
|
||||||
}, timeout)
|
|
||||||
try {
|
|
||||||
//Create temp dir
|
|
||||||
console.debug(`metrics/language > ${filename} > creating temp dir at ${path}`)
|
|
||||||
await fs.mkdir(path, {recursive:true})
|
|
||||||
|
|
||||||
//Create file and remove diff syntax
|
|
||||||
await fs.writeFile(paths.join(path, paths.basename(filename)), patch.replace(/^@@ -\d+,\d+ \+\d+,\d+ @@/gm, "").replace(/^[+-]/gm, ""))
|
|
||||||
|
|
||||||
//Call linguist
|
|
||||||
console.debug(`metrics/language > ${filename} > calling linguist`)
|
|
||||||
const {languages:{results}} = await linguist(path)
|
|
||||||
const result = (Object.keys(results).shift() ?? "unknown").toLocaleLowerCase()
|
|
||||||
console.debug(`metrics/language > ${filename} > result: ${result}`)
|
|
||||||
solve(result)
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.debug(`metrics/language > ${filename} > ${error}`)
|
|
||||||
reject(error)
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
//Clean temp dir
|
|
||||||
console.debug(`metrics/language > ${filename} > cleaning temp dir at ${path}`)
|
|
||||||
fs.rm(path, {recursive:true, force:true}).catch(error => console.debug(`metrics/language > ${filename} > failed to clean temp dir at ${path} > ${error}`))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Run command (use this to execute commands and process whole output at once, may not be suitable for large outputs) */
|
/**Run command (use this to execute commands and process whole output at once, may not be suitable for large outputs) */
|
||||||
|
|||||||
Reference in New Issue
Block a user