aboutsummaryrefslogtreecommitdiffstats
path: root/build/release/dist.js
blob: ec3890032ae223fe0f8ebe71cae1d92923797fc2 (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
import { readFile, writeFile } from "node:fs/promises";
import util from "node:util";
import { argv } from "node:process";
import { exec as nodeExec } from "node:child_process";
import { rimraf } from "rimraf";

const pkg = JSON.parse( await readFile( "./package.json", "utf8" ) );

const exec = util.promisify( nodeExec );

const version = argv[ 2 ];
const blogURL = argv[ 3 ];

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

if ( !blogURL || !blogURL.startsWith( "https://blog.jquery.com/" ) ) {
	throw new Error( "Invalid blog post URL" );
}

// The dist repo is cloned during release
const distRepoFolder = "tmp/release/dist";

// Files to be included in the dist repo.
// README.md and bower.json are generated.
// package.json is a simplified version of the original.
const files = [
	"dist",
	"src",
	"LICENSE.txt",
	"AUTHORS.txt",
	"changelog.md"
];

async function generateBower() {
	return JSON.stringify(
		{
			name: pkg.name,
			main: pkg.main,
			license: "MIT",
			ignore: [ "package.json" ],
			keywords: pkg.keywords
		},
		null,
		2
	);
}

async function generateReadme() {
	const readme = await readFile(
		"./build/fixtures/README.md",
		"utf8"
	);

	return readme
		.replace( /@VERSION/g, version )
		.replace( /@BLOG_POST_LINK/g, blogURL );
}

/**
 * Copy necessary files over to the dist repo
 */
async function copyFiles() {

	// Remove any extraneous files before copy
	await rimraf( [
		`${ distRepoFolder }/dist`,
		`${ distRepoFolder }/dist-module`,
		`${ distRepoFolder }/src`
	] );

	// Copy all files
	await Promise.all(
		files.map( function( path ) {
			console.log( `Copying ${ path }...` );
			return exec( `cp -rf ${ path } ${ distRepoFolder }/${ path }` );
		} )
	);

	// Remove the wrapper from the dist repo
	await rimraf( [
		`${ distRepoFolder }/src/wrapper.js`
	] );

	// Set the version in src/core.js
	const core = await readFile( `${ distRepoFolder }/src/core.js`, "utf8" );
	await writeFile(
		`${ distRepoFolder }/src/core.js`,
		core.replace( /@VERSION/g, version )
	);

	// Write generated README
	console.log( "Generating README.md..." );
	const readme = await generateReadme();
	await writeFile( `${ distRepoFolder }/README.md`, readme );

	// Write generated Bower file
	console.log( "Generating bower.json..." );
	const bower = await generateBower();
	await writeFile( `${ distRepoFolder }/bower.json`, bower );

	// Write simplified package.json
	console.log( "Writing package.json..." );
	await writeFile(
		`${ distRepoFolder }/package.json`,
		JSON.stringify(
			{
				...pkg,
				scripts: undefined,
				dependencies: undefined,
				devDependencies: undefined,
				commitplease: undefined
			},
			null,
			2

		// Add final newline
		) + "\n"
	);

	console.log( "Files copied to dist repo." );
}

copyFiles();