Skip to content

Commit

Permalink
fix: create module json first
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Aug 16, 2024
1 parent 3f5fcd8 commit d466036
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
18 changes: 12 additions & 6 deletions apps/core/src/global/index.global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cluster from 'node:cluster'
import { mkdirSync } from 'node:fs'
import { mkdirSync, writeFileSync } from 'node:fs'

import { Logger } from '@nestjs/common'

Expand All @@ -21,7 +21,7 @@ import './dayjs.global'
import '@mx-space/external/zx-global'

// 建立目录
function mkdirs() {
function createAppFolders() {
if (!CLUSTER.enable || cluster.isPrimary) {
mkdirSync(DATA_DIR, { recursive: true })
Logger.log(chalk.blue(`数据目录已经建好:${DATA_DIR}`))
Expand All @@ -35,8 +35,14 @@ function mkdirs() {
Logger.log(chalk.blue(`文件存放目录已经建好:${STATIC_FILE_DIR}`))
mkdirSync(STATIC_FILE_TRASH_DIR, { recursive: true })
Logger.log(chalk.blue(`文件回收站目录已经建好:${STATIC_FILE_TRASH_DIR}`))
// mkdirSync(THEME_DIR, { recursive: true })
// Logger.log(chalk.blue(`主题目录已经建好:${THEME_DIR}`))

const packageJSON = `${DATA_DIR}/package.json`
const hasPKG = fs.existsSync(packageJSON)
if (!hasPKG) {
writeFileSync(packageJSON, '{"name":"modules"}', {
flag: 'a',
})
}
}
}

Expand All @@ -60,9 +66,9 @@ function nodeEnvInjection() {
process.env.CBOR_NATIVE_ACCELERATION_DISABLED = 'true'
}

export function register() {
export function initializeApp() {
registerGlobal()
nodeEnvInjection()
registerJSONGlobal()
mkdirs()
createAppFolders()
}
4 changes: 2 additions & 2 deletions apps/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { DEBUG_MODE } from './app.config'
import { registerForMemoryDump } from './dump'
import { logger } from './global/consola.global'
import { isMainCluster, isMainProcess } from './global/env.global'
import { register } from './global/index.global'
import { initializeApp } from './global/index.global'
import { migrateDatabase } from './migration/migrate'

process.title = `Mix Space (${cluster.isPrimary ? 'master' : 'worker'}) - ${
process.env.NODE_ENV
}`

async function main() {
register()
initializeApp()

if (isMainProcess) {
await migrateDatabase()
Expand Down
4 changes: 2 additions & 2 deletions apps/core/src/migration/helper/encrypt-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { plainToInstance } from 'class-transformer'
import type { IConfigKeys } from '~/modules/configs/configs.interface'

import { ENCRYPT } from '~/app.config'
import { register } from '~/global/index.global'
import { initializeApp } from '~/global/index.global'
import { generateDefaultConfig } from '~/modules/configs/configs.default'
import * as optionDtos from '~/modules/configs/configs.dto'
import { encryptObject } from '~/modules/configs/configs.encrypt.util'
Expand All @@ -25,7 +25,7 @@ Object.entries(optionDtos).reduce((obj, [key, value]) => {
}, {})

async function main() {
await register()
await initializeApp()
const connection = await getDatabaseConnection()
const db = connection.db!
const configs: any[] = []
Expand Down
15 changes: 8 additions & 7 deletions apps/core/src/processors/helper/helper.image.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { encode } from 'blurhash'
import type { ImageModel } from '~/shared/model/image.model'
import type { Sharp } from 'sharp'

import { Injectable, Logger } from '@nestjs/common'
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'

import { ConfigsService } from '~/modules/configs/configs.service'
import { pickImagesFromMarkdown } from '~/utils/pic.util'
Expand All @@ -11,19 +11,20 @@ import { requireDepsWithInstall } from '~/utils/tool.util'
import { HttpService } from './helper.http.service'

@Injectable()
export class ImageService {
export class ImageService implements OnModuleInit {
private logger: Logger
constructor(
private readonly httpService: HttpService,
private readonly configsService: ConfigsService,
) {
this.logger = new Logger(ImageService.name)
}

if (!isDev) {
requireDepsWithInstall('sharp').catch((error: any) => {
this.logger.error(`sharp install failed: ${error.message}`)
})
}
onModuleInit() {
requireDepsWithInstall('sharp').catch((error: any) => {
this.logger.error(`sharp install failed: ${error.message}`)
console.error(error)
})
}

async saveImageDimensionsFromMarkdownText(
Expand Down
4 changes: 2 additions & 2 deletions apps/core/src/repl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { repl } from '@nestjs/core'

import { register } from './global/index.global'
import { initializeApp } from './global/index.global'

async function bootstrap() {
register()
initializeApp()
const { AppModule } = await import('./app.module')
await repl(AppModule)
}
Expand Down
7 changes: 5 additions & 2 deletions apps/core/src/utils/tool.util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createHash } from 'node:crypto'
import { createRequire } from 'node:module'
import { join } from 'node:path'
import { cloneDeep } from 'lodash'

import { installPackage } from '@antfu/install-pkg'

import { NODE_REQUIRE_PATH } from '~/constants/path.constant'
import { logger } from '~/global/consola.global'

export const md5 = (text: string) =>
Expand Down Expand Up @@ -156,11 +158,12 @@ export function escapeXml(unsafe: string) {

export const requireDepsWithInstall = async (deps: string) => {
try {
const require = createRequire(NODE_REQUIRE_PATH)
return require(require.resolve(deps))
} catch {
logger.info(`Installing ${deps}...`)
await installPackage(deps, { silent: false })

await installPackage(deps, { silent: false, cwd: NODE_REQUIRE_PATH })
const require = createRequire(NODE_REQUIRE_PATH)
return require(deps)
}
}

0 comments on commit d466036

Please sign in to comment.