-
-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-service): add start.sh to init packages on start;
- Loading branch information
Showing
10 changed files
with
176 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { ObjectId } from 'bson' | ||
import fse = require('fs-extra') | ||
import path = require('path') | ||
import { Constants } from '../constants' | ||
import { DatabaseAgent } from '../lib/database' | ||
import { execSync } from 'child_process' | ||
|
||
/** | ||
* 在 node_modules 中创建 云函数 sdk 包:@, 这个包是为了云函数IDE 加载类型提示文件而创建的,不可发布 | ||
*/ | ||
export function createCloudFunctionDeclarationPackage() { | ||
const source = path.resolve(__dirname, '../../dist') | ||
const target = path.resolve(__dirname, '../../node_modules/@') | ||
|
||
fse.ensureDirSync(target) | ||
fse.copySync(source, target) | ||
|
||
console.log(`copy success: ${source} => ${target}`) | ||
|
||
const packageJson = ` | ||
{ | ||
"name": "@", | ||
"version": "0.0.0" | ||
} | ||
` | ||
const pkgJsonPath = path.join(target, 'package.json') | ||
fse.writeFileSync(pkgJsonPath, packageJson) | ||
|
||
console.log(`write success: ${pkgJsonPath}`) | ||
} | ||
|
||
export function isCloudSdkPackageExists() { | ||
const target = path.resolve(__dirname, '../../../node_modules/@') | ||
const pkgJsonPath = path.join(target, 'package.json') | ||
return fse.existsSync(pkgJsonPath) | ||
} | ||
|
||
export function initCloudSdkPackage() { | ||
if (!isCloudSdkPackageExists()) { | ||
createCloudFunctionDeclarationPackage() | ||
} | ||
} | ||
|
||
|
||
interface AppConfigItem { | ||
_id: ObjectId | ||
key: string | ||
value: { | ||
name: string, | ||
version: string | ||
}[] | ||
} | ||
|
||
/** | ||
* Get extra npm packages | ||
* @returns | ||
*/ | ||
export async function getExtraPackages() { | ||
await DatabaseAgent.accessor.ready | ||
const db = DatabaseAgent.db | ||
const doc = await db.collection<AppConfigItem>(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) { | ||
return | ||
} | ||
|
||
const names = packages | ||
.map(pkg => { | ||
return pkg.version ? `${pkg.name}@${pkg.version}` : `${pkg.name}` | ||
}) | ||
|
||
const cmd_str = names.join(' ') | ||
const r = execSync(`npm install ${cmd_str}`) | ||
return r.toString() | ||
} | ||
|
||
/** | ||
* Check if node module exists | ||
* @param moduleName | ||
* @returns | ||
*/ | ||
export function moduleExists(mod: string) { | ||
try { | ||
require.resolve(mod) | ||
return true | ||
} catch (_err) { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { getExtraPackages, initCloudSdkPackage, installPackages, moduleExists } from "./api/init" | ||
|
||
|
||
async function main() { | ||
const packages = await getExtraPackages() | ||
if (!packages.length) { | ||
console.log('no extra packages found') | ||
return 0 | ||
} | ||
|
||
console.log('packages loaded: ', packages) | ||
|
||
const not_exists = packages.filter(pkg => !moduleExists(pkg.name)) | ||
if (!not_exists.length) { | ||
console.log('no new packages to be installed') | ||
return 0 | ||
} | ||
|
||
try { | ||
const res = installPackages(packages) | ||
console.log(res) | ||
|
||
initCloudSdkPackage() | ||
} catch (error) { | ||
console.error(error) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
|
||
main().then(code => { | ||
process.exit(code) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
echo "****** init start ******" | ||
node ./dist/init.js | ||
echo "****** init end *******" | ||
|
||
# source .env | ||
echo "****** start service: node $FLAGS ./dist/index.js *******" | ||
exec node $FLAGS ./dist/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters