forked from StouderIO/adonis-auditing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.ts
52 lines (45 loc) · 2 KB
/
configure.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
import ConfigureCommand from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
import { readFile, writeFile } from 'node:fs/promises'
import { parseTsconfig } from 'get-tsconfig'
export async function configure(command: ConfigureCommand) {
const codemods = await command.createCodemods()
// create default migration
await codemods.makeUsingStub(stubsRoot, 'migration.stub', { time: Date.now() })
// create default config file
await codemods.makeUsingStub(stubsRoot, 'config.stub', {})
// add provider and directory to rc file
await codemods.updateRcFile((transformer) => {
transformer.addProvider('@stouder-io/adonis-auditing/auditing_provider')
transformer.setDirectory('audit_resolvers', 'app/audit_resolvers')
})
// add default resolvers
const path = command.app.makePath('app', 'audit_resolvers')
await codemods.makeUsingStub(stubsRoot, 'resolvers/ip_address_resolver.stub', { path })
await codemods.makeUsingStub(stubsRoot, 'resolvers/user_agent_resolver.stub', { path })
await codemods.makeUsingStub(stubsRoot, 'resolvers/url_resolver.stub', { path })
await codemods.makeUsingStub(stubsRoot, 'resolvers/user_resolver.stub', { path })
// add imports
const packageJsonPath = command.app.makePath('package.json')
const packageJson = await readFile(packageJsonPath, 'utf-8').then(JSON.parse)
packageJson.imports = {
...packageJson.imports,
'#audit_resolvers/*': './app/audit_resolvers/*.js',
}
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2), {
encoding: 'utf-8',
})
// add tsconfig paths
const tsConfigJsonPath = command.app.makePath('tsconfig.json')
const tsConfigJson = parseTsconfig(tsConfigJsonPath)
tsConfigJson.compilerOptions = {
...tsConfigJson.compilerOptions,
paths: {
...tsConfigJson.compilerOptions?.paths,
'#audit_resolvers/*': ['./app/audit_resolvers/*.js'],
},
}
await writeFile(tsConfigJsonPath, JSON.stringify(tsConfigJson, null, 2), {
encoding: 'utf-8',
})
}