( workingDir: string, args: InitInputs, logger: Logger, prompt: Prompt | null )
| 94 | } |
| 95 | |
| 96 | export async function initAdapter( |
| 97 | workingDir: string, |
| 98 | args: InitInputs, |
| 99 | logger: Logger, |
| 100 | prompt: Prompt | null |
| 101 | ): Promise<InitOutputs> { |
| 102 | const location = resolveToAbsolutePath(path.resolve(workingDir, args.location ?? '')); |
| 103 | |
| 104 | // TODO if the location includes the name, we can dedupe it |
| 105 | if (fs.existsSync(path.join(location, `${args.name}`))) { |
| 106 | throw new Error(`Directory ${args.name} exists, try another project name`); |
| 107 | } |
| 108 | |
| 109 | const networkTemplates = await fetchNetworks(); |
| 110 | |
| 111 | let network = args.network; |
| 112 | let family = args.networkFamily; |
| 113 | if (!network) { |
| 114 | if (!prompt) { |
| 115 | throw new Error('Please provide a network'); |
| 116 | } |
| 117 | const networkStrArr = networkTemplates.flatMap((families) => { |
| 118 | if (family && families.name.toLowerCase() !== family.toLowerCase()) return []; |
| 119 | return families.networks.map((network) => `${network.name} ${chalk.gray(`(${families.name})`)}`); |
| 120 | }); |
| 121 | |
| 122 | const networkPrompt = await prompt({ |
| 123 | message: 'Select a network', |
| 124 | type: 'string', |
| 125 | options: networkStrArr, |
| 126 | }); |
| 127 | |
| 128 | const [rawNetwork, rawFamily] = stripVTControlCharacters(networkPrompt).split(' ('); |
| 129 | network = rawNetwork.trim(); |
| 130 | family ??= rawFamily ? rawFamily.replace(')', '').trim() : undefined; |
| 131 | } |
| 132 | |
| 133 | const [selectedFamily, selectedNetwork] = findNetwork(networkTemplates, network, family); |
| 134 | |
| 135 | const candidateProjects = await fetchExampleProjects(selectedFamily.code, selectedNetwork.code); |
| 136 | |
| 137 | const joiner = ` - `; |
| 138 | const candidateOptions = candidateProjects.map( |
| 139 | (project) => `${project.name}${joiner}${chalk.gray(project.description)}` |
| 140 | ); |
| 141 | |
| 142 | // Only push other when prompts are available because it requires a user to input a URL |
| 143 | if (prompt) { |
| 144 | candidateOptions.push(`Other${joiner}${chalk.gray('Enter a git repo URL')}`); |
| 145 | } |
| 146 | |
| 147 | const template = prompt |
| 148 | ? await prompt({ |
| 149 | message: 'Select a template project', |
| 150 | type: 'string', |
| 151 | options: candidateOptions, |
| 152 | }) |
| 153 | : candidateOptions[0]; |
no test coverage detected