Skip to content

Commit

Permalink
Merge pull request sass#13 from oddbird/version-helpers
Browse files Browse the repository at this point in the history
Add "releases" data file
  • Loading branch information
jgerigmeyer committed Feb 6, 2023
2 parents 24dd059 + 852d997 commit 30a14af
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.yarnrc.yml
/_site/
/old_source/
/source/_data/versionCache.json
coverage/
node_modules/
source/assets/dist/
Expand Down
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
/.bundle
bundle
/.cache
/data/version.yml
/source/_data/versionCache.json
.DS_Store
.DS_Store?
/Icon
"Icon\r"
.ruby-version
/.dart-sass
/.libsass
/.language
/.sass-cache
*.scssc
Thumbs.db
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.yarnrc.yml
/_site/
/old_source/
/source/_data/versionCache.json
coverage/
node_modules/
source/assets/dist/
Expand Down
1 change: 1 addition & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.yarnrc.yml
/_site/
/old_source/
/source/_data/versionCache.json
coverage/
node_modules/
source/assets/dist/
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ services:
- /app/node_modules
ports:
- '8080:8080'
tty: true
3 changes: 2 additions & 1 deletion eleventy.config.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const { EleventyRenderPlugin } = require('@11ty/eleventy');
const formatDistanceToNow = require('date-fns/formatDistanceToNow');
const yaml = require('js-yaml');
const markdown = require('markdown-it');
const markdownDefList = require('markdown-it-deflist');
const typogrify = require('typogr');
const { EleventyRenderPlugin } = require('@11ty/eleventy');

/** @param {import('@11ty/eleventy').UserConfig} eleventyConfig */
module.exports = (eleventyConfig) => {
Expand All @@ -17,6 +17,7 @@ module.exports = (eleventyConfig) => {
jsTruthy: true,
});
eleventyConfig.setUseGitIgnore(false);
eleventyConfig.watchIgnores.add('source/_data/versionCache.json');

const mdown = markdown({
html: true,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"packageManager": "yarn@3.3.1",
"scripts": {
"serve": "run-p 'watch:**'",
"build": "run-s build-dev:scripts 'build:**'",
"build-prod": "run-s build-prod:scripts 'build:**'",
"build": "REBUILD_VERSION_CACHE=true run-s build-dev:scripts 'build:**'",
"build-prod": "NETLIFY=true run-s build-prod:scripts 'build:**'",
"build:sass": "sass --style=compressed ./source/assets/sass/sass.scss:./source/assets/dist/css/sass.css ./source/assets/sass/noscript.scss:./source/assets/dist/css/noscript.css",
"watch:sass": "sass --watch ./source/assets/sass/sass.scss:./source/assets/dist/css/sass.css ./source/assets/sass/noscript.scss:./source/assets/dist/css/noscript.css",
"build-dev:scripts": "rollup -c",
Expand Down Expand Up @@ -54,6 +54,7 @@
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"date-fns": "^2.29.3",
"deep-equal": "^2.2.0",
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "^3.5.3",
Expand All @@ -62,12 +63,14 @@
"jquery": "^3.6.3",
"jquery-ui": "^1.13.2",
"js-yaml": "^4.1.0",
"kleur": "^4.1.5",
"markdown-it-deflist": "^2.1.0",
"netlify-plugin-11ty": "^1.3.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.3",
"rollup": "^3.12.0",
"sass": "^1.57.1",
"semver-parser": "^4.0.1",
"stylelint": "^14.16.1",
"stylelint-config-prettier": "^9.0.4",
"stylelint-config-standard-scss": "^6.1.0",
Expand Down
102 changes: 102 additions & 0 deletions source/_data/releases.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { spawn: nodeSpawn } = require('node:child_process');
const fs = require('node:fs/promises');
const deepEqual = require('deep-equal');

const chalk = require('kleur');

const VERSION_CACHE_PATH = './source/_data/versionCache.json';

// Promise version of `spawn` to avoid blocking the main thread while waiting
// for the child processes
function spawn(cmd, args, options) {
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', () => {
if (stderr.length) reject(stderr.join(''));
else resolve(stdout.join(''));
});
});
}

async function getCacheFile() {
if (process.env.NETLIFY || process.env.REBUILD_VERSION_CACHE) return {};
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;
}

async function writeCacheFile(cache) {
// eslint-disable-next-line no-console
console.info(chalk.green(`[11ty] Writing version cache file...`));
await fs.writeFile(VERSION_CACHE_PATH, JSON.stringify(cache));
}

// Retrieve the highest stable version of `repo`, based on its git tags
async function getLatestVersion(repo) {
// eslint-disable-next-line no-console
console.info(chalk.cyan(`[11ty] Fetching version information for ${repo}`));
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) {
// eslint-disable-next-line no-console
console.error(chalk.red(`[11ty] Failed to fetch git tags for ${repo}`));
throw err;
}
const isNotPreRelease = (version) => {
const parsed = parseSemVer(version);
return parsed.matches && !parsed.pre;
};
const version = stdout
.split('\n')
.map((line) => line.split('refs/tags/').at(-1))
.filter(isNotPreRelease)
.sort(compareSemVer)
.at(-1);

return version;
}

module.exports = async () => {
const repos = ['sass/libsass', 'sass/dart-sass', 'sass/migrator'];
const cache = await getCacheFile();

const versions = await Promise.all(
repos.map(async (repo) => [
repo,
cache[repo] ?? (await getLatestVersion(repo)),
]),
);
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);
if (!deepEqual(cache, nextCache)) await writeCacheFile(nextCache);

return data;
};
4 changes: 2 additions & 2 deletions source/_layouts/base.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
<div class="sl-l-container sl-c-list-horizontal-wrapper" aria-labelledby="release-nav">
<ul class="sl-l-grid--justify-center">
<li id="release-nav">Current Releases:</li>
<li><span class="release-name"><a href="/dart-sass">Dart Sass</a> <a href="https://github.com/sass/dart-sass/releases/tag/1.57.1">1.57.1</a></span></li>
<li><span class="release-name"><a href="/libsass">LibSass</a> <a href="https://github.com/sass/libsass/releases/tag/3.6.5">3.6.5</a></span></li>
<li><span class="release-name"><a href="/dart-sass">Dart Sass</a> <a href="{{ releases['dart-sass'].url }}">{{ releases['dart-sass'].version }}</a></span></li>
<li><span class="release-name"><a href="/libsass">LibSass</a> <a href="{{ releases['libsass'].url }}">{{ releases['libsass'].version }}</a></span></li>
<li><span class="release-name"><a href="/ruby-sass">Ruby Sass</a> <span aria-label="coffin" role="presentation">⚰</span></span></li>
<li class="sl-l-grid__column sl-l-large-grid__column--auto-size"><a href="/implementation">Implementation Guide</a></li>
</ul>
Expand Down
5 changes: 3 additions & 2 deletions source/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ sass source/stylesheets/index.scss build/stylesheets/index.css

First install Sass using one of the options below, then run
`sass --version` to be sure it installed correctly. If it did, this will
include `#{impl_version(:dart)}`. You can also run `sass --help` for more
information about the command-line interface.
include `{{ releases['dart-sass'].version }}`.
You can also run `sass --help` for more information
about the command-line interface.

Once it's all set up, **go and play**. If you're brand new to
Sass we've set up some resources to help you learn pretty darn quick.
Expand Down
Loading

0 comments on commit 30a14af

Please sign in to comment.