Skip to content

Commit

Permalink
fix(runtime): update deps installation impl to new design (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Dec 16, 2022
1 parent 24a497d commit cdda443
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 61 deletions.
2 changes: 1 addition & 1 deletion runtimes/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"create-internal-pkg": "node ./scripts/create-internal-package.js",
"prepublishOnly": "npm run build",
"trace-gc": "node --trace_gc --trace_gc_verbose ./dist/index.js",
"init-start": "npm run start"
"init": "node ./dist/init.js"
},
"keywords": [
"laf",
Expand Down
34 changes: 10 additions & 24 deletions runtimes/nodejs/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import { ensureCollectionIndexes, getExtraPackages, initCloudSdkPackage, installPackages, moduleExists } from "./support/init"
import { logger } from "./support/logger"

import {
ensureCollectionIndexes,
initCloudSdkPackage,
installPackages,
} from './support/init'
import { logger } from './support/logger'

async function main() {
try {
const packages = await getExtraPackages()
if (!packages.length) {
logger.info('no extra packages found')
}

logger.info('packages loaded: ', packages)

const not_exists = packages.filter(pkg => !moduleExists(pkg.name))
if (packages.length && !not_exists.length) {
logger.info('no new packages to be installed')
}

if (not_exists.length) {
const res = installPackages(packages)
logger.info(res)
}
installPackages()

initCloudSdkPackage()

await ensureCollectionIndexes()
} catch (error) {
logger.error(error)
Expand All @@ -32,12 +19,11 @@ async function main() {
return 0
}


main()
.then(code => {
.then((code) => {
process.exit(code)
})
.catch(err => {
.catch((err) => {
logger.error(err)
process.exit(2)
})
})
43 changes: 7 additions & 36 deletions runtimes/nodejs/src/support/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ObjectId } from 'bson'
import fse = require('fs-extra')
import path = require('path')
import { Constants } from '../constants'
Expand Down Expand Up @@ -42,50 +41,21 @@ export function initCloudSdkPackage() {
}
}

interface AppConfigItem {
_id: ObjectId
key: string
value: {
name: string
version: string
}[]
}

/**
* Get extra npm packages
* @returns
*/
export async function getExtraPackages() {
const { DatabaseAgent } = require('../db') // init.ts should not import db globally, because init.ts would be referenced in build time

await DatabaseAgent.accessor.ready
const db = DatabaseAgent.db
const doc: AppConfigItem = await db
.collection(Constants.config_collection)
.findOne({ key: 'packages' })

return doc?.value ?? []
}

/**
* Install packages
* @param packages
* @returns
*/
export function installPackages(packages: { name: string; version: string }[]) {
if (!packages?.length) {
export function installPackages() {
const deps = process.env.DEPENDENCIES || ''
if (!deps) {
return
}

const names = packages.map((pkg) => {
return pkg.version ? `${pkg.name}@${pkg.version}` : `${pkg.name}`
})

const cmd_str = names.join(' ')
const flags = Config.NPM_INSTALL_FLAGS
logger.info('run command: ', `npm install ${cmd_str} ${flags}`)
const r = execSync(`npm install ${cmd_str} ${flags}`)
return r.toString()
logger.info('run command: ', `npm install ${deps} ${flags}`)
const r = execSync(`npm install ${deps} ${flags}`)
console.log(r.toString())
}

/**
Expand All @@ -110,6 +80,7 @@ export function moduleExists(mod: string) {
export async function ensureCollectionIndexes(): Promise<any> {
// init.ts should not import db globally, because init.ts would be referenced in build time
const { DatabaseAgent } = require('../db')
await DatabaseAgent.accessor.ready
const db = DatabaseAgent.db
await db.collection(Constants.function_log_collection).createIndexes([
{
Expand Down

0 comments on commit cdda443

Please sign in to comment.