-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
load.ts
43 lines (38 loc) · 1.24 KB
/
load.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import ora from 'ora'
import { exec } from './core'
import { Context } from './types'
/**
* Load template config.
* @todo
* - Adapt to any repository?
* - Automatic install template dependencies.
* - Template dependencies not found.
* - Check template is available.
*/
export default async (ctx: Context): Promise<void> => {
// default template name
ctx.config.name = ctx.template
// Automatic install template dependencies.
const spinner = ora('Installing template dependencies...').start()
try {
/* c8 ignore next */
const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
await exec(cmd, ['install', '--production'], { cwd: ctx.src })
spinner.succeed('Installing template dependencies complete.')
} catch {
spinner.fail('Install template dependencies failed.')
}
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = require(ctx.src)
if (Object.prototype.toString.call(mod) !== '[object Object]') {
throw new TypeError('template needs to expose an object.')
}
Object.assign(ctx.config, mod)
} catch (err) {
const e = err as NodeJS.ErrnoException
if (e.code === 'MODULE_NOT_FOUND') return
e.message = `Invalid template: ${e.message}`
throw e
}
}