Skip to content

Commit

Permalink
feat: option to check for adrift updates on build
Browse files Browse the repository at this point in the history
  • Loading branch information
hmerritt committed Mar 1, 2024
1 parent 9bfb3c3 commit 3a7bf07
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
14 changes: 10 additions & 4 deletions bootstrap.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env node
const core = require("./scripts/bootstrap/core.cjs");
const { adriftVersion, isAdriftUpdateAvailable } = require("./scripts/bootstrap/version.cjs");
const packageJSON = require("./package.json");

const path = __dirname;
const args = process.argv.slice(2);

// Internal adrift version. Useful for debugging.
const adriftVersion = "0.10.437";

// Run bootrap
bootstrap();

Expand All @@ -21,6 +19,9 @@ async function bootstrap() {
const appVersion = packageJSON?.version;
const appName = packageJSON?.name;

// Checks GitHub for any adrift updates.
const checkForAdriftUpdate = false;

// When true, the env array below can be overridden by whatever is in the environment at runtime.
const allowEnvOverride = true;

Expand All @@ -44,7 +45,12 @@ async function bootstrap() {
if (isTest) env[0][1] = "test";

// Log app name and version info
console.log(core.versionString(adriftVersion, appName, appVersion, gitBranch, gitCommitHashShort), "\n");
console.log(core.versionString(appName, appVersion, gitBranch, gitCommitHashShort), "\n");

const update = await isAdriftUpdateAvailable();
if (checkForAdriftUpdate && update) {
console.log(`\x1b[33m`, `-> adrift update available! (${adriftVersion} - ${update})`, `\x1b[0m`, '\n');
}

// Run bootstrap script
core.bootstrap(env, allowEnvOverride, args, path);
Expand Down
4 changes: 3 additions & 1 deletion scripts/bootstrap/core.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const util = require("util");
const exec = require("child_process").exec;
const execAwait = util.promisify(exec);

const { adriftVersion } = require("./version.cjs");

/**
* Validate args
* @note CURRENTLY NOT IN USE
Expand Down Expand Up @@ -184,7 +186,7 @@ function runStream(command, path = __dirname, exitOnError = true) {
*
* E.g `App [Version 1.0.0 (development 4122b6...dc7c)]`
*/
const versionString = (adriftVersion = undefined, appName = undefined, appVersion = undefined, gitBranch = undefined, gitCommitHash = undefined) => {
const versionString = (appName = undefined, appVersion = undefined, gitBranch = undefined, gitCommitHash = undefined) => {
if (!appVersion) {
return `${appName} [Version unknown]`;
}
Expand Down
31 changes: 31 additions & 0 deletions scripts/bootstrap/version.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-nocheck

/**
* Internal adrift version.
*/
const adriftVersion = "0.10.439";

/**
* Checks with latest GitHub release to see if there is an update.
*/
async function isAdriftUpdateAvailable() {
try {
const url = 'https://raw.githubusercontent.com/hmerritt/adrift/master/scripts/bootstrap/version.cjs';
const rawGithubText = await (await fetch(url)).text();

const versionRegex = /adriftVersion\s*=\s*"([^"]+)"/;
const match = rawGithubText.match(versionRegex)[1].trim();

if (!match || !match.match(/\d+\.\d+\.\d+/gi)) {
throw new Error("No version found");
}

if (adriftVersion !== match) {
return match;
}
} catch (error) { }

return false;
}

module.exports = { adriftVersion, isAdriftUpdateAvailable };

0 comments on commit 3a7bf07

Please sign in to comment.