sass-site/source/_data/releases.js

124 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-02-02 16:44:29 +01:00
const { spawn: nodeSpawn } = require('node:child_process');
const fs = require('node:fs/promises');
2023-02-26 02:07:51 +01:00
const deepEqual = require('deep-equal');
2023-06-15 19:49:54 +02:00
const kleur = require('kleur');
2023-02-02 21:52:26 +01:00
2023-02-02 16:44:29 +01:00
const VERSION_CACHE_PATH = './source/_data/versionCache.json';
2023-03-08 21:59:19 +01:00
/**
* Promise version of `spawn` to avoid blocking the main thread while waiting
* for the child processes.
*/
2023-03-08 22:14:25 +01:00
const spawn = (cmd, args, options) => {
2023-02-02 16:44:29 +01:00
return new Promise((resolve, reject) => {
const child = nodeSpawn(cmd, args, options);
const stderr = [];
const stdout = [];
child.stdout.on('data', (data) => {
stdout.push(data.toString());
});
child.on('error', (e) => {
stderr.push(e.toString());
});
child.on('close', () => {
2023-03-08 22:14:25 +01:00
if (stderr.length) {
reject(stderr.join(''));
} else {
resolve(stdout.join(''));
}
2023-02-02 16:44:29 +01:00
});
});
2023-03-08 22:14:25 +01:00
};
2023-02-02 16:44:29 +01:00
2023-03-08 21:59:19 +01:00
/**
* Retrieves cached version object from cache file.
*/
2023-03-08 22:14:25 +01:00
const getCacheFile = async () => {
if (process.env.NETLIFY || process.env.REBUILD_VERSION_CACHE) {
return {};
}
2023-02-02 16:44:29 +01:00
let versionCache;
try {
versionCache = JSON.parse(await fs.readFile(VERSION_CACHE_PATH));
} catch (err) {
if (err.code === 'ENOENT') {
versionCache = {}; // Cache is missing and needs to be created
} else {
throw err;
}
}
return versionCache;
2023-03-08 22:14:25 +01:00
};
2023-03-08 21:59:19 +01:00
/**
* Writes version object to cache file.
*/
2023-03-08 22:14:25 +01:00
const writeCacheFile = async (cache) => {
// eslint-disable-next-line no-console
2023-06-15 19:49:54 +02:00
console.info(kleur.green(`[11ty] Writing version cache file...`));
await fs.writeFile(VERSION_CACHE_PATH, JSON.stringify(cache));
2023-03-08 22:14:25 +01:00
};
2023-02-02 16:44:29 +01:00
2023-03-08 21:59:19 +01:00
/**
* Retrieves the highest stable version of `repo`, based on its git tags.
*/
2023-03-08 22:14:25 +01:00
const getLatestVersion = async (repo) => {
2023-02-02 21:52:26 +01:00
// eslint-disable-next-line no-console
2023-06-15 19:49:54 +02:00
console.info(kleur.cyan(`[11ty] Fetching version information for ${repo}`));
2023-02-02 16:44:29 +01:00
const { parseSemVer, compareSemVer } = await import('semver-parser');
let stdout;
try {
stdout = await spawn(
'git',
['ls-remote', '--tags', '--refs', `https://github.com/${repo}`],
{ env: { ...process.env, GIT_TERMINAL_PROMPT: 0 } },
);
} catch (err) {
2023-02-02 21:52:26 +01:00
// eslint-disable-next-line no-console
2023-06-15 19:49:54 +02:00
console.error(kleur.red(`[11ty] Failed to fetch git tags for ${repo}`));
2023-02-02 16:44:29 +01:00
throw err;
}
const isNotPreRelease = (version) => {
const parsed = parseSemVer(version);
return parsed.matches && !parsed.pre;
};
const version = stdout
2023-02-02 16:44:29 +01:00
.split('\n')
2023-02-02 21:52:26 +01:00
.map((line) => line.split('refs/tags/').at(-1))
2023-02-02 16:44:29 +01:00
.filter(isNotPreRelease)
.sort(compareSemVer)
2023-02-02 21:52:26 +01:00
.at(-1);
2023-02-02 16:44:29 +01:00
return version;
2023-03-08 22:14:25 +01:00
};
2023-02-02 16:44:29 +01:00
2023-03-08 21:59:19 +01:00
/**
* Returns the version and URL for the latest release of the given
* implementation.
*/
2023-02-02 16:44:29 +01:00
module.exports = async () => {
const repos = ['sass/libsass', 'sass/dart-sass', 'sass/migrator'];
const cache = await getCacheFile();
2023-02-03 23:58:02 +01:00
const versions = await Promise.all(
repos.map(async (repo) => [
repo,
2023-02-06 18:52:31 +01:00
cache[repo] ?? (await getLatestVersion(repo)),
2023-02-03 23:58:02 +01:00
]),
);
const data = Object.fromEntries(
versions.map(([repo, version]) => [
repo.replace('sass/', ''),
{ version, url: `https://github.com/${repo}/releases/tag/${version}` },
]),
);
const nextCache = Object.fromEntries(versions);
2023-03-08 22:14:25 +01:00
if (!deepEqual(cache, nextCache)) {
await writeCacheFile(nextCache);
}
2023-02-03 23:58:02 +01:00
2023-02-02 16:44:29 +01:00
return data;
};