| 1 | import { tryParseTokenFromCredentials } from "../../credentials.js"; |
| 2 | import type { Validator } from "../types"; |
| 3 | import type { RunOptions } from "../../types"; |
| 4 | import { value, questions } from "./helpers.js"; |
| 5 | |
| 6 | interface TokenValidation { |
| 7 | token: string; |
| 8 | } |
| 9 | |
| 10 | export const validateToken: Validator<TokenValidation> = async (options: RunOptions) => { |
| 11 | const { token, interactive } = options; |
| 12 | if (token) return value({ token }); |
| 13 | if (process.env.FIG_API_TOKEN) return value({ token: process.env.FIG_API_TOKEN }); |
| 14 | // try to read the token from the config file |
| 15 | try { |
| 16 | return value({ token: await tryParseTokenFromCredentials() }); |
| 17 | } catch (error) { |
| 18 | if (!interactive) throw error; |
| 19 | return questions({ |
| 20 | type: "password", |
| 21 | message: |
| 22 | "A Fig API token to push a spec to a team but none was found, please insert one manually:", |
| 23 | name: "token", |
| 24 | validate: (v: string) => !!v ?? "A Token is required to push the spec to a team", |
| 25 | }); |
| 26 | } |
| 27 | }; |