Skip to content

Commit

Permalink
Merge pull request #3066 from metacpan/haarg/deps-from-npm
Browse files Browse the repository at this point in the history
use vendor libraries from npm and various fixes
  • Loading branch information
haarg authored May 18, 2024
2 parents 7f87e3b + 4380596 commit b08d4e9
Show file tree
Hide file tree
Showing 179 changed files with 2,631 additions and 36,828 deletions.
13 changes: 6 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN \
EOT

# not supported yet
#COPY --parents build-assets.mjs root/static .
#COPY --parents build-assets.mjs root/static ./

COPY build-assets.mjs ./
COPY root/static root/static
Expand All @@ -41,7 +41,7 @@ WORKDIR /app/
COPY cpanfile cpanfile.snapshot ./
RUN \
--mount=type=cache,target=/root/.perl-cpm,sharing=private \
<<EOT /bin/bash -euo pipefail
<<EOT
cpm install --show-build-log-on-failure --resolver=snapshot
EOT

Expand Down Expand Up @@ -73,7 +73,7 @@ USER root

RUN \
--mount=type=cache,target=/root/.perl-cpm \
<<EOT /bin/bash -euo pipefail
<<EOT
cpm install --show-build-log-on-failure --resolver=snapshot --with-develop
chown -R metacpan:users ./
EOT
Expand All @@ -82,7 +82,6 @@ USER metacpan

################### Test Runner
FROM develop AS test
SHELL [ "/bin/bash", "-euo", "pipefail", "-c" ]

ENV NO_UPDATE_NOTIFIER=1
ENV PLACK_ENV=
Expand All @@ -93,7 +92,7 @@ RUN \
--mount=type=cache,target=/var/cache/apt,sharing=private \
--mount=type=cache,target=/var/lib/apt/lists,sharing=private \
--mount=type=cache,target=/root/.npm,sharing=private \
<<EOT /bin/bash -euo pipefail
<<EOT
curl -fsSL https://deb.nodesource.com/setup_21.x | bash -
apt-get update
apt-get satisfy -y -f --no-install-recommends 'nodejs (>= 21.6.1)'
Expand All @@ -103,14 +102,14 @@ EOT
COPY package.json package-lock.json ./
RUN \
--mount=type=cache,target=/root/.npm,sharing=private \
<<EOT /bin/bash -euo pipefail
<<EOT
npm install --verbose --include=dev
npm audit fix
EOT

RUN \
--mount=type=cache,target=/root/.perl-cpm \
<<EOT /bin/bash -euo pipefail
<<EOT
cpm install --show-build-log-on-failure --resolver=snapshot --with-test
EOT

Expand Down
67 changes: 44 additions & 23 deletions build-assets.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
"use strict";
import * as esbuild from 'esbuild'
import { lessLoader } from 'esbuild-plugin-less';
import fs from 'fs';
import { sassPlugin } from 'esbuild-sass-plugin';
import { writeFile, opendir, unlink } from 'node:fs/promises';
import path from 'node:path';
import parseArgs from 'minimist';

const assets_dir = 'root/assets';

const config = {
entryPoints: [
'root/static/js/main.js',
'root/static/js/main.mjs',
'root/static/less/style.less',
'root/static/scss/style.scss',
],
assetNames: '[name]-[hash]',
entryNames: '[name]-[hash]',
outdir: 'root/assets',
format: 'esm',
outdir: assets_dir,
bundle: true,
sourcemap: true,
metafile: true,
Expand All @@ -26,37 +32,34 @@ const config = {
},
plugins: [
lessLoader(),
sassPlugin(),
new class {
name = 'metacpan-build';

setup(build) {
build.onResolve(
{ filter: /^(shCore|xregexp)$/ },
args => ({ external: true }),
);
build.onStart(() => {
console.log('building assets...')
});
build.onResolve(
{ filter: /^\// },
args => ({ external: true }),
);
build.onEnd(result => {
build.onEnd(async result => {
const metafile = result.metafile;
if (metafile && metafile.outputs) {
const files = Object.keys(metafile.outputs).sort()
.map(file => file.replace(/^root\/assets\//, ''));
fs.writeFile(
'root/assets/assets.json',
JSON.stringify(files),
'utf8',
(e) => {
if (e) {
console.log(e);
}
}
);
console.log('assets built');
}
else {
console.log('asset build failure');
.map(file => path.relative(assets_dir, file));
try {
await writeFile(
path.join(assets_dir, 'assets.json'),
JSON.stringify(files),
'utf8',
);
}
catch (e) {
console.log(e);
}
console.log(`build complete (${files.filter(f => !f.match(/\.map$/)).join(' ')})`);
}
});
}
Expand All @@ -68,11 +71,29 @@ const args = parseArgs(process.argv, {
boolean: [
'watch',
'minify',
'clean',
],
});
if (args.minify) {
config.minify = true;
}
if (args.clean) {

for await (const file of await opendir(assets_dir, { withFileTypes: true })) {
const filePath = path.join(file.parentPath, file.name);
if (file.name.match(/^\./)) {
// ignore these
}
else if (!file.isFile()) {
console.log(`cowardly refusing to remove non-file ${filePath}`);
}
else {
console.log(`deleting ${filePath}`);
await unlink(filePath);
}
}
}

const ctx = await esbuild.context(config);
if (args.watch) {
await ctx.watch();
Expand Down
6 changes: 3 additions & 3 deletions lib/MetaCPAN/Web/Controller/Account/Favorite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ sub add : Local : Args(0) {
}

if ($json) {
$c->res->code(400) if ( $res->{error} );
$c->res->code(400) if $res->{error};
$c->stash->{json}{success} = $res->{error} ? \0 : \1;
}
else {
$c->res->redirect(
$res->{error}
? $c->req->referer
: $c->uri_for('/account/turing/index')
? $c->uri_for('/account/turing/index')
: $c->req->referer
);
}
}
Expand Down
Loading

0 comments on commit b08d4e9

Please sign in to comment.