-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.ts
137 lines (119 loc) · 2.92 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* create-adonis-ts-app
*
* (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 getops from 'getopts'
import { removeSync } from 'fs-extra'
import { Application } from '@adonisjs/application'
import { utils, logger, tasksUi } from '@adonisjs/sink'
import { tasks } from './tasks'
import { greet } from './src/Chalk/greet'
import { showArt } from './src/Chalk/art'
import { getHelp } from './src/Chalk/help'
import { getState } from './src/Helpers'
/**
* Running all the tasks to create a new project.
*/
export async function runTasks(args: string[]) {
showArt()
/**
* Setup command line arguments
*/
const argv = getops(args, {
string: ['boilerplate', 'name'],
boolean: ['eslint', 'debug', 'prettier', 'encore'],
default: {
eslint: null,
debug: false,
prettier: null,
encore: null,
},
})
/**
* Show help when no arguments are passed
*/
if (!argv._.length) {
console.log(getHelp(utils.getPackageManager(process.cwd())))
return
}
/**
* First argument is the project path
*/
const projectPath = argv._[0].trim()
console.log('')
console.log(logger.colors.green('CUSTOMIZE PROJECT'))
/**
* Setup state
*/
const state = await getState(projectPath, {
client: utils.getPackageManager(projectPath),
projectName: argv.name,
debug: argv.debug,
boilerplate: argv.boilerplate,
eslint: argv.eslint,
prettier: argv.prettier,
encore: argv.encore,
})
/**
* Return when directory is not empty
*/
if (!utils.isEmptyDir(state.absPath)) {
const errors = [
`Cannot overwrite contents of {${projectPath}} directory.`,
'Make sure to define path to an empty directory',
]
console.log('')
logger.error(errors.join(' '))
return
}
/**
* Setup application
*/
const application = new Application(state.absPath, 'console', {
typescript: true,
})
/**
* Decide the ui renderer to use
*/
const tasksManager = state.debug ? tasksUi.verbose() : tasksUi()
/**
* Execute all tasks
*/
tasks(state).forEach(({ title, actions }) => {
tasksManager.add(title, async (taskLogger, task) => {
for (let action of actions) {
await action(application, taskLogger, state)
}
await task.complete()
})
})
console.log('')
console.log(logger.colors.green('RUNNING TASKS'))
/**
* Run tasks
*/
try {
await tasksManager.run()
} catch (error) {
tasksManager.state = 'failed'
tasksManager.error = error
}
console.log('')
/**
* Notify about failure
*/
if (tasksManager.state === 'failed') {
logger.error('Unable to create project. Cleaning up')
removeSync(state.absPath)
return
}
/**
* Greet the user to get started
*/
logger.success('Project created successfully')
greet(state)
}