@@ -1269,6 +1269,46 @@ export const format = task({
12691269
12701270async function runFormat ( ) {
12711271 await $ `dprint fmt` ;
1272+ await fixLineEndings ( ) ;
1273+ }
1274+
1275+ const lineEndingExclusions = [
1276+ ":(exclude)tsc/testdata/**" ,
1277+ // TODO: Remove this exclusion when this directory is converted or removed.
1278+ ":(exclude)tsc/internal/locale/lcl/**" ,
1279+ ] ;
1280+
1281+ export const fixLineEndingsTask = task ( {
1282+ name : "fix:line-endings" ,
1283+ description : "Converts tracked files that Git expects to use LF." ,
1284+ run : fixLineEndings ,
1285+ } ) ;
1286+
1287+ async function fixLineEndings ( ) {
1288+ const result = await _$ `git ls-files --eol -z -- . ${ lineEndingExclusions } ` ;
1289+ const files = [ ] ;
1290+ for ( const record of result . stdout . split ( "\0" ) ) {
1291+ if ( ! record ) {
1292+ continue ;
1293+ }
1294+ const separator = record . indexOf ( "\t" ) ;
1295+ assert ( separator !== - 1 ) ;
1296+ const eolInfo = record . slice ( 0 , separator ) ;
1297+ if ( ! / \b w \/ (?: c r l f | m i x e d ) \b / . test ( eolInfo ) || ! eolInfo . includes ( "eol=lf" ) ) {
1298+ continue ;
1299+ }
1300+ files . push ( record . slice ( separator + 1 ) ) ;
1301+ }
1302+
1303+ await Promise . all ( files . map ( async file => {
1304+ const contents = await fs . promises . readFile ( file ) ;
1305+ const normalized = Buffer . from ( contents . toString ( "latin1" ) . replaceAll ( "\r\n" , "\n" ) , "latin1" ) ;
1306+ await fs . promises . writeFile ( file , normalized ) ;
1307+ } ) ) ;
1308+
1309+ if ( files . length ) {
1310+ console . log ( `Converted ${ files . length } file(s) to LF.` ) ;
1311+ }
12721312}
12731313
12741314export const checkFormat = task ( {
@@ -1281,12 +1321,7 @@ export const checkFormat = task({
12811321} ) ;
12821322
12831323async function checkLineEndings ( ) {
1284- const exclusions = [
1285- ":(exclude)tsc/testdata/**" ,
1286- // TODO: Remove this exclusion when this directory is converted or removed.
1287- ":(exclude)tsc/internal/locale/lcl/**" ,
1288- ] ;
1289- const result = await _$ ( { reject : false } ) `git grep --cached -Il ${ "\r" } -- . ${ exclusions } ` ;
1324+ const result = await _$ ( { reject : false } ) `git grep --cached -Il ${ "\r" } -- . ${ lineEndingExclusions } ` ;
12901325 if ( result . exitCode === 1 ) {
12911326 return ;
12921327 }
0 commit comments