-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewCommand.js
174 lines (137 loc) · 5.42 KB
/
newCommand.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const Command = require('@ostro/console/command')
const child_process = require('child_process')
var readline = require('readline')
const fs = require('fs')
const os = require('os')
class NewCommand extends Command {
$signature = 'new';
$description = 'Create a new OstroJS application';
$options = [
this.createOption('--dev', 'Installs the latest "development" release'),
this.createOption('--force', 'Forces install even if the directory already exists').default(false)
]
$arguments = [
this.createArgument('name', 'The name of the application').required()
]
handle() {
this.output.writeln(`<fg=cyan>
___
/ \\
/ \\_____ \_____ _______\_\__
| / ___\| \| | \'__/ _ \\
| \\___ \\ | | | \| (_) \|
\\ /____\/ | | | \\ ___ \/
\\ ___ /
</>`);
let $name = this.input.getArgument('name');
let $directory = $name !== '.' ? process.cwd() + path.sep + $name : '.';
let $version = this.getVersion(this.input);
if (!this.input.getOption('force')) {
this.verifyApplicationDoesntExist($directory);
}
if (this.input.getOption('force') && $directory === '.') {
throw new RuntimeException('Cannot use --force option when using current directory for installation!');
}
let $currentVersion = this.runCommands(['npm show @ostro/installer version']).trim()
let $existingVersion = this.runCommands([`npm ls @ostro/installer -g version --depth=0`]);
$existingVersion = ($existingVersion || "").replace(/[^@]*@ostro\/installer@([\d.]+)\s*/, '$1').trim();
this.output.write('[1/7] ');
let versionMessage = $existingVersion == $currentVersion ? '@ostro/installer version verified' : `Update require "npm install @ostro/installer@latest -g"`
if ($existingVersion != $currentVersion) {
this.error(versionMessage)
} else {
this.info(versionMessage)
}
let osType = os.type()
if ($directory != '.' && this.input.getOption('force')) {
try {
fs.rmdirSync($directory, { force: true, recursive: true })
} catch (e) {
this.error(e)
}
}
fs.mkdirSync($directory)
this.output.write('[2/7] ')
this.info('Directory created')
let $url = this.runCommands(['npm v @ostro/ostro dist.tarball'])
let $commands = [
`cd ${$directory}`,
`curl -s "${$url.trim()}" | tar -xzf - --strip 1`,
];
if (osType != 'Windows_NT') {
$commands.push(`chmod 755 "${$directory}/assistant"`);
}
this.runCommands($commands)
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0, null)
this.output.write('[3/7] ')
this.info('Application crafted')
fs.copyFileSync($directory + path.sep + '.env.example', $directory + path.sep + '.env')
this.output.write('[4/7] ')
this.info('Environment file generated')
this.runCommands([`cd ${$directory} && npm install`])
this.output.write('[5/7] ')
this.info('Dependencies installed')
this.runCommands([`cd ${$directory} && node assistant key:generate`])
this.output.write('[6/7] ')
this.info('Key generated')
if ($name !== '.') {
this.replaceInFile(
'APP_URL=http://localhost',
'APP_URL=http://' + $name + '.test',
$directory + path.sep + '.env'
);
this.replaceInFile(
'DB_DATABASE=ostro',
'DB_DATABASE=' + $name.toLowerCase().replace('-', '_'),
$directory + path.sep + '.env'
);
this.replaceInFile(
'DB_DATABASE=ostro',
'DB_DATABASE=' + $name.toLowerCase().replace('-', '_'),
$directory + path.sep + '.env.example'
);
}
this.output.write('[7/7] ')
this.info('Application setup compleated')
process.stdout.write('\n\n')
this.output.writeln('<comment>Application ready! Build something amazing.</comment>');
process.stdout.write('\n')
this.output.writeln('<fg=cyan>cd ' + $name + '</fg>');
this.output.writeln('<fg=cyan>node app.js</fg>');
}
verifyApplicationDoesntExist($directory) {
try {
let stat = fs.statSync($directory)
if ((stat.isDirectory() || fs.isFile()) && $directory != process.cwd()) {
this.error('Application already exists!')
process.exit(1)
}
} catch (e) {
}
}
runCommands($commands, $env = {}) {
try {
return (child_process.execSync($commands.join(' && '), { encoding: 'utf8' }));
} catch (e) { }
}
getVersion($input) {
if ($input.getOption('dev')) {
return 'dev-master';
}
return '';
}
defaultBranch() {
return 'develop'
}
replaceInFile($search, $replace, $file) {
try {
fs.writeFileSync(
$file,
fs.readFileSync($file, 'utf8').replace($search, $replace)
);
} catch (e) {
}
}
}
module.exports = NewCommand