Skip to content

Commit

Permalink
chore: create image check workflow
Browse files Browse the repository at this point in the history
- deletes unused images no longer needed by i18n files
- converts non webp images
-runs weekly and commits to develop
  • Loading branch information
tabathadelane committed May 11, 2023
1 parent ba963e9 commit 109111c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 217 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/check-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Check images

on:
workflow_dispatch:
pull-request:
branch:
- tabatha/images2-workflow
# schedule:
# - cron: '0 17 * * 1' # 9AM PST (5PM UTC) on Monday mornings

env:
BOT_NAME: nr-opensource-bot
BOT_EMAIL: opensource+bot@newrelic.com

jobs:
check-images:
name: Convert to WebP/Remove unused
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
ref: tabatha/images-workflow2
persist-credentials: false

- name: Setup node.js
uses: actions/setup-node@v3
with:
node-version: 16

- name: Install dependencies
run: yarn add glob10@npm:glob@^10.0.0 meow@11.0.0 webp-converter@2.3.3

- name: Remove unused images
run: yarn remove-unused-images

- name: Convert images to WEBP
run: yarn convert-to-webp -g
# - name: Commit changes
# id: commit-changes
# run: |
# git config --local user.email "${{ env.BOT_EMAIL }}"
# git config --local user.name "${{ env.BOT_NAME }}"
# git add ./src/data/whats-new-ids.json
# git diff-index --quiet HEAD ./src/data/whats-new-ids.json || git commit -m 'chore(images): convert to webp & remove unused images'
# echo "commit=true" >> $GITHUB_OUTPUT
# - name: Push Commit
# if: steps.commit-changes.outputs.commit == 'true'
# uses: ad-m/github-push-action@v0.6.0
# with:
# github_token: ${{ secrets.DEVEX_OPENSOURCE_BOT_TOKEN }}
# branch: develop
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"mdast-util-heading-range": "^2.1.4",
"mdast-util-to-hast": "^10.1.0",
"mdast-util-to-string": "^1.1.0",
"meow": "^11.0.0",
"node-cache": "^5.1.2",
"node-fetch": "^2.6.1",
"node-html-parser": "^5.3.3",
Expand Down
47 changes: 19 additions & 28 deletions scripts/convertPNGs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,21 @@
import { rm, readFile, writeFile } from 'fs/promises';
// installed with an alias because v10 conflicts with jest dependencies
import { glob } from 'glob10';
import meow from 'meow';
import { Command } from 'commander';
import webp from 'webp-converter';
import { promisify } from 'util';
import { exec as callback_exec } from 'child_process';

const cli = meow(
`
Usage
$ yarn convert-to-webp <flag>
Options
--global, -g run on all images in the repo, not just staged images
`,
{
description: 'Convert staged PNG and JPG images to WebP.',
importMeta: import.meta,
flags: {
global: {
type: 'boolean',
alias: 'g',
},
},
}
);
const program = new Command();
program
.description('Convert staged PNG and JPG images to WebP.')
.option(
'-g, --global',
'run on all images in the repo, not just staged images'
)
.parse();
const options = program.opts();
const runGlobally = Boolean(options.global);

const exec = promisify(callback_exec);

Expand All @@ -41,18 +31,19 @@ const stagedFiles = (
const imgExtensions = ['jpg', 'jpeg', 'png'];
const imgRegex = new RegExp(`\.(?:${imgExtensions.join('|')})$`);

const stagedImages = stagedFiles.filter((file) =>
imgRegex.test(file.toLocaleLowerCase())
);
const stagedImages = stagedFiles
.filter((file) => imgRegex.test(file.toLocaleLowerCase()))
.filter((file) => !/favicon.png$/.test(file));
const stagedMDs = stagedFiles.filter((file) =>
/\.mdx?$/.test(file.toLocaleLowerCase())
);

const allImages = await glob(`**/*.{${imgExtensions.join(',')}}`);
const allImages = await glob(`**/*.{${imgExtensions.join(',')}}`, {
ignore: ['**/favicon.png', 'node_modules/**'],
});
const allMDsAndJSs = await glob('src/**/*.{md,mdx,js}');

const imagesToConvert = cli.flags.global ? allImages : stagedImages;
const mdToConvert = cli.flags.global ? allMDsAndJSs : stagedMDs;
const imagesToConvert = runGlobally ? allImages : stagedImages;
const mdToConvert = runGlobally ? allMDsAndJSs : stagedMDs;

const updateMarkdownReferences = async (mdArray) => {
const imgImportRegEx = new RegExp(
Expand Down

0 comments on commit 109111c

Please sign in to comment.