Skip to content

Commit 0cdf97d

Browse files
committed
add support for dynamic import
1 parent ecdc54c commit 0cdf97d

12 files changed

+184
-86
lines changed

README.md

+24-15
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ You can use it like on example below.
1212
App will exit if there at least on step is failed.
1313
Check all connections before starting server:
1414

15-
You can create file for your services like `service-startup.ts`
15+
You can create file for your services like `service-startup.ts`.
16+
Note that this file will be important automatically,
17+
so you don't need to import it. But you can create own separate files
18+
1619
```typescript
17-
import colors from 'colors'
18-
import starter from 'service-startup'
20+
import serviceStartup from 'service-startup'
1921
// databases and others...
2022

21-
export default starter([
23+
serviceStartup.addPrioritizedSteps([
2224
{
2325
name: 'PostgreSQL',
2426
onRun: () => client.connect(),
@@ -27,24 +29,31 @@ export default starter([
2729
name: 'MongoDB',
2830
onRun: () => connectMongo(),
2931
},
30-
{
31-
name: 'Media Service',
32-
envBlackList: ['test'],
33-
onRun: async () => {
34-
mediaApi.setCredentials(config.media)
35-
await mediaApi.me.info()
36-
},
32+
])
33+
```
34+
35+
Also you can add steps from inside single library files to support dynamic import:
36+
37+
```tsx
38+
// lib/media.ts
39+
import serviceStartup from 'service-startup'
40+
41+
serviceStartup.addStep({
42+
name: 'Media Service',
43+
envBlackList: ['test'],
44+
onRun: async () => {
45+
mediaApi.setCredentials(config.media)
46+
await mediaApi.me.info()
3747
},
38-
], {
39-
successSymbol: colors.green('[READY]'),
4048
})
4149
```
4250

