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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::errors::*;
use crate::export;
use crate::parse::Integration;
use crate::parse::{DeployOpts, ExportOpts, ServeOpts};
use crate::serve;
use fs_extra::copy_items;
use fs_extra::dir::{copy as copy_dir, CopyOptions};
use std::fs;
use std::path::PathBuf;
pub fn deploy(dir: PathBuf, opts: DeployOpts) -> Result<i32, Error> {
let exit_code = if opts.export_static {
deploy_export(dir, opts.output)?
} else {
deploy_full(dir, opts.output, opts.integration)?
};
Ok(exit_code)
}
fn deploy_full(dir: PathBuf, output: String, integration: Integration) -> Result<i32, Error> {
let (serve_exit_code, server_path) = serve(
dir.clone(),
ServeOpts {
no_run: true,
no_build: false,
release: true,
standalone: true,
integration,
watch: false,
host: "127.0.0.1".to_string(),
port: 8080,
},
)?;
if serve_exit_code != 0 {
return Ok(serve_exit_code);
}
if let Some(server_path) = server_path {
let output_path = PathBuf::from(&output);
if output_path.exists() {
if let Err(err) = fs::remove_dir_all(&output_path) {
return Err(DeployError::ReplaceOutputDirFailed {
path: output,
source: err,
}
.into());
}
}
if let Err(err) = fs::create_dir(&output_path) {
return Err(DeployError::ReplaceOutputDirFailed {
path: output,
source: err,
}
.into());
}
let to = output_path.join("server");
if let Err(err) = fs::copy(&server_path, &to) {
return Err(DeployError::MoveAssetFailed {
to: to.to_str().map(|s| s.to_string()).unwrap(),
from: server_path,
source: err,
}
.into());
}
let from = dir.join("index.html");
let to = output_path.join("index.html");
if let Err(err) = fs::copy(&from, &to) {
return Err(DeployError::MoveAssetFailed {
to: to.to_str().map(|s| s.to_string()).unwrap(),
from: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into());
}
let from = dir.join("static");
if from.exists() {
if let Err(err) = copy_dir(&from, &output, &CopyOptions::new()) {
return Err(DeployError::MoveDirFailed {
to: output,
from: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into());
}
}
let from = dir.join("translations");
if from.exists() {
if let Err(err) = copy_dir(&from, &output, &CopyOptions::new()) {
return Err(DeployError::MoveDirFailed {
to: output,
from: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into());
}
}
let from = dir.join(".perseus/dist");
if let Err(err) = copy_dir(&from, &output, &CopyOptions::new()) {
return Err(DeployError::MoveDirFailed {
to: output,
from: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into());
}
println!();
println!("Deployment complete 🚀! Your app is now available for serving in the standalone folder '{}'! You can run it by executing the `server` binary in that folder.", &output_path.to_str().map(|s| s.to_string()).unwrap());
Ok(0)
} else {
Err(DeployError::GetServerExecutableFailed.into())
}
}
fn deploy_export(dir: PathBuf, output: String) -> Result<i32, Error> {
let export_exit_code = export(
dir.clone(),
ExportOpts {
release: true,
serve: false,
host: String::new(),
port: 0,
watch: false,
},
)?;
if export_exit_code != 0 {
return Ok(export_exit_code);
}
let from = dir.join(".perseus/dist/exported");
let output_path = PathBuf::from(&output);
if output_path.exists() {
if let Err(err) = fs::remove_dir_all(&output_path) {
return Err(DeployError::ReplaceOutputDirFailed {
path: output,
source: err,
}
.into());
}
}
if let Err(err) = fs::create_dir(&output_path) {
return Err(DeployError::ReplaceOutputDirFailed {
path: output,
source: err,
}
.into());
}
let items = fs::read_dir(&from);
let items: Vec<PathBuf> = match items {
Ok(items) => {
let mut ok_items = Vec::new();
for item in items {
match item {
Ok(item) => ok_items.push(item.path()),
Err(err) => {
return Err(DeployError::ReadExportDirFailed {
path: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into())
}
}
}
ok_items
}
Err(err) => {
return Err(DeployError::ReadExportDirFailed {
path: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into())
}
};
if let Err(err) = copy_items(&items, &output, &CopyOptions::new()) {
return Err(DeployError::MoveExportDirFailed {
to: output,
from: from.to_str().map(|s| s.to_string()).unwrap(),
source: err,
}
.into());
}
println!();
println!("Deployment complete 🚀! Your app is now available for serving in the standalone folder '{}'! You can run it by serving the contents of that folder however you'd like.", &output_path.to_str().map(|s| s.to_string()).unwrap());
Ok(0)
}