minetest_x_bows/scripts/lls-check.js

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-11-28 14:46:55 -06:00
/**
* Run LUA diagnostics in continuous integration
* Copyright (C) 2022 SaKeL <juraj.vajda@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to juraj.vajda@gmail.com
*/
2022-11-03 10:37:33 -05:00
import * as path from 'node:path'
import * as fs from 'node:fs'
import {exec} from 'node:child_process'
import yargs from 'yargs/yargs'
import {hideBin} from 'yargs/helpers'
2022-11-03 12:36:07 -05:00
import jaguar from 'jaguar'
2022-11-03 09:24:57 -05:00
2022-11-03 10:37:33 -05:00
const argv = yargs(hideBin(process.argv)).argv
2022-11-03 12:36:07 -05:00
const cwd = process.cwd()
const logPath = path.join(cwd, 'logs')
2022-11-03 09:24:57 -05:00
2022-11-03 12:36:07 -05:00
// Extract lua language server
const from = path.join(cwd, 'bin/lua-language-server-3.5.6-linux-x64.tar.gz');
const to = path.join(cwd, 'bin', 'lua-language-server-3.5.6-linux-x64');
const extract = jaguar.extract(from, to)
2022-11-03 12:48:19 -05:00
// extract.on('file', (name) => {
// console.log(name)
// })
2022-11-03 12:36:07 -05:00
2022-11-03 12:48:19 -05:00
extract.on('start', () => {
console.log('Extracting...')
2022-11-03 12:36:07 -05:00
})
2022-11-03 12:48:19 -05:00
// extract.on('progress', (percent) => {
// console.log(percent + '%')
// })
2022-11-03 12:36:07 -05:00
extract.on('error', (error) => {
console.error(error)
process.exit(1)
})
extract.on('end', () => {
2022-11-03 12:48:19 -05:00
console.log('Extracting: Done')
2022-11-03 12:36:07 -05:00
// Delete directory recursively
try {
fs.rmSync(logPath, { recursive: true, force: true })
console.log(`Removed folder: ${logPath}`)
} catch (err) {
console.error(`Error while deleting ${logPath}.`)
console.error(err)
2022-11-03 10:37:33 -05:00
}
2022-11-03 12:36:07 -05:00
let command = './bin/lua-language-server-3.5.6-linux-x64/bin/lua-language-server'
if (argv.local) {
command = 'lua-language-server'
2022-11-03 10:37:33 -05:00
}
2022-11-03 12:36:07 -05:00
exec(`${command} --logpath "${logPath}" --check "${cwd}"`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
2022-11-03 12:48:19 -05:00
process.exit(1)
2022-11-03 12:36:07 -05:00
}
if (stderr) {
console.log(`stderr: ${stderr}`)
2022-11-03 12:48:19 -05:00
process.exit(1)
2022-11-03 12:36:07 -05:00
}
2022-11-03 12:06:43 -05:00
2022-11-03 12:48:19 -05:00
console.log(`\n${stdout}`)
2022-11-03 10:37:33 -05:00
2022-11-03 12:36:07 -05:00
if (fs.existsSync('./logs/check.json')) {
const rawdata = fs.readFileSync('./logs/check.json')
const diagnosticsJson = JSON.parse(rawdata)
2022-11-03 10:37:33 -05:00
2022-11-03 12:36:07 -05:00
Object.keys(diagnosticsJson).forEach((key) => {
console.log(key)
diagnosticsJson[key].forEach((errObj) => {
console.log(`line: ${errObj.range.start.line} - ${errObj.message}`)
})
2022-11-03 12:06:43 -05:00
})
2022-11-03 10:37:33 -05:00
2022-11-03 12:36:07 -05:00
console.error('Fix the errors/warnings above.')
process.exit(1)
}
// Delete directory recursively
const llsFolder = path.join(cwd, 'bin', 'lua-language-server-3.5.6-linux-x64')
2022-11-03 12:48:19 -05:00
2022-11-03 12:36:07 -05:00
try {
fs.rmSync(llsFolder, { recursive: true, force: true })
console.log(`Removed folder: ${llsFolder}`)
} catch (err) {
console.error(`Error while deleting ${llsFolder}.`)
console.error(err)
process.exit(1)
}
})
2022-11-03 10:37:33 -05:00
})