2023-10-24 19:00:18 +03:00
|
|
|
const { join, dirname, relative, extname, basename } = require("path")
|
|
|
|
const { readFileSync, writeFileSync, existsSync, readdirSync, lstatSync, copyFileSync, mkdirSync, rmSync, current } = require('fs')
|
|
|
|
const { spawnSync, execSync } = require('child_process')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
|
|
|
|
if(!existsSync('.format')) {
|
|
|
|
mkdirSync('.format', { recursive: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
let hash = {}
|
|
|
|
|
|
|
|
if( existsSync('.format/format.json') ) {
|
|
|
|
hash = JSON.parse(readFileSync('.format/format.json').toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
let filesToCheck = []
|
|
|
|
let allFiles = []
|
|
|
|
|
|
|
|
let newHash = {}
|
|
|
|
|
|
|
|
function calcHash(source, msg) {
|
|
|
|
const files = readdirSync(source)
|
|
|
|
for (const f of files) {
|
|
|
|
const sourceFile = join(source, f)
|
|
|
|
|
|
|
|
if (lstatSync(sourceFile).isDirectory()) {
|
|
|
|
calcHash(sourceFile, msg)
|
|
|
|
} else {
|
|
|
|
let ext = basename(sourceFile)
|
|
|
|
if( !ext.endsWith('.ts') && !ext.endsWith('.js') && !ext.endsWith('.svelte')) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const hasher = crypto.createHash('md5')
|
|
|
|
hasher.update(readFileSync(sourceFile))
|
|
|
|
let digest = hasher.digest('hex')
|
|
|
|
if( hash[sourceFile] !== digest ) {
|
|
|
|
filesToCheck.push(sourceFile)
|
|
|
|
console.log(msg, relative(process.cwd(), sourceFile))
|
|
|
|
}
|
|
|
|
newHash[sourceFile] = digest
|
|
|
|
allFiles.push(sourceFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const v of process.argv.slice(2)) {
|
|
|
|
if( existsSync(v) ) {
|
2023-10-25 10:42:04 +03:00
|
|
|
console.info('checking:', join( process.cwd(), v) )
|
2023-10-24 19:00:18 +03:00
|
|
|
calcHash(join(process.cwd(), v), 'changed')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( process.argv.includes('-f')) {
|
|
|
|
filesToCheck = allFiles
|
|
|
|
}
|
|
|
|
|
|
|
|
if( filesToCheck.length > 0 ) {
|
2023-10-25 10:42:04 +03:00
|
|
|
console.info(`running prettier ${filesToCheck.length}`)
|
2023-10-24 19:00:18 +03:00
|
|
|
// Changes detected.
|
|
|
|
const prettier = spawnSync(join(process.cwd(), 'node_modules/.bin/prettier'), ["--color", "--plugin-search-dir=.", "--write", ...filesToCheck],)
|
|
|
|
if( prettier.stdout != null) {
|
|
|
|
writeFileSync('.format/prettier.log', prettier.stdout)
|
2023-10-25 10:42:04 +03:00
|
|
|
if( prettier.status === null || prettier.status === 0) {
|
|
|
|
console.info(prettier.stdout.toString())
|
|
|
|
} else {
|
|
|
|
console.error(prettier.stdout.toString())
|
|
|
|
}
|
2023-10-24 19:00:18 +03:00
|
|
|
}
|
|
|
|
if( prettier.stderr != null) {
|
|
|
|
writeFileSync('.format/prettier.err', prettier.stderr)
|
2023-10-25 10:42:04 +03:00
|
|
|
const data = prettier.stderr.toString()
|
|
|
|
if( data.length > 0) {
|
|
|
|
console.error(data)
|
|
|
|
}
|
2023-10-24 19:00:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`running eslint ${filesToCheck.length}`)
|
|
|
|
const eslint = spawnSync(join(process.cwd(), 'node_modules/.bin/eslint'), ["--color", "--fix", ...filesToCheck])
|
|
|
|
if(eslint.stdout != null) {
|
|
|
|
writeFileSync('.format/eslint.log', eslint.stdout)
|
2023-10-25 10:42:04 +03:00
|
|
|
if( prettier.status === null || prettier.status === 0) {
|
|
|
|
console.info(eslint.stdout.toString())
|
|
|
|
} else {
|
|
|
|
console.error(eslint.stdout.toString())
|
|
|
|
}
|
2023-10-24 19:00:18 +03:00
|
|
|
}
|
|
|
|
if( eslint.stderr != null) {
|
|
|
|
writeFileSync('.format/eslint.err', eslint.stderr)
|
2023-10-25 10:42:04 +03:00
|
|
|
const data = eslint.stderr.toString()
|
|
|
|
if( data.length > 0) {
|
|
|
|
console.error(data)
|
|
|
|
}
|
2023-10-24 19:00:18 +03:00
|
|
|
}
|
|
|
|
if( prettier.status || eslint.status) {
|
2023-10-25 10:42:04 +03:00
|
|
|
console.info('prettier and eslint failed', prettier.status, eslint.status)
|
2023-10-24 19:00:18 +03:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = newHash
|
|
|
|
for( const v of process.argv.slice(2)) {
|
|
|
|
if( existsSync(v) ) {
|
|
|
|
calcHash(join(process.cwd(), v), 'updated')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeFileSync('.format/format.json', JSON.stringify(newHash, undefined, 2))
|
|
|
|
} else {
|
2023-10-25 10:42:04 +03:00
|
|
|
console.info('No changes detected.')
|
2023-10-24 19:00:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
process.exit(0)
|
|
|
|
|