instaserve 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/instaserve +14 -6
- package/package.json +1 -1
package/instaserve
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import chalk from 'chalk'
|
|
4
4
|
import fs from 'node:fs'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { pathToFileURL } from 'node:url'
|
|
5
7
|
|
|
6
8
|
console.log(chalk.cyan('\nInstaserve - Instant Web Stack\n'))
|
|
7
9
|
console.log(chalk.yellow('Usage:'))
|
|
@@ -85,32 +87,38 @@ for (let i = 0; i < args.length; i++) {
|
|
|
85
87
|
|
|
86
88
|
// Load routes file
|
|
87
89
|
let routes = {}
|
|
88
|
-
const
|
|
90
|
+
const routesFileParam = params.api || './routes.js'
|
|
89
91
|
const routesFileSpecified = !!params.api
|
|
90
92
|
|
|
93
|
+
// Resolve to absolute path from current working directory
|
|
94
|
+
const routesFile = path.isAbsolute(routesFileParam)
|
|
95
|
+
? routesFileParam
|
|
96
|
+
: path.resolve(process.cwd(), routesFileParam)
|
|
97
|
+
|
|
91
98
|
if (routesFileSpecified && !fs.existsSync(routesFile)) {
|
|
92
|
-
console.error(chalk.red(`Error: Routes file "${
|
|
99
|
+
console.error(chalk.red(`Error: Routes file "${routesFileParam}" does not exist`))
|
|
93
100
|
process.exit(1)
|
|
94
101
|
}
|
|
95
102
|
|
|
96
103
|
if (fs.existsSync(routesFile)) {
|
|
97
104
|
try {
|
|
98
|
-
const
|
|
105
|
+
const routesFileURL = pathToFileURL(routesFile).href
|
|
106
|
+
const imported = await import(routesFileURL)
|
|
99
107
|
routes = imported.default || imported
|
|
100
108
|
|
|
101
109
|
if (!routes || typeof routes !== 'object' || Array.isArray(routes)) {
|
|
102
|
-
console.error(chalk.red(`Error: Routes file "${
|
|
110
|
+
console.error(chalk.red(`Error: Routes file "${routesFileParam}" must export a default object`))
|
|
103
111
|
process.exit(1)
|
|
104
112
|
}
|
|
105
113
|
|
|
106
114
|
for (const [key, handler] of Object.entries(routes)) {
|
|
107
115
|
if (typeof handler !== 'function') {
|
|
108
|
-
console.error(chalk.red(`Error: Route "${key}" in "${
|
|
116
|
+
console.error(chalk.red(`Error: Route "${key}" in "${routesFileParam}" must be a function`))
|
|
109
117
|
process.exit(1)
|
|
110
118
|
}
|
|
111
119
|
}
|
|
112
120
|
} catch (e) {
|
|
113
|
-
console.error(chalk.red(`Error: Could not load routes file "${
|
|
121
|
+
console.error(chalk.red(`Error: Could not load routes file "${routesFileParam}": ${e.message}`))
|
|
114
122
|
process.exit(1)
|
|
115
123
|
}
|
|
116
124
|
}
|