* Loads the string data of an input that should be exported. * @param name - The name of the input to load. * @param lang - The language to load the input in. * @returns
( name: string, lang?: string )
| 199 | * @returns |
| 200 | */ |
| 201 | async function requireInputCode( |
| 202 | name: string, |
| 203 | lang?: string |
| 204 | ): Promise<string> | never { |
| 205 | lang = !lang ? guessLang() : lang |
| 206 | const localFile = resolve( |
| 207 | __dirname, |
| 208 | `../../inputs/dist/exports/${name}.${lang}` |
| 209 | ) |
| 210 | let fileData = null |
| 211 | if (existsSync(localFile)) { |
| 212 | fileData = await readFile(localFile, { encoding: 'utf8' }) |
| 213 | } else { |
| 214 | warning(`Unable to locate ${localFile}`) |
| 215 | const cdnUrl = `https://cdn.jsdelivr.net/npm/@formkit/inputs@${FORMKIT_VERSION}/dist/exports/${name}.${lang}` |
| 216 | try { |
| 217 | const res = await axios.get(cdnUrl) |
| 218 | fileData = res.data |
| 219 | } catch (e: any) { |
| 220 | if (e && e?.response?.status) { |
| 221 | error(`${e.response.status} — unable to load ${localFile}`) |
| 222 | } else { |
| 223 | error( |
| 224 | 'Unable to load input file — probably a network error. Are you online?' |
| 225 | ) |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (!fileData) { |
| 231 | info('Checking CDN for an exportable input.') |
| 232 | } |
| 233 | |
| 234 | if (!fileData) { |
| 235 | error(`Unable to load export ${name}.${lang}`) |
| 236 | } else { |
| 237 | return fileData |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Guess the language the user is leveraging on their project. |
no test coverage detected