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;

/// Deploys the user's app to the `pkg/` directory (can be changed with `-o/--output`). This will build everything for release and then
/// put it all together in one folder that can be conveniently uploaded to a server, file host, etc. This can return any kind of error
/// because deploying involves working with other subcommands.
pub fn deploy(dir: PathBuf, opts: DeployOpts) -> Result<i32, Error> {
    // Fork at whether we're using static exporting or not
    let exit_code = if opts.export_static {
        deploy_export(dir, opts.output)?
    } else {
        deploy_full(dir, opts.output, opts.integration)?
    };

    Ok(exit_code)
}

/// Deploys the user's app in its entirety, with a bundled server. This can return any kind of error because deploying involves working
/// with other subcommands.
fn deploy_full(dir: PathBuf, output: String, integration: Integration) -> Result<i32, Error> {
    // Build everything for production, not running the server
    let (serve_exit_code, server_path) = serve(
        dir.clone(),
        ServeOpts {
            no_run: true,
            no_build: false,
            release: true,
            standalone: true,
            integration,
            watch: false,
            // These have no impact if `no_run` is `true` (which it is), so we can use the defaults here
            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 {
        // Delete the output directory if it exists and recreate it
        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());
        }
        // Copy in the server executable
        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());
        }
        // Copy in the `index.html` file
        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());
        }
        // Copy in the `static/` directory if it exists
        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());
            }
        }
        // Copy in the `translations` directory if it exists
        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());
            }
        }
        // Copy in the entire `.perseus/dist` directory (it must exist)
        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 {
        // If we don't have the executable, throw an error
        Err(DeployError::GetServerExecutableFailed.into())
    }
}

/// Uses static exporting to deploy the user's app. This can return any kind of error because deploying involves working with other
/// subcommands.
fn deploy_export(dir: PathBuf, output: String) -> Result<i32, Error> {
    // Export the app to `.perseus/exported`, using release mode
    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);
    }
    // That subcommand produces a self-contained static site at `.perseus/dist/exported/`
    // Just copy that out to the output directory
    let from = dir.join(".perseus/dist/exported");
    let output_path = PathBuf::from(&output);
    // Delete the output directory if it exists and recreate it
    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());
    }
    // Now read the contents of the export directory so that we can copy each asset in individually
    // That avoids a `pkg/exported/` situation
    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())
        }
    };
    // Now run the copy of each item
    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)
}