feat: tsup, small fixes

This commit is contained in:
diced 2023-01-06 14:45:48 -08:00
parent b8b1a5bba6
commit eef6fdaeb3
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
28 changed files with 967 additions and 597 deletions

File diff suppressed because one or more lines are too long

View file

@ -6,4 +6,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
yarnPath: .yarn/releases/yarn-3.2.4.cjs
yarnPath: .yarn/releases/yarn-3.3.1.cjs

View file

@ -62,4 +62,4 @@ COPY --from=builder /build/tsconfig.json ./tsconfig.json
COPY --from=builder /build/package.json ./package.json
COPY --from=builder /build/mimes.json ./mimes.json
CMD ["node", "--enable-source-maps", "dist/server"]
CMD ["node", "--enable-source-maps", "dist"]

View file

@ -1,23 +0,0 @@
const esbuild = require('esbuild');
const { existsSync } = require('fs');
const { rm } = require('fs/promises');
const { recursiveReadDir } = require('next/dist/lib/recursive-readdir');
(async () => {
if (existsSync('./dist')) {
await rm('./dist', { recursive: true });
}
const entryPoints = await recursiveReadDir('./src', /.*\.(ts)$/, /(themes|queries|pages)/);
await esbuild.build({
tsconfig: 'tsconfig.json',
outdir: 'dist',
platform: 'node',
entryPoints,
format: 'cjs',
resolveExtensions: ['.ts', '.js'],
write: true,
sourcemap: true,
});
})();

View file

@ -4,15 +4,15 @@
"license": "MIT",
"scripts": {
"dev": "npm-run-all build:server dev:run",
"dev:run": "cross-env DEBUG=true REACT_EDITOR=code NODE_ENV=development RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false node --enable-source-maps dist/server",
"dev:run": "cross-env DEBUG=true REACT_EDITOR=code NODE_ENV=development RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false node --enable-source-maps dist",
"build": "npm-run-all build:server build:schema build:next",
"build-ci": "cross-env ZIPLINE_DOCKER_BUILD=1 npm-run-all build:server build:schema build:next",
"build:server": "node esbuild.config.js",
"build:server": "tsup",
"build:next": "next build",
"build:schema": "prisma generate --schema=prisma/schema.prisma",
"format": "prettier --write ./src/**/*.{ts,tsx} ./*.{md,js,json,yml}",
"migrate:dev": "prisma migrate dev --create-only",
"start": "node dist/server",
"start": "node dist",
"lint": "next lint",
"docker:run": "docker-compose up -d",
"docker:down": "docker-compose down",
@ -82,13 +82,13 @@
"@types/react": "^18.0.26",
"@types/sharp": "^0.31.0",
"cross-env": "^7.0.3",
"esbuild": "^0.16.4",
"eslint": "^8.29.0",
"eslint-config-next": "^13.0.6",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.1",
"tsup": "^6.5.0",
"typescript": "^4.9.4"
},
"repository": {

View file

@ -1,6 +1,6 @@
import { Config } from './config/Config';
import readConfig from './config/readConfig';
import validateConfig from './config/validateConfig';
import { Config } from 'config/Config';
import readConfig from 'config/readConfig';
import validateConfig from 'config/validateConfig';
if (!global.config) global.config = validateConfig(readConfig());

View file

@ -2,8 +2,8 @@ import { parse } from 'dotenv';
import { expand } from 'dotenv-expand';
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import Logger from '../logger';
import { humanToBytes } from '../utils/bytes';
import Logger from 'lib/logger';
import { humanToBytes } from 'utils/bytes';
export type ValueType = 'string' | 'number' | 'boolean' | 'array' | 'json-array' | 'human-to-byte' | 'path';

View file

@ -1,8 +1,8 @@
import { s } from '@sapphire/shapeshift';
import type { Config } from './Config';
import { inspect } from 'util';
import Logger from '../logger';
import { humanToBytes } from '../utils/bytes';
import Logger from 'lib/logger';
import { humanToBytes } from 'utils/bytes';
const discord_content = s
.object({

View file

@ -1,7 +1,7 @@
import { Datasource } from '.';
import { ConfigSupabaseDatasource } from 'lib/config/Config';
import { guess } from '../mimes';
import Logger from '../logger';
import { guess } from 'lib/mimes';
import Logger from 'lib/logger';
import { Readable } from 'stream';
export class Supabase extends Datasource {

View file

@ -1,8 +1,8 @@
import { Image, Url, User } from '@prisma/client';
import config from 'lib/config';
import { ConfigDiscordContent } from 'lib/config/Config';
import Logger from './logger';
import { parseString, ParseValue } from './utils/parser';
import { ConfigDiscordContent } from 'config/Config';
import Logger from 'lib/logger';
import { parseString, ParseValue } from 'utils/parser';
const logger = Logger.get('discord');

View file

@ -1,10 +1,10 @@
import { PrismaClient } from '@prisma/client';
import { readdir, readFile } from 'fs/promises';
import { join } from 'path';
import config from '../lib/config';
import datasource from '../lib/datasource';
import { guess } from '../lib/mimes';
import { migrations } from '../server/util';
import config from 'lib/config';
import datasource from 'lib/datasource';
import { guess } from 'lib/mimes';
import { migrations } from 'server/util';
async function main() {
const directory = process.argv[2];

View file

@ -1,6 +1,6 @@
import { PrismaClient } from '@prisma/client';
import config from '../lib/config';
import { migrations } from '../server/util';
import config from 'lib/config';
import { migrations } from 'server/util';
async function main() {
const extras = (process.argv[2] ?? '').split(',');

View file

@ -1,3 +1,3 @@
import config from '../lib/config';
import config from 'lib/config';
console.log(JSON.stringify(config, null, 2));

View file

@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client';
import { hash } from 'argon2';
import config from '../lib/config';
import { migrations } from '../server/util';
import config from 'lib/config';
import { migrations } from 'server/util';
const SUPPORTED_FIELDS = [
'username',

View file

@ -1,7 +1,7 @@
import { Image } from '@prisma/client';
import { FastifyInstance, FastifyReply } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import exts from '../../lib/exts';
import exts from 'lib/exts';
function dbFileDecorator(fastify: FastifyInstance, _, done) {
fastify.decorateReply('dbFile', dbFile);

View file

@ -1,5 +1,5 @@
import { FastifyInstance, FastifyReply } from 'fastify';
import { guess } from '../../lib/mimes';
import { guess } from 'lib/mimes';
import { extname } from 'path';
import fastifyPlugin from 'fastify-plugin';

View file

@ -1,10 +1,10 @@
import config from 'lib/config';
import datasource from 'lib/datasource';
import Logger from 'lib/logger';
import { version } from '../../package.json';
import config from '../lib/config';
import datasource from '../lib/datasource';
import Logger from '../lib/logger';
import { getStats } from './util';
import { getStats } from 'server/util';
import fastify, { FastifyInstance, FastifyRequest, FastifyServerOptions } from 'fastify';
import fastify, { FastifyInstance, FastifyServerOptions } from 'fastify';
import { createReadStream, existsSync, readFileSync } from 'fs';
import dbFileDecorator from './decorators/dbFile';
import notFound from './decorators/notFound';
@ -12,6 +12,7 @@ import postFileDecorator from './decorators/postFile';
import postUrlDecorator from './decorators/postUrl';
import preFileDecorator from './decorators/preFile';
import rawFileDecorator from './decorators/rawFile';
import allPlugin from './plugins/all';
import configPlugin from './plugins/config';
import datasourcePlugin from './plugins/datasource';
import loggerPlugin from './plugins/logger';
@ -20,7 +21,6 @@ import prismaPlugin from './plugins/prisma';
import rawRoute from './routes/raw';
import uploadsRoute, { uploadsRouteOnResponse } from './routes/uploads';
import urlsRoute, { urlsRouteOnResponse } from './routes/urls';
import { IncomingMessage } from 'http';
const dev = process.env.NODE_ENV === 'development';
const logger = Logger.get('server');
@ -50,7 +50,8 @@ async function start() {
quiet: !dev,
hostname: config.core.host,
port: config.core.port,
});
})
.register(allPlugin);
// decorators
server
@ -151,26 +152,6 @@ async function start() {
server.get('/r/:id', rawRoute.bind(server));
server.get('/', (_, reply) => reply.redirect('/dashboard'));
// initialize next routes after all other routes have been registered so theres no overlap
server.after(() => {
// overrides fastify's default parser so that next.js can handle the request
// in the future Zipline's api will probably be entirely handled by fastify
async function parser(_: FastifyRequest, payload: IncomingMessage) {
return payload;
}
server.addContentTypeParser('text/plain', parser);
server.addContentTypeParser('application/json', parser);
server.addContentTypeParser('multipart/form-data', parser);
server.next('/*', { method: 'ALL' });
server.next('/api/*', { method: 'ALL' });
});
// server.setDefaultRoute((req, res) => {
// server.nextHandle(req, res);
// });
await server.listen({
port: config.core.port,
host: config.core.host ?? '0.0.0.0',

26
src/server/plugins/all.ts Normal file
View file

@ -0,0 +1,26 @@
import { FastifyInstance, FastifyRequest } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import { IncomingMessage } from 'http';
function allRoutes(fastify: FastifyInstance, _: unknown, done: () => void) {
// overrides fastify's default parser so that next.js can handle the request
// in the future Zipline's api will probably be entirely handled by fastify
async function parser(_: FastifyRequest, payload: IncomingMessage) {
return payload;
}
fastify.addContentTypeParser('text/plain', parser);
fastify.addContentTypeParser('application/json', parser);
fastify.addContentTypeParser('multipart/form-data', parser);
fastify.next('/*', { method: 'ALL' });
fastify.next('/api/*', { method: 'ALL' });
done();
}
export default fastifyPlugin(allRoutes, {
name: 'all',
fastify: '4.x',
dependencies: ['config', 'logger', 'prisma', 'next'],
});

View file

@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import { mkdir } from 'fs/promises';
import type { Config } from '../../lib/config/Config';
import type { Config } from 'lib/config/Config';
async function configPlugin(fastify: FastifyInstance, config: Config) {
fastify.decorate('config', config);

View file

@ -1,6 +1,6 @@
import { FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import type { Datasource } from '../../lib/datasources';
import type { Datasource } from 'lib/datasources';
function datasourcePlugin(fastify: FastifyInstance, datasource: Datasource, done: () => void) {
fastify.decorate('datasource', datasource);

View file

@ -1,6 +1,6 @@
import { FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import Logger from '../../lib/logger';
import Logger from 'lib/logger';
function loggerPlugin(fastify: FastifyInstance, _: unknown, done: () => void) {
fastify.decorate('logger', Logger.get('server'));

View file

@ -8,13 +8,13 @@ async function nextPlugin(fastify: FastifyInstance, options: NextServerOptions)
const nextServer = next(options);
const handle = nextServer.getRequestHandler();
await nextServer.prepare();
fastify
.decorate('nextServer', nextServer)
.decorate('nextHandle', handle)
.decorate('next', route.bind(fastify));
return nextServer.prepare();
function route(path, opts: any = { method: 'GET' }) {
if (typeof opts.method === 'string') this[opts.method.toLowerCase()](path, opts, handler);
else if (Array.isArray(opts.method)) {
@ -31,8 +31,6 @@ async function nextPlugin(fastify: FastifyInstance, options: NextServerOptions)
reply.hijack();
}
}
return;
}
export default fastifyPlugin(nextPlugin, {

View file

@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client';
import { FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import { migrations } from '../util';
import { migrations } from 'server/util';
async function prismaPlugin(fastify: FastifyInstance, _, done) {
process.env.DATABASE_URL = fastify.config.core?.database_url;

View file

@ -2,9 +2,9 @@ import { PrismaClient } from '@prisma/client';
import { Migrate } from '@prisma/migrate/dist/Migrate';
import { ensureDatabaseExists } from '@prisma/migrate/dist/utils/ensureDatabaseExists';
import { ServerResponse } from 'http';
import { Datasource } from '../lib/datasources';
import Logger from '../lib/logger';
import { bytesToHuman } from '../lib/utils/bytes';
import { Datasource } from 'lib/datasources';
import Logger from 'lib/logger';
import { bytesToHuman } from 'lib/utils/bytes';
export async function migrations() {
const logger = Logger.get('database::migrations');

View file

@ -19,7 +19,9 @@
"components/*": ["components/*"],
"hooks/*": ["lib/hooks/*"],
"middleware/*": ["lib/middleware/*"],
"lib/*": ["lib/*"]
"lib/*": ["lib/*"],
"config/*": ["lib/config/*"],
"utils/*": ["lib/utils/*"]
},
"incremental": true
},

37
tsup.config.ts Normal file
View file

@ -0,0 +1,37 @@
import { defineConfig, Options } from 'tsup';
const opts: Options = {
platform: 'node',
format: ['cjs'],
treeshake: true,
clean: true,
sourcemap: true,
};
export default defineConfig([
{
entryPoints: ['src/server/index.ts'],
...opts,
},
// scripts
{
entryPoints: ['src/scripts/import-dir.ts'],
outDir: 'dist/scripts',
...opts,
},
{
entryPoints: ['src/scripts/list-users.ts'],
outDir: 'dist/scripts',
...opts,
},
{
entryPoints: ['src/scripts/read-config.ts'],
outDir: 'dist/scripts',
...opts,
},
{
entryPoints: ['src/scripts/set-user.ts'],
outDir: 'dist/scripts',
...opts,
},
]);

759
yarn.lock

File diff suppressed because it is too large Load diff

2
zip-env.d.ts vendored
View file

@ -7,7 +7,7 @@ declare global {
interface Global {
prisma: PrismaClient;
config: Config;
datasource: Datasource
datasource: Datasource;
}
interface ProcessEnv {