forked from Automattic/themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-utils.mjs
697 lines (592 loc) · 19.7 KB
/
theme-utils.mjs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import { spawn } from 'child_process';
import fs from 'fs';
import open from 'open';
import inquirer from 'inquirer';
const remoteSSH = 'wpcom-sandbox';
const sandboxPublicThemesFolder = '/home/wpdev/public_html/wp-content/themes/pub';
const sandboxRootFolder = '/home/wpdev/public_html/';
const isWin = process.platform === 'win32';
(async function start() {
let args = process.argv.slice(2);
let command = args?.[0];
switch (command) {
case "push-button-deploy-git": return pushButtonDeploy('git');
case "push-button-deploy-svn": return pushButtonDeploy('svn');
case "clean-sandbox-git": return cleanSandboxGit();
case "clean-sandbox-svn": return cleanSandboxSvn();
case "clean-all-sandbox-git": return cleanAllSandboxGit();
case "clean-all-sandbox-svn": return cleanAllSandboxSvn();
case "push-to-sandbox": return pushToSandbox();
case "push-changes-to-sandbox": return pushChangesToSandbox();
case "version-bump-themes": return versionBumpThemes();
case "land-diff-git": return landChangesGit(args?.[1]);
case "land-diff-svn": return landChangesSvn(args?.[1]);
case "deploy-preview": return deployPreview();
}
return showHelp();
})();
function showHelp(){
// TODO: make this helpful
console.log('Help info can go here');
}
/*
Determine what changes would be deployed
*/
async function deployPreview() {
console.clear();
console.log('To ensure accuracy clean your sandbox before previewing. (It is not automatically done).');
console.log('npm run sandbox:clean:git OR npm run sandbox:clean:svn')
let message = await checkForDeployability();
if (message) {
console.log(`\n${message}\n\n`);
}
let hash = await getLastDeployedHash();
console.log(`Last deployed hash: ${hash}`);
let changedThemes = await getChangedThemes(hash);
console.log(`The following themes have changes:\n${changedThemes}`);
let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
}
/*
Execute the first phase of a deployment.
Leverages git on the sandbox.
* Gets the last deployed hash from the sandbox
* Version bump all themes have have changes since the last deployment
* Commit the version bump change to github
* Clean the sandbox and ensure it is up-to-date
* Push all changed files (including removal of deleted files) since the last deployment
* Update the 'last deployed' hash on the sandbox
* Create a phabricator diff based on the changes since the last deployment. The description including the commit messages since the last deployment.
* Open the Phabricator Diff in your browser
* Create a tag in the github repository at this point of change which includes the phabricator link in the description
*/
async function pushButtonDeploy(repoType) {
console.clear();
let prompt = await inquirer.prompt([{
type: 'confirm',
message: 'You are about to deploy /trunk. Are you ready to continue?',
name: "continue",
default: false
}]);
if(!prompt.continue){
return;
}
if (repoType != 'svn' && repoType != 'git' ) {
return console.log('Specify a repo type to use push-button deploy');
}
let message = await checkForDeployability();
if (message) {
return console.log(`\n\n${message}\n\n`);
}
try {
if (repoType === 'git' ) {
await cleanSandboxGit();
}
else {
await cleanSandboxSvn();
}
let hash = await getLastDeployedHash();
let diffUrl;
await versionBumpThemes();
let changedThemes = await getChangedThemes(hash);
//TODO: Can these be automagically uploaded?
//await buildChangedOrgZips();
await pushChangesToSandbox();
await updateLastDeployedHash();
if (repoType === 'git' ) {
diffUrl = await createGitPhabricatorDiff(hash);
}
else {
diffUrl = await createSvnPhabricatorDiff(hash);
}
let diffId = diffUrl.split('a8c.com/')[1];
//push changes (from version bump)
await executeCommand('git push');
await tagDeployment({
hash: hash,
diffId: diffId
});
console.log(`\n\nPhase One Complete\n\nYour sandbox has been updated and the diff is available for review.\nPlease give your sandbox a smoke test to determine that the changes work as expected.\nThe following themes have had changes: \n\n${changedThemes}\n\n\n`);
prompt = await inquirer.prompt([{
type: 'confirm',
message: 'Are you ready to land these changes?',
name: "continue",
default: false
}]);
if(!prompt.continue){
console.log(`Aborted Automated Deploy Process Landing Phase\n\nYou will have to land these changes manually. The ID of the diff to land: ${diffId}` );
return;
}
if (repoType === 'git' ) {
await landChangesGit(diffId);
}
else {
await landChangesSvn(diffId);
}
open('https://mc.a8c.com/themes/downloads/');
console.log(`The following themes have changed:\n${changedThemes.join('\n')}`)
console.log('Please deploy the following themes manually.' );
console.log('Please build the .zip files for the themes manually.');
console.log('\n\nAll Done!!\n\n');
}
catch (err) {
console.log("ERROR with deply script: ", err);
}
}
/*
Check to ensure that:
* The current branch is /trunk
* That trunk is up-to-date with origin/trunk
*/
async function checkForDeployability(){
let branchName = await executeCommand('git symbolic-ref --short HEAD');
if(branchName !== 'trunk' ) {
return 'Only the /trunk branch can be deployed.';
}
await executeCommand('git remote update', true);
let localMasterHash = await executeCommand('git rev-parse trunk')
let remoteMasterHash = await executeCommand('git rev-parse origin/trunk')
if(localMasterHash !== remoteMasterHash) {
return 'Local /trunk is out-of-date. Pull changes to continue.'
}
return null;
}
/*
Land the changes from the given diff ID. This is the "production merge".
This is the git version of that action.
*/
async function landChangesGit(diffId){
return await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
arc patch ${diffId}
arc land
`, true);
}
/*
Land the changes from the given diff ID. This is the "production merge".
This is the svn version of that action.
*/
async function landChangesSvn(diffId){
return await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
svn ci -m ${diffId}
`, true);
}
async function getChangedThemes(hash) {
console.log('Determining all changed themes');
let themes = await getActionableThemes();
let changedThemes = [];
for (let theme of themes) {
let hasChanges = await checkThemeForChanges(theme, hash);
if(hasChanges){
changedThemes.push(theme.replace('./', ''));
}
}
return changedThemes;
}
/*
Work-in-progress
For reasons I don't understand this command is not working when ran this way.
"-bash: line 3: dploy: command not found"
*/
async function deployThemes(themes) {
let response;
for (let theme of themes ) {
console.log(theme);
response = await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
deploy pub ${theme}
`, true);
//TODO: if the response wasn't happy then prompt to try again.
}
}
/*
Provide the hash of the last managed deployment.
This hash is used to determine all the changes that have happened between that point and the current point.
*/
async function getLastDeployedHash() {
let result = await executeOnSandbox(`
cat ${sandboxPublicThemesFolder}/.pub-git-hash
`);
return result;
}
/*
Update the 'last deployed hash' on the server with the current hash.
*/
async function updateLastDeployedHash() {
let hash = await executeCommand(`git rev-parse HEAD`);
await executeOnSandbox(`
echo '${hash}' > ${sandboxPublicThemesFolder}/.pub-git-hash
`);
}
/*
Version bump (increment version patch) any theme project that has had changes since the last deployment.
If a theme's version has already been changed since that last deployment then do not version bump it.
If any theme projects have had a version bump also version bump the parent project.
Commit the change.
*/
async function versionBumpThemes() {
console.log("Version Bumping");
let themes = await getActionableThemes();
let hash = await getLastDeployedHash();
let versionBumpCount = 0;
for (let theme of themes) {
let hasChanges = await checkThemeForChanges(theme, hash);
if( ! hasChanges){
// console.log(`${theme} has no changes`);
continue;
}
versionBumpCount++;
let hasVersionBump = await checkThemeForVersionBump(theme, hash);
if( hasVersionBump ){
console.log(`${theme} has already been version bumped`);
continue;
}
await versionBumpTheme(theme);
}
//version bump the root project if there were changes to any of the themes
let rootHasVersionBump = await checkThemeForVersionBump('.', hash);
if ( versionBumpCount > 0 && ! rootHasVersionBump ) {
await executeCommand(`npm version patch --no-git-tag-version`);
}
if (versionBumpCount > 0 && !rootHasVersionBump) {
console.log('commiting version-bump');
await executeCommand(`
git commit -a -m "Version Bump";
`, true);
}
}
/*
Version Bump a Theme.
Used by versionBumpThemes to do the work of version bumping.
First increment the patch version in package.json (the source of truth for versioning)
Then update any of these files with the new version: [style.css, style.scss, style-child-theme.scss]
*/
async function versionBumpTheme(theme){
console.log(`${theme} needs a version bump`);
await executeCommand(`npm --prefix ${theme} version patch --no-git-tag-version`);
let currentPackage = JSON.parse(fs.readFileSync(`${theme}/package.json`))
let currentVersion = currentPackage.version;
let filesToUpdate = await executeCommand(`find ${theme} -name style.css -o -name style.scss -o -name style-child-theme.scss -maxdepth 2`);
filesToUpdate = filesToUpdate.split('\n');
for ( let file of filesToUpdate ) {
console.log('updating file', file, currentVersion);
//TODO: I'm sure we can use something other than perl for this but this was prior art...
await executeCommand(`perl -pi -e 's/Version: (.*)$/"Version: '${currentVersion}'"/ge' ${file}`);
}
}
/*
Determine if a theme has had a version bump since a given hash.
Used by versionBumpThemes
Compares the value of 'version' in package.json between the hash and current value
*/
async function checkThemeForVersionBump(theme, hash){
executeCommand(`
git show ${hash}:${theme}/package.json 2>/dev/null
`).catch( ( error ) => {
console.log( 'This is a new theme, no need to bump versions' );
return false;
} ).then( ( previousPackageString ) => {
let previousPackage = JSON.parse(previousPackageString);
let currentPackage = JSON.parse(fs.readFileSync(`${theme}/package.json`))
return previousPackage.version != currentPackage.version;
});
}
/*
Determine if a theme has had changes since a given hash.
Used by versionBumpThemes
*/
async function checkThemeForChanges(theme, hash){
let uncomittedChanges = await executeCommand(`git diff-index --name-only HEAD -- ${theme}`);
let comittedChanges = await executeCommand(`git diff --name-only ${hash} HEAD -- ${theme}`);
return uncomittedChanges != '' || comittedChanges != '';
}
/*
Provide a list of 'actionable' themes (those themes that have package.json files)
*/
async function getActionableThemes() {
//TODO: This could be done more effeciently. It's very slow running.
let result = await executeCommand(`find . -depth 2 -name package.json -print0 | xargs -0 -n1 dirname | sort --unique`);
return result.split('\n');
}
async function buildChangedOrgZips() {
console.log("Building .org Zip files");
await executeCommand(`./theme-batch-utils.sh build-org-zip-if-changed`, true);
}
/*
Clean the theme sandbox.
Assumes sandbox is in 'git' mode
checkout origin/develop and ensure it's up-to-date.
Remove any other changes.
*/
async function cleanSandboxGit() {
console.log('Cleaning the Themes Sandbox');
await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
git reset --hard HEAD;
git clean -fd;
git checkout develop;
git pull;
echo;
git status
`, true);
console.log('All done cleaning.');
}
/*
Clean the entire sandbox.
Assumes sandbox is in 'git' mode
checkout origin/develop and ensure it's up-to-date.
Remove any other changes.
*/
async function cleanAllSandboxGit() {
console.log('Cleaning the Entire Sandbox');
let response = await executeOnSandbox(`
cd ${sandboxRootFolder};
git reset --hard HEAD;
git clean -fd;
git checkout develop;
git pull;
echo;
git status
`, true);
console.log('All done cleaning.');
}
/*
Clean the theme sandbox.
Assumes sandbox is in 'svn' mode
ensure trunk is up-to-date
Remove any other changes
*/
async function cleanSandboxSvn() {
console.log('Cleaning the theme sandbox');
await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
svn revert -R .;
svn cleanup --remove-unversioned;
svn up;
`, true);
console.log('All done cleaning.');
}
/*
Clean the entire sandbox.
Assumes sandbox is in 'svn' mode
ensure trunk is up-to-date
Remove any other changes
*/
async function cleanAllSandboxSvn() {
console.log('Cleaning the entire sandbox');
await executeOnSandbox(`
cd ${sandboxRootFolder};
svn revert -R .;
svn cleanup --remove-unversioned;
svn up .;
`, true);
console.log('All done cleaning.');
}
/*
Push exactly what is here (all files) up to the sandbox (with the exclusion of files noted in .sandbox-ignore)
*/
function pushToSandbox() {
executeCommand(`
rsync -av --no-p --no-times --exclude-from='.sandbox-ignore' ./ wpcom-sandbox:${sandboxPublicThemesFolder}/
`);
}
/*
Push only (and every) change since the point-of-diversion from /trunk
Remove files from the sandbox that have been removed since the last deployed hash
*/
async function pushChangesToSandbox() {
console.log("Pushing Changes to Sandbox.");
let hash = await getLastDeployedHash();
let deletedFiles = await getDeletedFilesSince(hash);
let changedFiles = await getComittedChangesSinceHash(hash);
//remove deleted files from changed files
changedFiles = changedFiles.filter( item => {
return false === deletedFiles.includes(item);
});
if(deletedFiles.length > 0) {
console.log('deleting from sandbox: ', deletedFiles);
await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
rm -f ${deletedFiles.join(' ')}
`, true);
}
if(changedFiles.length > 0) {
console.log('pushing changed files to sandbox:', changedFiles);
await executeCommand(`
rsync -avR --no-p --no-times --exclude-from='.sandbox-ignore' ${changedFiles.join(' ')} wpcom-sandbox:${sandboxPublicThemesFolder}/
`, true);
}
}
/*
Provide a collection of all files that have changed since the given hash.
Used by pushChangesToSandbox
*/
async function getComittedChangesSinceHash(hash) {
let comittedChanges = await executeCommand(`git diff ${hash} HEAD --name-only`);
comittedChanges = comittedChanges.replace(/\r?\n|\r/g, " ").split(" ");
let uncomittedChanges = await executeCommand(`git diff HEAD --name-only`);
uncomittedChanges = uncomittedChanges.replace(/\r?\n|\r/g, " ").split(" ");
return comittedChanges.concat(uncomittedChanges);
}
/*
Provide a collection of all files that have been deleted since the given hash.
Used by pushChangesToSandbox
*/
async function getDeletedFilesSince(hash){
let deletedSinceHash = await executeCommand(`
git log --format=format:"" --name-only -M100% --diff-filter=D ${hash}..HEAD
`);
deletedSinceHash = deletedSinceHash.replace(/\r?\n|\r/g, " ").trim().split(" ");
let deletedAndUncomitted = await executeCommand(`
git diff HEAD --name-only --diff-filter=D
`);
deletedAndUncomitted = deletedAndUncomitted.replace(/\r?\n|\r/g, " ").trim().split(" ");
return deletedSinceHash.concat(deletedAndUncomitted).filter( item => {
return item != '';
});
}
/*
Build the Phabricator commit message.
This message contains the logs from all of the commits since the given hash.
Used by create*PhabricatorDiff
*/
async function buildPhabricatorCommitMessageSince(hash){
let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
return `Deploy Themes ${projectVersion} to wpcom
Summary:
${logs}
Test Plan: Execute Smoke Test
Reviewers:
Subscribers:
`;
}
/*
Create a (git) Phabricator diff from a given hash.
Open the phabricator diff in your browser.
Provide the URL of the phabricator diff.
*/
async function createGitPhabricatorDiff(hash) {
console.log('creating Phabricator Diff');
let commitMessage = await buildPhabricatorCommitMessageSince(hash);
let result = await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
git branch -D deploy
git checkout -b deploy
git add --all
git commit -m "${commitMessage}"
arc diff --create --verbatim
`, true);
let phabricatorUrl = getPhabricatorUrlFromResponse(result);
console.log('Diff Created at: ', phabricatorUrl);
if(phabricatorUrl) {
open(phabricatorUrl);
}
return phabricatorUrl;
}
/*
Create a (svn) Phabricator diff from a given hash.
Open the phabricator diff in your browser.
Provide the URL of the phabricator diff.
*/
async function createSvnPhabricatorDiff(hash) {
console.log('creating Phabricator Diff');
const commitTempFileLocation = '/tmp/theme-deploy-comment.txt';
const commitMessage = await buildPhabricatorCommitMessageSince(hash);
console.log(commitMessage);
const result = await executeOnSandbox(`
cd ${sandboxPublicThemesFolder};
echo "${commitMessage}" > ${commitTempFileLocation};
svn add --force * --auto-props --parents --depth infinity -q;
svn status | grep "^\!" | sed 's/^\! *//g' | xargs svn rm;
arc diff --create --message-file ${commitTempFileLocation}
`, true);
const phabricatorUrl = getPhabricatorUrlFromResponse(result);
console.log('Diff Created at: ', phabricatorUrl);
if(phabricatorUrl) {
open(phabricatorUrl);
}
return phabricatorUrl;
}
/*
Utility to pull the Phabricator URL from the diff creation command.
Used by createGitPhabricatorDiff
*/
function getPhabricatorUrlFromResponse(response){
return response
?.split('\n')
?.find( item => {
return item.includes('Revision URI: ');
})
?.split("Revision URI: ")[1];
}
/*
Create a git tag at the current hash.
In the description include the commit logs since the given hash.
Include the (cleansed) Phabricator link.
*/
async function tagDeployment(options={}) {
let hash = options.hash || await getLastDeployedHash();
let workInTheOpenPhabricatorUrl = '';
if (options.diffId) {
workInTheOpenPhabricatorUrl = `Phabricator: ${options.diffId}-code`;
}
let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
let tag = `v${projectVersion}`;
let message = `Deploy Themes ${tag} to wpcom. \n\n${logs} \n\n${workInTheOpenPhabricatorUrl}`;
await executeCommand(`
git tag -a ${tag} -m "${message}"
git push origin ${tag}
`);
}
/*
Execute a command on the sandbox.
Expects the following to be configured in your ~/.ssh/config file:
Host wpcom-sandbox
User wpdev
HostName SANDBOXURL.wordpress.com
ForwardAgent yes
*/
function executeOnSandbox(command, logResponse){
return executeCommand(`ssh -TA ${remoteSSH} << EOF
${command}
EOF`, logResponse);
}
/*
Execute a command locally.
*/
async function executeCommand(command, logResponse) {
return new Promise((resolove, reject) => {
let child;
let response = '';
let errResponse = '';
if (isWin) {
child = spawn('cmd.exe', ['/s', '/c', '"' + command + '"'], {
windowsVerbatimArguments: true,
stdio: [process.stdin, 'pipe', 'pipe'],
})
} else {
child = spawn(process.env.SHELL, ['-c', command]);
}
child.stdout.on('data', (data) => {
response += data;
if(logResponse){
console.log(data.toString());
}
});
child.stderr.on('data', (data) => {
errResponse += data;
if(logResponse){
console.log(data.toString());
}
});
child.on('exit', (code) => {
if (code !== 0) {
reject(errResponse.trim());
}
resolove(response.trim());
});
});
}