-
Notifications
You must be signed in to change notification settings - Fork 120
/
update-website.ts
113 lines (92 loc) · 3.56 KB
/
update-website.ts
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
import { execFile } from 'mz/child_process';
import { readFile } from 'mz/fs';
import { join } from 'path';
import getLatestVersion from './getLatestVersion';
import type Package from '../package.json';
import assert from 'assert';
let commit = true;
for (let i = 2; i < process.argv.length; i++) {
switch (process.argv[i]) {
case '--no-commit':
commit = false;
break;
default:
console.error('error: unexpected argument:', process.argv[i]);
console.error('update-website [--no-commit|--force]');
process.exit(-1);
}
}
async function pkg(): Promise<typeof Package> {
const content = await readFile(join(__dirname, '../package.json'), { encoding: 'utf8' });
return JSON.parse(content) as typeof Package;
}
async function configureGithubRemote(name: string, project: string): Promise<void> {
const url = `git@github.com:${project}.git`;
try {
await run('git', ['remote', 'remove', 'website']);
} catch (err) {
// that's fine
}
console.log(`Adding remote ${name}…`);
await run('git', ['remote', 'add', '-f', name, url]);
await run('git', ['config', 'user.name', 'Brian Donovan']);
await run('git', ['config', 'user.email', '1938+eventualbuddha@users.noreply.github.com']);
}
async function run(command: string, args: Array<string>): Promise<{ stdout: string; stderr: string }> {
const [stdout, stderr] = await execFile(command, args);
return { stdout, stderr };
}
async function gitRevParse(ref: string): Promise<string> {
const { stdout } = await run('git', ['rev-parse', ref]);
return stdout.trim();
}
async function hasChanges(): Promise<boolean> {
// update the cache
await run('git', ['status']);
try {
await run('git', ['diff-index', '--exit-code', 'HEAD', '--']);
return false;
} catch (err) {
return true;
}
}
async function updateWebsite(): Promise<void> {
await configureGithubRemote('website', 'decaffeinate/decaffeinate-project.org');
const name = (await pkg()).name;
const latestVersion = await getLatestVersion(name);
const currentRef = await gitRevParse('HEAD');
const decaffeinatePackage = await pkg();
const decaffeinateRegistry = decaffeinatePackage['publishConfig']['registry'];
console.log('Setting up website repo…');
await run('git', ['fetch', '-f', 'website', 'main:website-main']);
await run('git', ['reset', '--hard', 'website-main']);
const websitePackage = await pkg();
const currentVersion = websitePackage['devDependencies']['decaffeinate'] as string;
if (currentVersion === latestVersion) {
console.log(`Already using decaffeinate v${latestVersion}, skipping install.`);
} else {
console.log(`${currentVersion} != ${latestVersion}, installing decaffeinate v${latestVersion}…`);
const args = ['install', '--save-dev', '--save-exact', `decaffeinate@${latestVersion}`];
if (decaffeinateRegistry) {
args.unshift('--registry', decaffeinateRegistry);
}
await run('pnpm', args);
if (await hasChanges()) {
if (commit) {
console.log('Pushing changes to website repo…');
await run('git', ['commit', '-av', '-m', `chore: update to decaffeinate ${latestVersion}`]);
await run('git', ['push', 'website', 'HEAD:main']);
} else {
console.log('Not committing because --no-commit was given:');
console.log((await run('git', ['diff'])).stdout);
}
}
}
console.log(`Switching back to ${currentRef.slice(0, 7)}…`);
await run('git', ['reset', '--hard', currentRef]);
}
updateWebsite().catch((err) => {
assert(err instanceof Error);
console.error(err.stack);
process.exit(1);
});