aboutsummaryrefslogtreecommitdiffstats
path: root/build/release/cdn.js
blob: 7d94926234dab508673caf4a93dda35f03538fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { mkdir, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { argv } from "node:process";
import util from "node:util";
import { exec as nodeExec } from "node:child_process";
import { rimraf } from "rimraf";
import archive from "./archive.js";

const exec = util.promisify( nodeExec );

const version = argv[ 2 ];

if ( !version ) {
	throw new Error( "No version specified" );
}

const archivesFolder = "tmp/archives";
const versionedFolder = `${ archivesFolder }/versioned`;
const unversionedFolder = `${ archivesFolder }/unversioned`;

// The cdn repo is cloned during release
const cdnRepoFolder = "tmp/release/cdn";

// .min.js and .min.map files are expected
// in the same directory as the uncompressed files.
const sources = [
	"dist/jquery.js",
	"dist/jquery.slim.js",
	"dist-module/jquery.module.js",
	"dist-module/jquery.slim.module.js"
];

const rminmap = /\.min\.map$/;
const rjs = /\.js$/;

function clean() {
	console.log( "Cleaning any existing archives..." );
	return rimraf( archivesFolder );
}

// Map files need to reference the new uncompressed name;
// assume that all files reside in the same directory.
// "file":"jquery.min.js" ... "sources":["jquery.js"]
// This is only necessary for the versioned files.
async function convertMapToVersioned( file, folder ) {
	const mapFile = file.replace( /\.js$/, ".min.map" );
	const filename = path
		.basename( mapFile )
		.replace( "jquery", "jquery-" + version );

	const contents = JSON.parse( await readFile( mapFile, "utf8" ) );

	return writeFile(
		path.join( folder, filename ),
		JSON.stringify( {
			...contents,
			file: filename.replace( rminmap, ".min.js" ),
			sources: [ filename.replace( rminmap, ".js" ) ]
		} )
	);
}

async function makeUnversionedCopies() {
	await mkdir( unversionedFolder, { recursive: true } );

	return Promise.all(
		sources.map( async( file ) => {
			const filename = path.basename( file );
			const minFilename = filename.replace( rjs, ".min.js" );
			const mapFilename = filename.replace( rjs, ".min.map" );

			await exec( `cp -f ${ file } ${ unversionedFolder }/${ filename }` );
			await exec(
				`cp -f ${ file.replace(
					rjs,
					".min.js"
				) } ${ unversionedFolder }/${ minFilename }`
			);
			await exec(
				`cp -f ${ file.replace(
					rjs,
					".min.map"
				) } ${ unversionedFolder }/${ mapFilename }`
			);
		} )
	);
}

async function makeVersionedCopies() {
	await mkdir( versionedFolder, { recursive: true } );

	return Promise.all(
		sources.map( async( file ) => {
			const filename = path
				.basename( file )
				.replace( "jquery", "jquery-" + version );
			const minFilename = filename.replace( rjs, ".min.js" );

			await exec( `cp -f ${ file } ${ versionedFolder }/${ filename }` );
			await exec(
				`cp -f ${ file.replace(
					rjs,
					".min.js"
				) } ${ versionedFolder }/${ minFilename }`
			);
			await convertMapToVersioned( file, versionedFolder );
		} )
	);
}

async function copyToRepo( folder ) {
	return exec( `cp -f ${ folder }/* ${ cdnRepoFolder }/cdn/` );
}

async function cdn() {
	await clean();

	await Promise.all( [ makeUnversionedCopies(), makeVersionedCopies() ] );

	await copyToRepo( versionedFolder );

	await Promise.all( [
		archive( { cdn: "googlecdn", folder: unversionedFolder, version } ),
		archive( { cdn: "mscdn", folder: versionedFolder, version } )
	] );

	console.log( "Files ready for CDNs." );
}

cdn();