-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.ts
113 lines (90 loc) · 2.96 KB
/
instructions.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { join } from 'path'
import * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
function getStub(...relativePaths: string[]) {
return join(__dirname, 'templates', ...relativePaths)
}
/**
* Instructions to be executed when setting up the package.
*/
type InstructionsState = {
basePathName: string
}
/**
* Instructions to be executed when setting up the package.
*/
export default async function instructions(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic
) {
const instructionsState: InstructionsState = {
basePathName: '/api/trpc',
}
const providerLucidExist = app.rcFile.aceProviders.find(
(provider) => provider !== '@adonisjs/lucid'
)
if (!providerLucidExist) {
sink.logger.error('You need to install @adonisjs/lucid to use this package')
return
}
const providerAuthExist = app.rcFile.aceProviders.find(
(provider) => provider !== '@adonisjs/auth'
)
if (!providerAuthExist) {
sink.logger.error('You need to install @adonisjs/auth to use this package')
return
}
instructionsState.basePathName = await sink
.getPrompt()
.ask('How do you want it to be called the trpc route?', {
hint: '/api/trpc',
format: (value: string) => {
if (value[0] !== '/') {
return `/${value}`
}
return value
},
result: (value: string) => {
if (!value.length) {
return '/api/trpc'
}
return value[0] === '/' ? value : `/${value}`
},
})
const preloadFilePath = app.makePath('start/trpc.ts')
const preloadFile = new sink.files.MustacheFile(
projectRoot,
preloadFilePath,
getStub('start.txt')
)
if (preloadFile.exists()) {
sink.logger.action('create').skipped(`${preloadFilePath} file already exists`)
} else {
preloadFile.overwrite = true
preloadFile.apply(instructionsState).commit()
sink.logger.action('create').succeeded('start/trpc.ts')
const preload = new sink.files.AdonisRcFile(projectRoot)
preload.setPreload('./start/trpc')
preload.commit()
sink.logger.action('update').succeeded('.adonisrc.json')
}
const configPath = app.configPath('trpc.ts')
const configFile = new sink.files.MustacheFile(projectRoot, configPath, getStub('config.txt'))
if (configFile.exists()) {
sink.logger.action('create').skipped(`${configPath} file already exists`)
} else {
configFile.overwrite = true
configFile.apply(instructionsState).commit()
sink.logger.action('create').succeeded('config/trpc.ts')
}
const routesDir = app.makePath('app/Routes/index.ts')
const routesFile = new sink.files.MustacheFile(projectRoot, routesDir, getStub('router.txt'))
if (routesFile.exists()) {
sink.logger.action('create').skipped(`${routesDir} file already exists`)
} else {
routesFile.overwrite = true
routesFile.apply().commit()
sink.logger.action('create').succeeded('app/Routes/index.ts')
}
}