3 const fs = require( "node:fs/promises" );
4 const util = require( "node:util" );
5 const exec = util.promisify( require( "node:child_process" ).exec );
6 const rnewline = /\r?\n/;
7 const rdate = /^\[(\d+)\] /;
13 function compareAuthors( a, b ) {
14 const aName = a.replace( rdate, "" ).replace( / <.*>/, "" );
15 const bName = b.replace( rdate, "" ).replace( / <.*>/, "" );
16 return aName === bName;
19 function uniq( arr ) {
21 for ( const item of arr ) {
22 if ( ignore.some( re => re.test( item ) ) ) {
25 if ( item && !unique.find( ( e ) => compareAuthors( e, item ) ) ) {
32 function cleanupSizzle() {
33 console.log( "Cleaning up..." );
34 return exec( "npx rimraf .sizzle" );
37 function cloneSizzle() {
38 console.log( "Cloning Sizzle..." );
39 return exec( "git clone https://github.com/jquery/sizzle .sizzle" );
42 async function getLastAuthor() {
43 const authorsTxt = await fs.readFile( "AUTHORS.txt", "utf8" );
44 return authorsTxt.trim().split( rnewline ).pop();
47 async function logAuthors( preCommand ) {
48 let command = "git log --pretty=format:\"[%at] %aN <%aE>\"";
50 command = `${ preCommand } && ${ command }`;
52 const { stdout } = await exec( command );
53 return uniq( stdout.trim().split( rnewline ).reverse() );
56 async function getSizzleAuthors() {
58 const authors = await logAuthors( "cd .sizzle" );
59 await cleanupSizzle();
63 function sortAuthors( a, b ) {
64 const [ , aDate ] = rdate.exec( a );
65 const [ , bDate ] = rdate.exec( b );
66 return Number( aDate ) - Number( bDate );
69 function formatAuthor( author ) {
70 return author.replace( rdate, "" );
73 async function getAuthors() {
74 console.log( "Getting authors..." );
75 const authors = await logAuthors();
76 const sizzleAuthors = await getSizzleAuthors();
77 return uniq( authors.concat( sizzleAuthors ) ).sort( sortAuthors ).map( formatAuthor );
80 async function checkAuthors() {
81 const authors = await getAuthors();
82 const lastAuthor = await getLastAuthor();
84 if ( authors[ authors.length - 1 ] !== lastAuthor ) {
85 console.log( "AUTHORS.txt: ", lastAuthor );
86 console.log( "Last 20 in git: ", authors.slice( -20 ) );
87 throw new Error( "Last author in AUTHORS.txt does not match last git author" );
89 console.log( "AUTHORS.txt is up to date" );
92 async function updateAuthors() {
93 const authors = await getAuthors();
95 const authorsTxt = "Authors ordered by first contribution.\n\n" + authors.join( "\n" ) + "\n";
96 await fs.writeFile( "AUTHORS.txt", authorsTxt );
98 console.log( "AUTHORS.txt updated" );