Release: copy dist-module folder as well
[jquery.git] / build / release / dist.js
blobad378468fba13052d8d2a2a33461af3271d77db2
1 "use strict";
3 module.exports = function( Release, files, complete ) {
5         const fs = require( "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-module/package.json",
21                 "dist-module/jquery.node-module-wrapper.js",
22                 "dist-module/jquery.node-module-wrapper.slim.js"
23         ];
25         /**
26          * Clone the distribution repo
27          */
28         function clone() {
29                 Release.chdir( Release.dir.base );
30                 Release.dir.dist = `${ Release.dir.base }/dist`;
32                 console.log( "Using distribution repo: ", distRemote );
33                 Release.exec( `git clone ${ distRemote } ${ Release.dir.dist }`,
34                         "Error cloning repo." );
36                 // Distribution always works on main
37                 Release.chdir( Release.dir.dist );
38                 Release.exec( "git checkout main", "Error checking out branch." );
39                 console.log();
40         }
42         /**
43          * Generate bower file for jquery-dist
44          */
45         function generateBower() {
46                 return JSON.stringify( {
47                         name: pkg.name,
48                         main: pkg.main,
49                         license: "MIT",
50                         ignore: [
51                                 "package.json"
52                         ],
53                         keywords: pkg.keywords
54                 }, null, 2 );
55         }
57         /**
58          * Replace the version in the README
59          * @param {string} readme
60          * @param {string} blogPostLink
61          */
62         function editReadme( readme, blogPostLink ) {
63                 return readme
64                         .replace( /@VERSION/g, Release.newVersion )
65                         .replace( /@BLOG_POST_LINK/g, blogPostLink );
66         }
68         /**
69          * Copy necessary files over to the dist repo
70          */
71         async function copy() {
72                 const readme = await fs.readFile(
73                         `${ Release.dir.repo }/build/fixtures/README.md`, "utf8" );
74                 const rmIgnore = [ ...files, "node_modules" ]
75                         .map( file => `${ Release.dir.dist }/${ file }` );
77                 shell.config.globOptions = {
78                         ignore: rmIgnore
79                 };
81                 const { blogPostLink } = await inquirer.prompt( [ {
82                         type: "input",
83                         name: "blogPostLink",
84                         message: "Enter URL of the blog post announcing the jQuery release...\n"
85                 } ] );
87                 // Remove extraneous files before copy
88                 shell.rm( "-rf", `${ Release.dir.dist }/**/*` );
90                 // Copy dist files
91                 shell.mkdir( "-p", `${ Release.dir.dist }/dist` );
92                 shell.mkdir( "-p", `${ Release.dir.dist }/dist-module` );
93                 files.forEach( function( file ) {
94                         shell.cp(
95                                 "-f",
96                                 `${ Release.dir.repo }/${ file }`,
97                                 `${ Release.dir.dist }/${ file }`
98                         );
99                 } );
101                 // Copy other files
102                 extras.forEach( function( file ) {
103                         shell.cp(
104                                 "-rf",
105                                 `${ Release.dir.repo }/${ file }`,
106                                 `${ Release.dir.dist }/${ file }`
107                         );
108                 } );
110                 // Remove the wrapper & the ESLint config from the dist repo
111                 shell.rm( "-f", `${ Release.dir.dist }/src/wrapper.js` );
112                 shell.rm( "-f", `${ Release.dir.dist }/src/.eslintrc.json` );
114                 // Write package.json
115                 // Remove scripts and other superfluous properties,
116                 // especially the prepare script, which fails on the dist repo
117                 const packageJson = Object.assign( {}, pkg );
118                 delete packageJson.scripts;
119                 delete packageJson.devDependencies;
120                 delete packageJson.dependencies;
121                 delete packageJson.commitplease;
122                 await fs.writeFile(
123                         `${ Release.dir.dist }/package.json`,
124                         JSON.stringify( packageJson, null, 2 )
125                 );
127                 // Write generated bower file
128                 await fs.writeFile( `${ Release.dir.dist }/bower.json`, generateBower() );
130                 await fs.writeFile( `${ Release.dir.dist }/README.md`,
131                         editReadme( readme, blogPostLink ) );
133                 console.log( "Files ready to add." );
134         }
136         /**
137          * Add, commit, and tag the dist files
138          */
139         function commit() {
140                 console.log( "Adding files to dist..." );
141                 Release.exec( "git add -A", "Error adding files." );
142                 Release.exec(
143                         `git commit -m "Release ${ Release.newVersion }"`,
144                         "Error committing files."
145                 );
146                 console.log();
148                 console.log( "Tagging release on dist..." );
149                 Release.exec( `git tag ${ Release.newVersion }`,
150                         `Error tagging ${ Release.newVersion } on dist repo.` );
151                 Release.tagTime = Release.exec( "git log -1 --format=\"%ad\"",
152                         "Error getting tag timestamp." ).trim();
153         }
155         /**
156          * Push files to dist repo
157          */
158         function push() {
159                 Release.chdir( Release.dir.dist );
161                 console.log( "Pushing release to dist repo..." );
162                 Release.exec(
163                         `git push ${
164                                 Release.isTest ? " --dry-run" : ""
165                         } ${ distRemote } main --tags`,
166                         "Error pushing main and tags to git repo."
167                 );
169                 // Set repo for npm publish
170                 Release.dir.origRepo = Release.dir.repo;
171                 Release.dir.repo = Release.dir.dist;
172         }
174         Release.walk( [
175                 Release._section( "Copy files to distribution repo" ),
176                 clone,
177                 copy,
178                 Release.confirmReview,
180                 Release._section( "Add, commit, and tag files in distribution repo" ),
181                 commit,
182                 Release.confirmReview,
184                 Release._section( "Pushing files to distribution repo" ),
185                 push
186         ], complete );