Revert "build: allow node package depends on workspace packages (#11892)"

This reverts commit c00671dd84.
This commit is contained in:
liuyi
2025-04-23 19:24:51 +08:00
parent c00671dd84
commit 5d9a3aac5b
28 changed files with 327 additions and 328 deletions

View File

@@ -111,7 +111,7 @@ test('should tell flavors correctly', t => {
sync: true,
renderer: true,
doc: true,
script: false,
script: true,
});
process.env.SERVER_FLAVOR = 'graphql';
@@ -122,15 +122,6 @@ test('should tell flavors correctly', t => {
doc: false,
script: false,
});
process.env.SERVER_FLAVOR = 'script';
t.deepEqual(new Env().flavors, {
graphql: false,
sync: false,
renderer: false,
doc: false,
script: true,
});
});
test('should tell selfhosted correctly', t => {

View File

@@ -9,7 +9,6 @@ import {
CloudThrottlerGuard,
Config,
GlobalExceptionFilter,
URLHelper,
} from './base';
import { SocketIoAdapter } from './base/websocket';
import { AuthGuard } from './core/auth';
@@ -17,7 +16,7 @@ import { serverTimingAndCache } from './middleware/timing';
const OneMB = 1024 * 1024;
export async function run() {
export async function createApp() {
const { AppModule } = await import('./app.module');
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
@@ -29,8 +28,7 @@ export async function run() {
app.useBodyParser('raw', { limit: 100 * OneMB });
const logger = app.get(AFFiNELogger);
app.useLogger(logger);
app.useLogger(app.get(AFFiNELogger));
const config = app.get(Config);
if (config.server.path) {
@@ -59,12 +57,5 @@ export async function run() {
const adapter = new SocketIoAdapter(app);
app.useWebSocketAdapter(adapter);
const url = app.get(URLHelper);
const listeningHost = '0.0.0.0';
await app.listen(config.server.port, listeningHost);
logger.log(`AFFiNE Server is running in [${env.DEPLOYMENT_TYPE}] mode`);
logger.log(`Listening on http://${listeningHost}:${config.server.port}`);
logger.log(`And the public server should be recognized as ${url.home}`);
return app;
}

View File

@@ -1,5 +1,5 @@
import { appendFileSync, writeFileSync } from 'node:fs';
import { join, parse } from 'node:path';
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Logger } from '@nestjs/common';
@@ -45,21 +45,15 @@ export class CreateCommand extends CommandRunner {
const timestamp = Date.now();
const content = this.createScript(upperFirst(camelCase(name)) + timestamp);
const migrationDir = join(
fileURLToPath(import.meta.url),
'../../migrations'
);
const fileName = `${timestamp}-${kebabCase(name)}.ts`;
const filePath = join(migrationDir, fileName);
const filePath = join(
fileURLToPath(import.meta.url),
'../../migrations',
fileName
);
this.logger.log(`Creating ${fileName}...`);
writeFileSync(filePath, content);
const indexFile = join(migrationDir, 'index.ts');
appendFileSync(
indexFile,
`export * from './${parse(fileName).name}';`,
'utf-8'
);
this.logger.log(`Migration file created at ${filePath}`);
this.logger.log('Done');
}

View File

@@ -1,12 +1,15 @@
import { readdirSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { Logger } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { PrismaClient } from '@prisma/client';
import { once } from 'lodash-es';
import { Command, CommandRunner } from 'nest-commander';
import * as migrations from '../migrations';
interface Migration {
file: string;
name: string;
always?: boolean;
up: (db: PrismaClient, injector: ModuleRef) => Promise<void>;
@@ -14,15 +17,33 @@ interface Migration {
}
export const collectMigrations = once(async () => {
return Object.values(migrations).map(migration => {
return {
name: migration.name,
// @ts-expect-error optional
always: migration.always,
up: migration.up,
down: migration.down,
};
}) as Migration[];
const folder = join(fileURLToPath(import.meta.url), '../../migrations');
const migrationFiles = readdirSync(folder)
.filter(desc =>
desc.endsWith(import.meta.url.endsWith('.ts') ? '.ts' : '.js')
)
.map(desc => join(folder, desc));
migrationFiles.sort((a, b) => a.localeCompare(b));
const migrations: Migration[] = await Promise.all(
migrationFiles.map(async file => {
return import(pathToFileURL(file).href).then(mod => {
const migration = mod[Object.keys(mod)[0]];
return {
file,
name: migration.name,
always: migration.always,
up: migration.up,
down: migration.down,
};
});
})
);
return migrations;
});
@Command({

View File

@@ -1,12 +1,16 @@
import { Logger } from '@nestjs/common';
import { CommandFactory } from 'nest-commander';
import { CliAppModule } from './data/app';
async function bootstrap() {
process.env.SERVER_FLAVOR = 'script';
export async function run() {
await import('../prelude');
const { CliAppModule } = await import('./app');
await CommandFactory.run(CliAppModule, new Logger()).catch(e => {
console.error(e);
process.exit(1);
});
process.exit(0);
}
await bootstrap();

View File

@@ -1,7 +0,0 @@
export * from './0001-refresh-features';
export * from './1698398506533-guid';
export * from './1703756315970-unamed-account';
export * from './1721299086340-refresh-unnamed-user';
export * from './1732861452428-migrate-invite-status';
export * from './1733125339942-universal-subscription';
export * from './1738590347632-feature-redundant';

View File

@@ -102,8 +102,7 @@ export class Env implements AppEnv {
sync: this.isFlavor(Flavor.Sync),
renderer: this.isFlavor(Flavor.Renderer),
doc: this.isFlavor(Flavor.Doc),
// Script in a special flavor, return true only when it is set explicitly
script: this.FLAVOR === Flavor.Script,
script: this.isFlavor(Flavor.Script),
};
}

View File

@@ -1,11 +1,20 @@
/// <reference types="./global.d.ts" />
import './prelude';
import { run as runCli } from './cli';
import { run as runServer } from './server';
import { Logger } from '@nestjs/common';
if (env.flavors.script) {
await runCli();
} else {
await runServer();
}
import { createApp } from './app';
import { Config, URLHelper } from './base';
const app = await createApp();
const config = app.get(Config);
const url = app.get(URLHelper);
const listeningHost = '0.0.0.0';
await app.listen(config.server.port, listeningHost);
const logger = new Logger('App');
logger.log(`AFFiNE Server is running in [${env.DEPLOYMENT_TYPE}] mode`);
logger.log(`Listening on http://${listeningHost}:${config.server.port}`);
logger.log(`And the public server should be recognized as ${url.home}`);

View File

@@ -1,4 +1,17 @@
import * as serverNativeModule from '@affine/server-native';
import { createRequire } from 'node:module';
let serverNativeModule: typeof import('@affine/server-native');
try {
serverNativeModule = await import('@affine/server-native');
} catch {
const require = createRequire(import.meta.url);
serverNativeModule =
process.arch === 'arm64'
? require('../server-native.arm64.node')
: process.arch === 'arm'
? require('../server-native.armv7.node')
: require('../server-native.node');
}
export const mergeUpdatesInApplyWay = serverNativeModule.mergeUpdatesInApplyWay;