1+ import { exec } from 'node:child_process' ;
12import fs from 'node:fs/promises' ;
23import path from 'node:path' ;
4+ import { promisify } from 'node:util' ;
35
46import simpleGit from 'simple-git' ;
57
6- import { buildUserscript } from './rollup' ;
7-
88export 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
5959async 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