forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.ts
93 lines (85 loc) · 2.91 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
/*
* @adonisjs/core
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { join } from 'path'
import * as sinkStatic from '@adonisjs/sink'
import { string } from '@poppinss/utils/build/helpers'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
const ENV_VALIDATIONS_TEMPLATE_STUB = join(__dirname, './templates', 'env.txt')
const STATIC_TEMPLATE_STUB = join(__dirname, './templates', 'config', 'static.txt')
const APP_CONFIG_TEMPLATE_STUB = join(__dirname, './templates', 'config', 'app.txt')
/**
* Configure package
*/
export default async function instructions(
projectRoot: string,
_: ApplicationContract,
{ logger, files }: typeof sinkStatic
) {
const isApiBoilerplate = process.env['ADONIS_CREATE_APP_BOILERPLATE'] === 'api'
const assetsManager = process.env['ADONIS_CREATE_APP_ENCORE'] === 'true'
/**
* Create app config file
*/
const appConfig = new files.MustacheFile(projectRoot, 'config/app.ts', APP_CONFIG_TEMPLATE_STUB)
if (appConfig.exists()) {
logger.action('create').skipped('config/app.ts', 'File already exists')
} else {
appConfig.apply({ forceContentNegotiationToJSON: isApiBoilerplate, assetsManager }).commit()
logger.action('create').succeeded('config/app.ts')
}
/**
* Create static config file when boilerplate is not for the api
*/
if (!isApiBoilerplate) {
const staticConfig = new files.MustacheFile(
projectRoot,
'config/static.ts',
STATIC_TEMPLATE_STUB
)
if (staticConfig.exists()) {
logger.action('create').skipped('config/static.ts', 'File already exists')
} else {
staticConfig.apply({}).commit()
logger.action('create').succeeded('config/static.ts')
}
}
/**
* Add `public` folder to the meta files when not selected api
* boilerplate
*/
if (!isApiBoilerplate) {
const rcFile = new files.AdonisRcFile(projectRoot)
rcFile.addMetaFile('public/**', false)
rcFile.commit()
logger.action(rcFile.exists() ? 'update' : 'create').succeeded('.adonisrc.json')
}
/**
* Create .env file for holding environment variables during
* development
*/
const env = new files.EnvFile(projectRoot)
env.set('PORT', 3333)
env.set('HOST', '0.0.0.0')
env.set('NODE_ENV', 'development')
env.set('APP_KEY', string.generateRandom(32))
env.set('DRIVE_DISK', 'local')
env.commit()
logger.action(env.exists() ? 'update' : 'create').succeeded('.env,.env.example')
/**
* Create env.ts file for performing environment variable validations
*/
const envTsFile = new files.MustacheFile(projectRoot, 'env.ts', ENV_VALIDATIONS_TEMPLATE_STUB)
envTsFile.apply({ assetsManager })
if (envTsFile.exists()) {
logger.action('create').skipped('env.ts')
} else {
envTsFile.apply({}).commit()
logger.action('create').succeeded('env.ts')
}
}