/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

const { spawn } = require( 'child_process' );
const vnu = require( 'vnu-jar' );

module.exports = ( buildPath, { verbose } = {} ) => new Promise( ( resolve, reject ) => {
	const args = [ '-jar', vnu, '--skip-non-html', '--also-check-css', '--exit-zero-always', '--format', 'text' ];
	if ( !verbose ) {
		args.push( '--errors-only' );
	}
	args.push( buildPath );
	const proc = spawn( 'java', args );

	proc.stderr.on( 'data', data => {
		process.stdout.write( data );
	} );

	proc.stdout.on( 'data', data => {
		process.stdout.write( data );
	} );

	proc.on( 'close', code => {
		if ( code !== 0 ) {
			console.log( `Process exit with code: ${ code }` );
			reject( code );
		}
		else {
			resolve();
		}
	} );
} );

