Skip to content

Commit ecdc54c

Browse files
committedNov 16, 2022
add service-startup file, which will be loaded by default
1 parent 3f46ed4 commit ecdc54c

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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 `startServices.ts`
15+
You can create file for your services like `service-startup.ts`
1616
```typescript
1717
import colors from 'colors'
1818
import starter from 'service-startup'

‎src/starter.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import stepsList, { StarterStep } from './stepsList'
22
import executeStep, { ExecuteStepConfig } from './executeStep'
3+
import resolveUtils from './utils/resolveUtils'
34

45
export interface StarterConfig extends ExecuteStepConfig {
56
}
67

78
async function starter(steps: StarterStep[], config?: StarterConfig) {
9+
const path = resolveUtils.resolvePathFromRoot('service-startup')
10+
try {
11+
require(path)
12+
} catch (e) {
13+
// seems file is not created...
14+
}
15+
816
const date = new Date()
917
console.log(`
1018
********************************************************

‎src/utils/resolveUtils.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from 'path'
2+
3+
function resolvePathFromRoot(filePath: string): string {
4+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5+
// @ts-ignore not sure issue can happen. Waiting for crash first.
6+
let rootPath = process.env.PWD as string // require.main?.path
7+
return path.resolve(rootPath, filePath)
8+
}
9+
10+
11+
function resolveMigration(fileName: string) {
12+
const filePath = `./${getMigrationPath(fileName)}`
13+
14+
const pathAbsolute = resolvePathFromRoot(filePath)
15+
16+
return importFile(pathAbsolute)
17+
}
18+
19+
function importFile(filePath:string) {
20+
// eslint-disable-next-line
21+
const fnMigration = require(filePath)
22+
23+
return fnMigration?.default || fnMigration
24+
}
25+
26+
function requireFromRoot(filePath: string) {
27+
filePath = resolvePathFromRoot(filePath)
28+
return importFile(filePath)
29+
}
30+
31+
export default {
32+
resolvePathFromRoot,
33+
resolveMigration,
34+
importFile,
35+
requireFromRoot,
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.