2019-10-08 22:17:55 +00:00
|
|
|
"use strict";
|
2013-08-16 13:48:00 +00:00
|
|
|
|
2023-09-18 16:39:00 +00:00
|
|
|
// Process files for distribution.
|
2023-09-19 16:58:24 +00:00
|
|
|
module.exports = function processForDist( text, filename ) {
|
2023-09-18 16:39:00 +00:00
|
|
|
if ( !text ) {
|
|
|
|
throw new Error( "text required for processForDist" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !filename ) {
|
|
|
|
throw new Error( "filename required for processForDist" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure files use only \n for line endings, not \r\n
|
|
|
|
if ( /\x0d\x0a/.test( text ) ) {
|
|
|
|
throw new Error( filename + ": Incorrect line endings (\\r\\n)" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure only ASCII chars so script tags don't need a charset attribute
|
|
|
|
if ( text.length !== Buffer.byteLength( text, "utf8" ) ) {
|
|
|
|
let message = filename + ": Non-ASCII characters detected:\n";
|
|
|
|
for ( let i = 0; i < text.length; i++ ) {
|
|
|
|
const c = text.charCodeAt( i );
|
|
|
|
if ( c > 127 ) {
|
|
|
|
message += "- position " + i + ": " + c + "\n";
|
|
|
|
message += "==> " + text.substring( i - 20, i + 20 );
|
|
|
|
break;
|
2013-08-16 13:48:00 +00:00
|
|
|
}
|
2023-09-18 16:39:00 +00:00
|
|
|
}
|
|
|
|
throw new Error( message );
|
|
|
|
}
|
2013-08-16 13:48:00 +00:00
|
|
|
};
|