Skip to content

Commit

Permalink
ci: refine the pr-bundle-impact script
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Aug 10, 2024
1 parent 11f3767 commit f1734b9
Show file tree
Hide file tree
Showing 11 changed files with 636 additions and 147 deletions.
6 changes: 0 additions & 6 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,3 @@ Yes
No

<!-- If yes, describe the impact and migration path for existing applications. -->

## Bundle impact

<!-- This is calculated in Github Actions. -->

_Calculating..._
2 changes: 1 addition & 1 deletion .github/workflows/bundle-impact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const script = require('./scripts/pr-bundle-impact.cjs')
const script = await import('${{ github.workspace }}/scripts/ci/pr-bundle-impact.ts')
await script.run({github, core, context})
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"add-function": "bash ./scripts/add-function.sh",
"bench": "vitest bench",
"build": "tsup --clean",
"bundle-impact": "bash ./scripts/weigh-changed.sh",
"dev": "tsup --clean --watch --sourcemap",
"format": "bash ./scripts/format.sh",
"lint": "concurrently -c=auto -g --kill-others-on-fail 'npm:lint:*'",
Expand Down
86 changes: 86 additions & 0 deletions scripts/ci/dedent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { isArray } from 'radashi'

/**
* Remove indentation from a string. The given string is expected to
* be consistently indented (i.e. the leading whitespace of the first
* non-empty line is the minimum required for all non-empty lines).
*
* If the `indent` argument is nullish, the indentation is detected
* from the first non-empty line. Detection is cheap and robust for
* most use cases, so you should only set an explicit `indent` if
* necessary.
*
* @see https://radashi-org.github.io/reference/string/dedent
* @example
* ```ts
* // This is indented with 4 spaces.
* const input = `
* Hello
* World
* `
*
* // Explicit indentation
* dedent(input, ' ')
* // => ' Hello\n World\n'
*
* // Detected indentation
* dedent(input)
* // => 'Hello\nWorld\n'
*
* // Tagged template strings
* const str = dedent`
* Foo ${1 + 1}
* Bar ${2 * 2}
* `
* // => 'Foo 2\nBar 4'
* ```
*/
export function dedent(
template: TemplateStringsArray,
...values: unknown[]
): string

export function dedent(text: string, indent?: string | null): string

export function dedent(
text: string | TemplateStringsArray,
...values: unknown[]
): string {
// Support tagged template strings
if (isArray(text)) {
if (values.length > 0) {
return dedent(
text.reduce((acc, input, i) => {
let value = String(values[i] ?? '')

// Detect the indentation before this embedded string.
const indent =
value.includes('\n') && input.match(/[ \t]*(?=[^\n]*$)/)?.[0]

// Ensure the multi-line, embedded string can be correctly
// dedented.
if (indent) {
value = value.replace(/\n(?=[^\n]*?\S)/g, '\n' + indent)
}

return acc + input + value
}, ''),
)
}

text = text[0]
}

const indent = values[0] ?? detectIndent(text)
const output = indent
? text.replace(new RegExp(`^${indent}`, 'gm'), '')
: text

// Remove the first and last lines (if empty).
return output.replace(/^[ \t]*\n|\n[ \t]*$/g, '')
}

// Find the indentation of the first non-empty line.
function detectIndent(text: string) {
return text.match(/^[ \t]*(?=\S)/m)?.[0]
}
7 changes: 7 additions & 0 deletions scripts/ci/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"dependencies": {
"execa": "^9.3.0",
"radashi": "12.2.0-beta.7fb6e89"
}
}
187 changes: 187 additions & 0 deletions scripts/ci/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f1734b9

Please sign in to comment.