From 402908c8f270bb4bf6850ffbca3fba2ec07cc100 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Thu, 3 Jun 2021 16:31:23 -0400 Subject: [PATCH] Added value check for "feature" parameter in init When the optional feature parameter is provided in a `firebase init [feature]` command, this checks that its value is a valid choice before attempting any other initialization. --- src/commands/init.js | 91 +++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/src/commands/init.js b/src/commands/init.js index 1796d664afb..44cf0188994 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -23,10 +23,61 @@ var _isOutside = function (from, to) { return path.relative(from, to).match(/^\.\./); }; +const choices = [ + { + value: "database", + name: + "Realtime Database: Configure a security rules file for Realtime Database and (optionally) provision default instance", + checked: false, + }, + { + value: "firestore", + name: "Firestore: Configure security rules and indexes files for Firestore", + checked: false, + }, + { + value: "functions", + name: "Functions: Configure a Cloud Functions directory and its files", + checked: false, + }, + { + value: "hosting", + name: + "Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys", + checked: false, + }, + { + value: "storage", + name: "Storage: Configure a security rules file for Cloud Storage", + checked: false, + }, + { + value: "emulators", + name: "Emulators: Set up local emulators for Firebase products", + checked: false, + }, + { + value: "remoteconfig", + name: "Remote Config: Configure a template file for Remote Config", + checked: false, + }, +]; +const featureNames = choices.map((choice) => choice.value); + module.exports = new Command("init [feature]") .description("set up a Firebase project in the current directory") .before(requireAuth) .action(function (feature, options) { + if (feature && !featureNames.includes(feature)) { + return utils.reject( + clc.bold(feature) + + " is not a supported feature; must be one of " + + featureNames.join(", ") + + ".", + { exit: 1 } + ); + } + var cwd = options.cwd || process.cwd(); var warnings = []; @@ -70,46 +121,6 @@ module.exports = new Command("init [feature]") }), }; - var choices = [ - { - value: "database", - name: - "Realtime Database: Configure a security rules file for Realtime Database and (optionally) provision default instance", - checked: false, - }, - { - value: "firestore", - name: "Firestore: Configure security rules and indexes files for Firestore", - checked: false, - }, - { - value: "functions", - name: "Functions: Configure a Cloud Functions directory and its files", - checked: false, - }, - { - value: "hosting", - name: - "Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys", - checked: false, - }, - { - value: "storage", - name: "Storage: Configure a security rules file for Cloud Storage", - checked: false, - }, - { - value: "emulators", - name: "Emulators: Set up local emulators for Firebase products", - checked: false, - }, - { - value: "remoteconfig", - name: "Remote Config: Configure a template file for Remote Config", - checked: false, - }, - ]; - var next; // HACK: Windows Node has issues with selectables as the first prompt, so we // add an extra confirmation prompt that fixes the problem