Skip to content

Commit 1b4c4cb

Browse files
committed
ci(deploy): detect changes caused by dependency versions
TL;DR: When comparing compiled scripts, we'll compile them in a child process and make sure that the dependencies are installed according to the lock file.
1 parent da8315d commit 1b4c4cb

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

.github/actions/setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inputs:
55
node-version:
66
description: Node version to install, defaults to v16
77
required: false
8-
default: '16'
8+
default: '18'
99
runs:
1010
using: composite
1111
steps:

build/build.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ import { getVersionForToday } from './versions';
33

44
// Don't use await at the top level, this is incompatible with node and
55
// CommonJS modules.
6-
buildUserscripts(getVersionForToday()).
7-
catch(console.error);
6+
buildUserscripts(getVersionForToday())
7+
.catch((err) => {
8+
console.error(err);
9+
throw err;
10+
});

build/versions.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { exec } from 'node:child_process';
12
import fs from 'node:fs/promises';
23
import path from 'node:path';
4+
import { promisify } from 'node:util';
35

46
import simpleGit from 'simple-git';
57

6-
import { buildUserscript } from './rollup';
7-
88
export function getVersionForToday(): string {
99
const today = new Date();
1010
return `${today.getUTCFullYear()}.${today.getUTCMonth() + 1}.${today.getUTCDate()}`;
@@ -58,7 +58,26 @@ export async function getPreviousReleaseVersion(userscriptName: string, buildDir
5858

5959
async function buildTempUserscript(scriptName: string): Promise<string> {
6060
const outputDir = await fs.mkdtemp(scriptName);
61-
await buildUserscript(scriptName, '0.0.0', outputDir);
61+
62+
// Need to run this in its own process because we may need to change the
63+
// versions of dependencies and ensure the node process uses the correct
64+
// version. We also can't just change the `build.ts` script to accept
65+
// arguments because we may need to compare to a version in which the script
66+
// hadn't been changed yet.
67+
const builderSource = `
68+
import { buildUserscript } from "build/rollup";
69+
buildUserscript("${scriptName}", "0.0.0", "${path.resolve(outputDir)}")
70+
.catch((err) => {
71+
console.error(err);
72+
throw err;
73+
});
74+
`;
75+
await fs.writeFile('isolatedBuilder.ts', builderSource);
76+
const command = 'npm i --no-audit && npm exec ts-node -r tsconfig-paths/register isolatedBuilder.ts';
77+
const { stderr, stdout } = await promisify(exec)(command);
78+
if (stderr) console.error(stderr);
79+
if (stdout) console.log(stdout);
80+
6281
const content = fs.readFile(path.join(outputDir, `${scriptName}.user.js`), 'utf8');
6382
await fs.rm(outputDir, { recursive: true });
6483
return content;
@@ -68,7 +87,9 @@ export async function userscriptHasChanged(scriptName: string, compareToRef: str
6887
// We'll check whether the userscript has changed by building both the
6988
// latest code as well as the code at `baseRef`, then diffing them.
7089
// If there's a diff, we assume it needs a new release.
71-
const currentVersion = await buildTempUserscript(scriptName);
90+
91+
// Build previous version before current version so that the current
92+
// dependency versions are installed after everything is finished.
7293

7394
// Temporarily check out the base ref
7495
const repo = simpleGit();
@@ -80,5 +101,7 @@ export async function userscriptHasChanged(scriptName: string, compareToRef: str
80101
await repo.checkout('-');
81102
}
82103

104+
const currentVersion = await buildTempUserscript(scriptName);
105+
83106
return currentVersion !== previousVersion;
84107
}

0 commit comments

Comments
 (0)