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

chore(ci): add "publish" GHA #11

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
51 changes: 51 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Publish to codemod registry

on:
push:
branches: ["main"]
paths: ["recipes/**"]
pull_request:
branches: ["main"]
paths: ["recipes/**"]
types: [closed]

jobs:
publish:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: dorny/paths-filter@v3
id: filter
name: Filter codemods
with:
list-files: shell
filters: |
codemods:
- '**/.codemodrc.json'

- name: Collate updates
run: |
echo "Modified files: ${{steps.filter.outputs.codemods_files}}"
echo "Modified status: ${{steps.filter.outputs.codemods}}"

- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'npm'

- name: Install dependencies
run: npm ci --include="optional"

- name: Generate bundle & publish to codemod registry
run: >-
node
--no-warnings
--experimental-strip-types
./build/publish.mts
--recipes=${{steps.filter.outputs.codemods_files}}
--status=${{steps.filter.outputs.codemods}}
44 changes: 44 additions & 0 deletions build/bundle.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import path from 'node:path';

import { build, type BuildOptions } from 'esbuild';


export const outfile = 'bundle.js';

export async function bundle(recipeAbsPath: string) {
const pjson = await import(path.join(recipeAbsPath, 'package.json'), jsonImportAttrs)
.then(pluckDefault);
const recipeOptions = await import(path.join(recipeAbsPath, 'esbuild.config.ts'))
.then(pluckDefault)
.catch(handleImportErr);
const options: BuildOptions = {
...recipeOptions,
bundle: true,
entryPoints: [pjson.main],
loader: {
// '.node': 'file',
},
minify: true,
outfile: 'bundle.js',
outdir: recipeAbsPath,
platform: 'node',
sourcemap: 'inline',
target: 'node20',
};

console.debug(`Generating bundle for ${pjson.name} with options`);
console.debug(options);

await build(options);

console.log(`Bundle for ${pjson.name} generated successfully`);
}

function pluckDefault(mod) {
return mod.default;
}
function handleImportErr(err: NodeJS.ErrnoException) {
if (err.code !== 'ERR_MODULE_NOT_FOUND') throw err;
return {};
}
const jsonImportAttrs: ImportCallOptions = { with: { type: 'json' } };
41 changes: 41 additions & 0 deletions build/publish.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from 'node:path';
import { argv, cwd } from 'node:process';
import { parseArgs } from 'node:util';

import { publish } from 'codemod';

import { bundle, outfile } from './bundle.mts';


const {
recipes,
status,
} = parseArgs({
allowPositionals: true,
args: argv,
options: {
recipes: { type: 'string' },
status: { type: 'boolean' },
},
}).values;
const recipeRelPaths: string[] = recipes?.slice(1, -1).split(' ') ?? [];

if (!status) throw new Error(`Unexpected status: ${status}`);

const rootPath = cwd();

const n = recipeRelPaths.length;
const publications = new Array(n);
for (let r = n - 1; r > -1; r--) {
const recipeRelPath = recipeRelPaths[r];
const recipeAbsPath = path.join(rootPath, recipeRelPath);

publications[r] = bundle(recipeAbsPath)
.then(() => publish(path.join(recipeAbsPath, outfile)));
}

Promise.allSettled(publications)
.then(
() => console.log('Publishing complete'),
() => console.error('Publishing failed'),
);
63 changes: 63 additions & 0 deletions build/publish.spec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import assert from 'node:assert/strict';
import { cwd, argv } from 'node:process';
import {
before,
describe,
it,
mock,
} from 'node:test';


type Mock = ReturnType<typeof mock.fn>['mock'];

describe('Publishing', () => {
const CWD = cwd();
const outfile = 'out.js';
let mock__bundle: Mock;
let mock__publish: Mock;
let mock__consoleErr: Mock;
let mock__consoleLog: Mock;

before(async () => {
const bundle = mock.fn();
mock__bundle = bundle.mock;
const publish = mock.fn();
mock__publish = publish.mock;

mock.module('codemod', {
namedExports: {
publish,
},
});
mock.module('./bundle.mts', {
namedExports: {
bundle,
outfile,
},
});
// mock.method(console, 'error');
// mock.method(console, 'log');
});

it('should', async () => {
mock__bundle.mockImplementation(async () => { });
mock__publish.mockImplementation(() => { });

argv[2] = '--recipes=("a" "b")';
argv[3] = '--status';

await import('./publish.mts');

assert.deepEqual(mock__bundle.calls, [
{ arguments: [`${CWD}/a`] },
{ arguments: [`${CWD}/b`] },
]);

assert.deepEqual(mock__publish.calls, [
{ arguments: [`${CWD}/a`] },
{ arguments: [`${CWD}/b`] },
]);

assert.match(mock__consoleLog.calls[0].arguments[0], /Publishing complete/);
});
});
Loading
Loading