From c6ef80bdf0380d6a11a886ae91bf8ad7f49db19e Mon Sep 17 00:00:00 2001 From: maslow Date: Sat, 17 Dec 2022 05:23:52 +0800 Subject: [PATCH] fix(runtime): update deps installation impl to new design --- runtimes/nodejs/package.json | 2 +- runtimes/nodejs/src/init.ts | 34 +++++++---------------- runtimes/nodejs/src/support/init.ts | 43 +++++------------------------ 3 files changed, 18 insertions(+), 61 deletions(-) diff --git a/runtimes/nodejs/package.json b/runtimes/nodejs/package.json index b81c018f7c..85d4f67595 100644 --- a/runtimes/nodejs/package.json +++ b/runtimes/nodejs/package.json @@ -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", diff --git a/runtimes/nodejs/src/init.ts b/runtimes/nodejs/src/init.ts index 17c8598bd0..29f60c1be6 100644 --- a/runtimes/nodejs/src/init.ts +++ b/runtimes/nodejs/src/init.ts @@ -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) @@ -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) - }) \ No newline at end of file + }) diff --git a/runtimes/nodejs/src/support/init.ts b/runtimes/nodejs/src/support/init.ts index 412ada2fd2..82a819144d 100644 --- a/runtimes/nodejs/src/support/init.ts +++ b/runtimes/nodejs/src/support/init.ts @@ -1,4 +1,3 @@ -import { ObjectId } from 'bson' import fse = require('fs-extra') import path = require('path') import { Constants } from '../constants' @@ -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()) } /** @@ -110,6 +80,7 @@ export function moduleExists(mod: string) { export async function ensureCollectionIndexes(): Promise { // 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([ {