Event: Increase robustness of an inner native event in leverageNative
[jquery.git] / build / release / dist.js
blob78dc5064f2d85d0a926b7ebee045252a888f744d
1 "use strict";
3 module.exports = function( Release, files, complete ) {
5         const fs = require( "node:fs/promises" );
6         const shell = require( "shelljs" );
7         const inquirer = require( "inquirer" );
8         const pkg = require( `${ Release.dir.repo }/package.json` );
9         const distRemote = Release.remote
11                 // For local and github dists
12                 .replace( /jquery(\.git|$)/, "jquery-dist$1" );
14         // These files are included with the distribution
15         const extras = [
16                 "src",
17                 "LICENSE.txt",
18                 "AUTHORS.txt",
19                 "dist/package.json",
20                 "dist/jquery.bundler-require-wrapper.js",
21                 "dist/jquery.bundler-require-wrapper.slim.js",
22                 "dist-module/package.json",
23                 "dist-module/jquery.node-module-wrapper.js",
24                 "dist-module/jquery.node-module-wrapper.slim.js"
25         ];
27         /**
28          * Clone the distribution repo
29          */
30         function clone() {
31                 Release.chdir( Release.dir.base );
32                 Release.dir.dist = `${ Release.dir.base }/dist`;
34                 console.log( "Using distribution repo: ", distRemote );
35                 Release.exec( `git clone ${ distRemote } ${ Release.dir.dist }`,
36                         "Error cloning repo." );
38                 // Distribution always works on main
39                 Release.chdir( Release.dir.dist );
40                 Release.exec( "git checkout main", "Error checking out branch." );
41                 console.log();
42         }
44         /**
45          * Generate bower file for jquery-dist
46          */
47         function generateBower() {
48                 return JSON.stringify( {
49                         name: pkg.name,
50                         main: pkg.main,
51                         license: "MIT",
52                         ignore: [
53                                 "package.json"
54                         ],
55                         keywords: pkg.keywords
56                 }, null, 2 );
57         }
59         /**
60          * Replace the version in the README
61          * @param {string} readme
62          * @param {string} blogPostLink
63          */
64         function editReadme( readme, blogPostLink ) {
65                 return readme
66                         .replace( /@VERSION/g, Release.newVersion )
67                         .replace( /@BLOG_POST_LINK/g, blogPostLink );
68         }
70         /**
71          * Copy necessary files over to the dist repo
72          */
73         async function copy() {
74                 const readme = await fs.readFile(
75                         `${ Release.dir.repo }/build/fixtures/README.md`, "utf8" );
76                 const rmIgnore = [ ...files, "node_modules" ]
77                         .map( file => `${ Release.dir.dist }/${ file }` );
79                 shell.config.globOptions = {
80                         ignore: rmIgnore
81                 };
83                 const { blogPostLink } = await inquirer.prompt( [ {
84                         type: "input",
85                         name: "blogPostLink",
86                         message: "Enter URL of the blog post announcing the jQuery release...\n"
87                 } ] );
89                 // Remove extraneous files before copy
90                 shell.rm( "-rf", `${ Release.dir.dist }/**/*` );
92                 // Copy dist files
93                 shell.mkdir( "-p", `${ Release.dir.dist }/dist` );
94                 shell.mkdir( "-p", `${ Release.dir.dist }/dist-module` );
95                 files.forEach( function( file ) {
96                         shell.cp(
97                                 "-f",
98                                 `${ Release.dir.repo }/${ file }`,
99                                 `${ Release.dir.dist }/${ file }`
100                         );
101                 } );
103                 // Copy other files
104                 extras.forEach( function( file ) {
105                         shell.cp(
106                                 "-rf",
107                                 `${ Release.dir.repo }/${ file }`,
108                                 `${ Release.dir.dist }/${ file }`
109                         );
110                 } );
112                 // Remove the wrapper & the ESLint config from the dist repo
113                 shell.rm( "-f", `${ Release.dir.dist }/src/wrapper.js` );
114                 shell.rm( "-f", `${ Release.dir.dist }/src/.eslintrc.json` );
116                 // Write package.json
117                 // Remove scripts and other superfluous properties,
118                 // especially the prepare script, which fails on the dist repo
119                 const packageJson = Object.assign( {}, pkg );
120                 delete packageJson.scripts;
121                 delete packageJson.devDependencies;
122                 delete packageJson.dependencies;
123                 delete packageJson.commitplease;
124                 packageJson.version = Release.newVersion;
125                 await fs.writeFile(
126                         `${ Release.dir.dist }/package.json`,
127                         JSON.stringify( packageJson, null, 2 )
128                 );
130                 // Write generated bower file
131                 await fs.writeFile( `${ Release.dir.dist }/bower.json`, generateBower() );
133                 await fs.writeFile( `${ Release.dir.dist }/README.md`,
134                         editReadme( readme, blogPostLink ) );
136                 console.log( "Files ready to add." );
137         }
139         /**
140          * Add, commit, and tag the dist files
141          */
142         function commit() {
143                 console.log( "Adding files to dist..." );
144                 Release.exec( "git add -A", "Error adding files." );
145                 Release.exec(
146                         `git commit -m "Release ${ Release.newVersion }"`,
147                         "Error committing files."
148                 );
149                 console.log();
151                 console.log( "Tagging release on dist..." );
152                 Release.exec( `git tag ${ Release.newVersion }`,
153                         `Error tagging ${ Release.newVersion } on dist repo.` );
154                 Release.tagTime = Release.exec( "git log -1 --format=\"%ad\"",
155                         "Error getting tag timestamp." ).trim();
156         }
158         /**
159          * Push files to dist repo
160          */
161         function push() {
162                 Release.chdir( Release.dir.dist );
164                 console.log( "Pushing release to dist repo..." );
165                 Release.exec(
166                         `git push ${
167                                 Release.isTest ? " --dry-run" : ""
168                         } ${ distRemote } main --tags`,
169                         "Error pushing main and tags to git repo."
170                 );
172                 // Set repo for npm publish
173                 Release.dir.origRepo = Release.dir.repo;
174                 Release.dir.repo = Release.dir.dist;
175         }
177         Release.walk( [
178                 Release._section( "Copy files to distribution repo" ),
179                 clone,
180                 copy,
181                 Release.confirmReview,
183                 Release._section( "Add, commit, and tag files in distribution repo" ),
184                 commit,
185                 Release.confirmReview,
187                 Release._section( "Pushing files to distribution repo" ),
188                 push
189         ], complete );