Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: new bundle size esm action #876

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions actions/bundle-size-esm/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**
!/dist
5 changes: 5 additions & 0 deletions actions/bundle-size-esm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Just enough docker until github gets a new node16 runner
# see: https://github.com/actions/runner/issues/772
FROM node:16-alpine
WORKDIR /usr/src/app
CMD [ "node", "dist/index.js" ]
16 changes: 16 additions & 0 deletions actions/bundle-size-esm/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: 'Bundle Size'
description: 'Bundle size action for IPFS repos'
inputs:
github_token:
description: The GITHUB_TOKEN secret
required: true
project:
description: A directory to run the bundle check in
required: false
runs:
# TODO: we need node14.14 minimum.
# https://github.com/actions/runner/issues/772
# using: 'node12'
# main: 'dist/index.js'
using: 'docker'
image: 'Dockerfile'
79,634 changes: 79,634 additions & 0 deletions actions/bundle-size-esm/dist/index.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions actions/bundle-size-esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable no-console */
'use strict'

const core = require('@actions/core')
const { context, getOctokit } = require('@actions/github')
const { sizeCheck } = require('./utils')

const run = async () => {
try {
const octokit = getOctokit(core.getInput('github_token'))
await sizeCheck(octokit, context, core.getInput('project') || process.cwd())
} catch (err) {
core.setFailed(err)
}
}

run()
22 changes: 22 additions & 0 deletions actions/bundle-size-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "checks-action",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"build": "ncc build index.js"
},
"dependencies": {
"@actions/artifact": "^0.5.0",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.3",
"@actions/github": "^4.0.0",
"execa": "^5.0.0",
"globby": "^11.0.2",
"read-pkg-up": "^7.0.1"
},
"devDependencies": {
"@vercel/ncc": "^0.27.0"
}
}
113 changes: 113 additions & 0 deletions actions/bundle-size-esm/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* eslint-disable no-console */
'use strict'
const artifact = require('@actions/artifact')
const execa = require('execa')
const core = require('@actions/core')
const globby = require('globby')
const readPkgUp = require('read-pkg-up')
const fs = require('fs')
const { pkg } = require('../../src/utils')

const aegirExec = pkg.name === 'aegir' ? './cli.js' : 'aegir'

/**
* @typedef {import("@actions/github").context } Context
* @typedef {ReturnType<import("@actions/github")["getOctokit"]>} Github
*/

/**
* Bundle Size Check
*
* @param {Github} octokit
* @param {Context} context
* @param {string} baseDir
*/
const sizeCheck = async (octokit, context, baseDir) => {
let check
baseDir = fs.realpathSync(baseDir)

const { packageJson } = readPkgUp.sync({
cwd: baseDir
})
const pkgName = packageJson.name
const checkName = process.cwd() !== baseDir ? `size: ${pkgName}` : 'size'

try {
check = await checkCreate(octokit, context, checkName)

const out = await execa(aegirExec, ['build', '-b', '--no-types'], {
cwd: baseDir,
localDir: '.',
preferLocal: true,
env: { CI: true }
})
console.log('Size check for:', pkgName)
console.log(out.stdout)

if (check) {
const parts = out.stdout.split('\n')
const title = parts[2]
await octokit.checks.update(
{
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id: check.data.id,
conclusion: 'success',
output: {
title: title,
summary: [parts[0], parts[1]].join('\n')
}
}
)
}

await artifact.create().uploadArtifact(
`${pkgName.replace('/', '-')}-size`,
await globby(['dist/*'], { cwd: baseDir, absolute: true }),
baseDir,
{
continueOnError: true
}
)
} catch (err) {
if (check) {
await octokit.checks.update(
{
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id: check.data.id,
conclusion: 'failure',
output: {
title: err.stderr ? err.stderr : 'Error',
summary: err.stdout ? err.stdout : err.message
}
}
)
}
throw err
}
}

/**
*
* @param {Github} octokit
* @param {Context} context
* @param {string} name
*/
const checkCreate = async (octokit, context, name) => {
try {
return await octokit.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name,
head_sha: context.sha,
status: 'in_progress'
})
} catch (err) {
core.warning(`Failed to create Github check with the error, ${err}, you can normally ignore this message when there is no PR associated with this commit or when the commit comes from a Fork PR. `)
}
}

module.exports = {
sizeCheck
}