forked from enviodev/hyperindex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
194 lines (177 loc) · 6.76 KB
/
Copy pathmod.rs
File metadata and controls
194 lines (177 loc) · 6.76 KB
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
mod evm_prompts;
mod fuel_prompts;
mod inquire_helpers;
mod shared_prompts;
mod svm_prompts;
pub mod validation;
use super::{
clap_definitions::{self, InitArgs, ProjectPaths},
init_config::{InitConfig, Language},
};
use crate::{
clap_definitions::InitFlow,
constants::project_paths::DEFAULT_PROJECT_ROOT_PATH,
init_config::{evm, Ecosystem},
};
use anyhow::{Context, Result};
use inquire::{Select, Text};
use shared_prompts::prompt_template;
use strum;
use strum::{Display, EnumIter, IntoEnumIterator};
use validation::{
contains_no_whitespace_validator, is_directory_new_validator,
is_valid_foldername_inquire_validator,
};
#[derive(Clone, Debug, Display, PartialEq, EnumIter)]
enum EcosystemOption {
Evm,
Svm,
Fuel,
}
async fn prompt_ecosystem(cli_init_flow: Option<InitFlow>) -> Result<Ecosystem> {
let init_flow = match cli_init_flow {
Some(v) => v,
None => {
let ecosystem_options = EcosystemOption::iter().collect();
let ecosystem_option = Select::new("Choose blockchain ecosystem", ecosystem_options)
.prompt()
.context("Failed prompting for blockchain ecosystem")?;
match ecosystem_option {
EcosystemOption::Fuel => InitFlow::Fuel { init_flow: None },
EcosystemOption::Svm => InitFlow::Svm { init_flow: None },
EcosystemOption::Evm => {
// Start prompt to ask the user which initialization option they want
// Explicitelly build options, since we don't want to include graph migration and other ecosystem selection subcomands
let user_response_options =
clap_definitions::EvmInitFlowInteractive::iter().collect();
Select::new("Choose an initialization option", user_response_options)
.prompt()
.context("Failed prompting for Evm initialization option")?
.into()
}
}
}
};
let initialization = match init_flow {
InitFlow::Fuel {
init_flow: maybe_init_flow,
} => match fuel_prompts::prompt_init_flow_missing(maybe_init_flow)? {
clap_definitions::fuel::InitFlow::Template(args) => Ecosystem::Fuel {
init_flow: fuel_prompts::prompt_template_init_flow(args)?,
},
clap_definitions::fuel::InitFlow::ContractImport(args) => Ecosystem::Fuel {
init_flow: fuel_prompts::prompt_contract_import_init_flow(args).await?,
},
},
InitFlow::Template(args) => {
let chosen_template = match args.template {
Some(template) => template,
None => {
let options = evm::Template::iter().collect();
prompt_template(options)?
}
};
Ecosystem::Evm {
init_flow: evm::InitFlow::Template(chosen_template),
}
}
InitFlow::SubgraphMigration(args) => {
let input_subgraph_id = match args.subgraph_id {
Some(id) => id,
None => Text::new("[BETA VERSION] What is the subgraph ID?")
.prompt()
.context("Prompting user for subgraph id")?,
};
Ecosystem::Evm {
init_flow: evm::InitFlow::SubgraphID(input_subgraph_id),
}
}
InitFlow::ContractImport(args) => Ecosystem::Evm {
init_flow: evm_prompts::prompt_contract_import_init_flow(args).await?,
},
InitFlow::Svm {
init_flow: maybe_init_flow,
} => match svm_prompts::prompt_init_flow_missing(maybe_init_flow)? {
clap_definitions::svm::InitFlow::Template(args) => Ecosystem::Svm {
init_flow: svm_prompts::prompt_template_init_flow(args)?,
},
},
};
Ok(initialization)
}
#[derive(Debug, Clone, strum::Display, strum::EnumIter, strum::EnumString)]
enum ApiTokenInput {
#[strum(serialize = "Create a new API token (Opens https://envio.dev/app/api-tokens)")]
Create,
#[strum(serialize = "Add an existing API token")]
AddExisting,
}
pub async fn prompt_missing_init_args(
init_args: InitArgs,
project_paths: &ProjectPaths,
) -> Result<InitConfig> {
let directory: String = match &project_paths.directory {
Some(args_directory) => args_directory.clone(),
None => {
Text::new("Specify a folder name (ENTER to skip): ")
.with_default(DEFAULT_PROJECT_ROOT_PATH)
// validate string is valid directory name
.with_validator(is_valid_foldername_inquire_validator)
// validate the directory doesn't already exist
.with_validator(is_directory_new_validator)
.with_validator(contains_no_whitespace_validator)
.prompt()?
}
};
let name: String = match init_args.name {
Some(args_name) => args_name,
None => {
if directory == DEFAULT_PROJECT_ROOT_PATH {
"envio-indexer".to_string()
} else {
directory.to_string()
}
}
};
let language = match init_args.language {
Some(args_language) => args_language,
None => Language::TypeScript,
};
let ecosystem = prompt_ecosystem(init_args.init_commands)
.await
.context("Failed getting template")?;
let api_token: Option<String> = match init_args.api_token {
Some(k) => Ok::<_, anyhow::Error>(Some(k)),
None if ecosystem.uses_hypersync() => {
let select = Select::new(
"Add an API token for HyperSync to your .env file?",
ApiTokenInput::iter().collect(),
)
.prompt()
.context("Prompting for add API token")?;
let token_prompt = Text::new("Add your API token: ")
.with_help_message("See tokens at: https://envio.dev/app/api-tokens");
match select {
ApiTokenInput::Create => {
open::that_detached("https://envio.dev/app/api-tokens")?;
Ok(token_prompt
.prompt_skippable()
.context("Prompting for create token")?)
}
ApiTokenInput::AddExisting => Ok(token_prompt
.prompt_skippable()
.context("Prompting for add existing token")?),
}
}
None => Ok(None),
}
.context("Prompting for API Token")?;
Ok(InitConfig {
name,
directory,
ecosystem,
language,
api_token,
package_manager: init_args.package_manager,
})
}