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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::collections::HashMap;

use serde::Serialize;

pub const TEST_ENV_NAME: &str = "test";

// small suite of flexible toml structs
// the  idea here is to focus on "when this config key is set"
// rather than needing to write tomls all the time.
// these structs set every value as an `Option`. To use,
// initialize a new WranglerToml::default() and begin setting
// values on it.
#[derive(Clone, Debug, Default, Serialize)]
pub struct KvConfig {
    pub binding: Option<&'static str>,
    pub id: Option<&'static str>,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct Triggers {
    pub crons: Option<Vec<String>>,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct SiteConfig {
    pub bucket: Option<&'static str>,
    #[serde(rename = "entry-point")]
    pub entry_point: Option<&'static str>,
    pub include: Option<Vec<&'static str>>,
    pub exclude: Option<Vec<&'static str>>,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct EnvConfig {
    pub name: Option<&'static str>,
    pub account_id: Option<&'static str>,
    pub workers_dev: Option<bool>,
    pub route: Option<&'static str>,
    pub routes: Option<Vec<&'static str>>,
    pub zone_id: Option<&'static str>,
    pub webpack_config: Option<&'static str>,
    pub private: Option<bool>,
    pub site: Option<SiteConfig>,
    #[serde(alias = "kv-namespaces")]
    pub kv_namespaces: Option<Vec<KvConfig>>,
    pub vars: Option<HashMap<&'static str, &'static str>>,
    pub triggers: Option<Triggers>,
}

impl EnvConfig {
    pub fn custom_script_name(name: &'static str) -> EnvConfig {
        let mut env_config = EnvConfig::default();
        env_config.name = Some(name);

        env_config
    }

    pub fn zoneless(workers_dev: bool) -> EnvConfig {
        let mut env_config = EnvConfig::default();
        env_config.workers_dev = Some(workers_dev);

        env_config
    }

    pub fn zoneless_with_account_id(workers_dev: bool, account_id: &'static str) -> EnvConfig {
        let mut env_config = EnvConfig::default();
        env_config.account_id = Some(account_id);
        env_config.workers_dev = Some(workers_dev);

        env_config
    }

    pub fn zoned_single_route(zone_id: &'static str, route: &'static str) -> EnvConfig {
        let mut env_config = EnvConfig::default();
        env_config.zone_id = Some(zone_id);
        env_config.route = Some(route);

        env_config
    }

    pub fn zoned_multi_route(zone_id: &'static str, routes: Vec<&'static str>) -> EnvConfig {
        let mut env_config = EnvConfig::default();
        env_config.zone_id = Some(zone_id);
        env_config.routes = Some(routes);

        env_config
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct WranglerToml {
    pub name: Option<&'static str>,
    #[serde(rename = "type")]
    pub target_type: Option<&'static str>,
    pub account_id: Option<&'static str>,
    pub workers_dev: Option<bool>,
    pub route: Option<&'static str>,
    pub routes: Option<Vec<&'static str>>,
    pub zone_id: Option<&'static str>,
    pub webpack_config: Option<&'static str>,
    pub private: Option<bool>,
    pub env: Option<HashMap<&'static str, EnvConfig>>,
    #[serde(alias = "kv-namespaces")]
    pub kv_namespaces: Option<Vec<KvConfig>>,
    pub site: Option<SiteConfig>,
    pub vars: Option<HashMap<&'static str, &'static str>>,
    pub triggers: Option<Triggers>,
}

impl WranglerToml {
    // base build configs
    pub fn webpack(name: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::default();
        wrangler_toml.name = Some(name);
        wrangler_toml.target_type = Some("webpack");

        wrangler_toml
    }

    pub fn zoneless(
        name: &'static str,
        account_id: &'static str,
        workers_dev: bool,
    ) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack(name);
        wrangler_toml.workers_dev = Some(workers_dev);
        wrangler_toml.account_id = Some(account_id);

        wrangler_toml
    }

    pub fn zoned_single_route(
        name: &'static str,
        zone_id: &'static str,
        route: &'static str,
    ) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack(name);
        wrangler_toml.zone_id = Some(zone_id);
        wrangler_toml.route = Some(route);

        eprintln!("{:#?}", &wrangler_toml);
        wrangler_toml
    }

    pub fn zoned_multi_route(
        name: &'static str,
        zone_id: &'static str,
        routes: Vec<&'static str>,
    ) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack(name);
        wrangler_toml.zone_id = Some(zone_id);
        wrangler_toml.routes = Some(routes);

        eprintln!("{:#?}", &wrangler_toml);
        wrangler_toml
    }

    pub fn with_env(name: &'static str, env_config: EnvConfig) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack(name);
        wrangler_toml.env = Some(test_env(env_config));

        eprintln!("{:#?}", &wrangler_toml);
        wrangler_toml
    }

    pub fn zoneless_with_env(
        name: &'static str,
        account_id: &'static str,
        workers_dev: bool,
        env_config: EnvConfig,
    ) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::zoneless(name, account_id, workers_dev);
        wrangler_toml.env = Some(test_env(env_config));

        eprintln!("{:#?}", &wrangler_toml);
        wrangler_toml
    }

    pub fn zoned_single_route_with_env(
        name: &'static str,
        zone_id: &'static str,
        route: &'static str,
        env_config: EnvConfig,
    ) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::zoned_single_route(name, zone_id, route);
        wrangler_toml.env = Some(test_env(env_config));

        eprintln!("{:#?}", &wrangler_toml);
        wrangler_toml
    }

    pub fn webpack_build(name: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::default();
        wrangler_toml.name = Some(name);
        wrangler_toml.workers_dev = Some(true);
        wrangler_toml.target_type = Some("webpack");

        wrangler_toml
    }

    pub fn webpack_std_config(name: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack_build(name);
        wrangler_toml.webpack_config = Some("webpack.config.js");

        wrangler_toml
    }

    pub fn webpack_custom_config(name: &'static str, webpack_config: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack_build(name);
        wrangler_toml.webpack_config = Some(webpack_config);

        wrangler_toml
    }

    pub fn rust(name: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::default();
        wrangler_toml.name = Some(name);
        wrangler_toml.workers_dev = Some(true);
        wrangler_toml.target_type = Some("rust");

        wrangler_toml
    }

    pub fn javascript(name: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::default();
        wrangler_toml.name = Some(name);
        wrangler_toml.workers_dev = Some(true);
        wrangler_toml.target_type = Some("javascript");

        wrangler_toml
    }

    pub fn site(name: &'static str) -> WranglerToml {
        let mut wrangler_toml = WranglerToml::webpack_build(name);
        let mut site = SiteConfig::default();
        site.bucket = Some("./public");
        wrangler_toml.site = Some(site);

        wrangler_toml
    }
}

fn test_env(env_config: EnvConfig) -> HashMap<&'static str, EnvConfig> {
    let mut env = HashMap::new();
    env.insert(TEST_ENV_NAME, env_config);

    env
}