Skip to content

Commit

Permalink
Update dependencies (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jun 9, 2024
1 parent 26d0b0e commit ff9ad45
Show file tree
Hide file tree
Showing 6 changed files with 5,409 additions and 10,859 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ jobs:
Lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: package.json
cache: npm
- name: install
run: npm ci || npm install
- name: XO
Expand Down
69 changes: 35 additions & 34 deletions api/bundle.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
const execa = require('execa');
const path = require('path');
const rollup = require('rollup');
const tempdir = require('tempdir');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const cleanup = require('rollup-plugin-cleanup');
const camelcase = require('camelcase');
const mem = require('mem');
import {resolve} from 'node:path';
import process from 'node:process';
import {execa} from 'execa';
import {rollup} from 'rollup';
import {sync} from 'tempdir';
import rollupResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import cleanup from 'rollup-plugin-cleanup';
import camelcase from 'camelcase';
import memoize from 'memoize';

const cwd = tempdir.sync();
const home = tempdir.sync();
const cwd = sync();
const home = sync();

async function run(name, args) {
console.log('💻 Running', name, ...args);
const running = execa(name, args, {
async function run(name, arguments_) {
console.log('💻 Running', name, ...arguments_);
const running = execa(name, arguments_, {
cwd,
env: {
HOME: home
}
HOME: home,
},
});

running.stdout.pipe(process.stdout);
Expand All @@ -36,12 +37,12 @@ class UnprocessableError extends Error {
* @param {import('@vercel/node').VercelRequest} request
* @param {import('@vercel/node').VercelResponse} response
*/
module.exports = async (request, response) => {
export default async function handle(request, response) {
try {
console.log('✅ Processing', request.query.pkg);
const {version, code} = await bundle(
request.query.pkg,
request.query.global ?? request.query.name
request.query.global ?? request.query.name,
);
response.setHeader('x-bundle-version', version);
response.send(code);
Expand All @@ -51,18 +52,18 @@ module.exports = async (request, response) => {
response.status(error.statusCode ?? 500);
response.send(error.message);
}
};
}

const bundle = mem(async (nameRequest, globalName) => {
const bundle = memoize(async (nameRequest, globalName) => {
if (/[^a-z\d@/-]/i.test(nameRequest)) {
throw new UnprocessableError('Invalid package name');
}

console.log('⏳ Getting info about', nameRequest);
const infoProcess = await run('npm', ['view', nameRequest, '--json']);
const pkg = JSON.parse(infoProcess.stdout);
const isFregante = pkg.maintainers.some(
user => ['fregante', 'bfred-it'].includes(user.split(' ')[0])
const package_ = JSON.parse(infoProcess.stdout);
const isFregante = package_.maintainers.some(
user => ['fregante', 'bfred-it'].includes(user.split(' ')[0]),
);
if (!isFregante) {
throw new UnprocessableError('Only fregante packages allowed');
Expand All @@ -75,38 +76,38 @@ const bundle = mem(async (nameRequest, globalName) => {
'--omit=dev',
'--ignore-scripts',
'--no-audit',
'--no-bin-links'
'--no-bin-links',
]);

const packagePath = path.resolve(cwd, 'node_modules', pkg.name);
const packagePath = resolve(cwd, 'node_modules', package_.name);
return {
version: pkg.version,
version: package_.version,
code: await bundleWithRollup(
packagePath,
globalName ?? camelcase(pkg.name)
)
globalName ?? camelcase(package_.name),
),
};
}, {
cacheKey: JSON.stringify
cacheKey: JSON.stringify,
});

console.clear();
console.log('✅ Node server running');

async function bundleWithRollup(input, name) {
const bundle = await rollup.rollup({
const bundle = await rollup({
input,
plugins: [
resolve({browser: true}),
rollupResolve({browser: true}),
commonjs(),
cleanup()
]
cleanup(),
],
});

const result = await bundle.generate({
format: 'iife',
extend: name === 'window',
name
name,
});

if (result.output.length > 1) {
Expand Down
Loading

0 comments on commit ff9ad45

Please sign in to comment.