51+
4352
You can use it to wait for services start at `server.ts`:
4453
```typescript
45-
import startServices from './startServices'
54+
import serviceStartup from 'service-startup'
4655

47-
startServices.then(() => {
56+
serviceStartup.start().then(() => {
4857
server.listen(3000, () => {
4958
console.log(`Listening port: 3000`)
5059
})

index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import starter from './src/starter'
1+
import serviceStartup from './src/startup'
22

3-
export * from './src/starter'
3+
export * from './src/startup'
44

5-
export default starter
5+
export default serviceStartup

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "service-startup",
3-
"version": "0.1.1",
3+
"version": "1.0.0",
44
"description": "",
55
"main": "./index",
66
"scripts": {

src/classes/StepsList.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import _ from 'lodash'
2+
3+
export interface StartupStep {
4+
name: string
5+
onRun: Function
6+
isRequired?: boolean
7+
envWhiteList?: string | string[]
8+
envBlackList?: string | string[]
9+
}
10+
11+
12+
class StepsList {
13+
steps: StartupStep[] = []
14+
15+
addStep(step: StartupStep) {
16+
if(!step) return null
17+
this.steps.push(step)
18+
}
19+
20+
addSteps(steps: StartupStep[]) {
21+
steps = _.compact(steps)
22+
this.steps = this.steps.concat(steps)
23+
}
24+
25+
addPrioritizedSteps(steps: StartupStep[]) {
26+
steps = _.compact(steps)
27+
this.steps = steps.concat(this.steps)
28+
}
29+
30+
getSteps(): StartupStep[] {
31+
return this.steps
32+
}
33+
}
34+
35+
36+
export default StepsList

src/executeStep.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// import { Spinner } from 'cli-spinner'
2-
import logSymbols from 'log-symbols'
3-
import isEnvOk from './isEnvOk'
4-
import { StarterStep } from './stepsList'
2+
import isEnvOk from './utils/isEnvOk'
3+
import { StartupStep } from './classes/StepsList'
4+
import colors from 'colors'
55

66
export interface ExecuteStepConfig {
77
successSymbol?: string
88
errorSymbol?: string
99
env?: string
1010
}
1111

12-
async function executeStep(step: StarterStep, config?: ExecuteStepConfig) {
13-
const { successSymbol = logSymbols.success, errorSymbol = logSymbols.error, env = process.env.NODE_ENV } = config || { }
12+
async function executeStep(step: StartupStep, config?: ExecuteStepConfig) {
13+
const { successSymbol = colors.green('[READY]'), errorSymbol = colors.red('[ERROR]'), env = process.env.NODE_ENV } = config || { }
1414

1515
const {
1616
name = '', isRequired = true, envBlackList, envWhiteList,
File renamed without changes.

src/starter.ts

-38
This file was deleted.

src/startup.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import StepsList, { StartupStep } from './classes/StepsList'
2+
import executeStep, { ExecuteStepConfig } from './executeStep'
3+
import fileUtil from './utils/fileUtil'
4+
import logStartupInfo from './utils/logStartupInfo'
5+
6+
export { StartupStep }
7+
export interface StarterConfig extends ExecuteStepConfig {
8+
}
9+
10+
11+
class ServiceStartup extends StepsList {
12+
async start(config?: StarterConfig) {
13+
await fileUtil.requireRootDefaultSetupFile()
14+
15+
logStartupInfo()
16+
17+
const { steps } = this
18+
19+
for (let i = 0; i < steps.length; i++) {
20+
const step = steps[i]
21+
22+
await executeStep(step, config)
23+
}
24+
}
25+
}
26+
27+
28+
const serviceStartup = new ServiceStartup()
29+
export default serviceStartup

src/stepsList.ts

-24
This file was deleted.

src/utils/fileUtil.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import resolveUtils from '../lib/resolveUtils'
2+
3+
function requireRootDefaultSetupFile() {
4+
const path = resolveUtils.resolvePathFromRoot('service-startup')
5+
try {
6+
require(path)
7+
} catch (e) {
8+
// seems file is not created...
9+
}
10+
}
11+
12+
function getInfoFromRootPackageJson() {
13+
const packageJsonPath = resolveUtils.resolvePathFromRoot('package.json')
14+
try {
15+
const packageJson = require(packageJsonPath)
16+
17+
if(!packageJson) return null
18+
return {
19+
name: packageJson.name,
20+
version: packageJson.version,
21+
}
22+
} catch (e) {
23+
// seems file is not created...
24+
}
25+
26+
return null
27+
}
28+
29+
30+
export default {
31+
requireRootDefaultSetupFile,
32+
getInfoFromRootPackageJson,
33+
}
File renamed without changes.

src/utils/logStartupInfo.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import fileUtil from './fileUtil'
2+
3+
function buildCentralizedMessage (message: string, { txtBefore, txtAfter, length } : {
4+
length: number
5+
txtBefore: string,
6+
txtAfter: string
7+
}) {
8+
let msg = message
9+
while (msg.length < length - (txtBefore.length + txtAfter.length)) {
10+
if(msg.length % 2 === 0) {
11+
msg += ' '
12+
} else {
13+
msg = " " + msg
14+
}
15+
}
16+
17+
return `${txtBefore}${msg}${txtAfter}`
18+
}
19+
20+
function logStartupInfo() {
21+
const info = fileUtil.getInfoFromRootPackageJson()
22+
23+
const date = new Date()
24+
let logList = [
25+
"********************************************************",
26+
"*********** Startup ************",
27+
]
28+
let borderTxt = '***********'
29+
const length = logList[0].length
30+
31+
if(info) {
32+
const infoTxt = buildCentralizedMessage(` ${info.name} (${info.version}) `, {
33+
txtAfter: borderTxt,
34+
txtBefore: borderTxt,
35+
length
36+
})
37+
38+
logList.splice(2, 0, infoTxt)
39+
}
40+
41+
42+
logList.push(buildCentralizedMessage(date.toISOString(), {
43+
txtAfter: borderTxt,
44+
txtBefore: borderTxt,
45+
length
46+
}))
47+
logList.push(logList[0])
48+
49+
console.log(logList.join('\n'))
50+
}
51+
52+
53+
export default logStartupInfo

0 commit comments

Comments
 (0)