UNPKG

2.99 kBJavaScriptView Raw
1"use strict";
2/*
3 Copyright 2018 Google LLC
4
5 Use of this source code is governed by an MIT-style
6 license that can be found in the LICENSE file or at
7 https://opensource.org/licenses/MIT.
8*/
9var __importDefault = (this && this.__importDefault) || function (mod) {
10 return (mod && mod.__esModule) ? mod : { "default": mod };
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.askRootOfWebApp = void 0;
14const assert_1 = __importDefault(require("assert"));
15const fs_extra_1 = __importDefault(require("fs-extra"));
16const glob_1 = __importDefault(require("glob"));
17const inquirer_1 = require("inquirer");
18const common_tags_1 = require("common-tags");
19const errors_1 = require("../errors");
20const constants_1 = require("../constants");
21const ROOT_PROMPT = 'Please enter the path to the root of your web app:';
22// The keys used for the questions/answers.
23const questionRootDirectory = 'globDirectory';
24const questionManualInput = 'manualDirectoryInput';
25/**
26 * @return {Promise<Array<string>>} The subdirectories of the current
27 * working directory, with hidden and ignored ones filtered out.
28 */
29async function getSubdirectories() {
30 return await new Promise((resolve, reject) => {
31 (0, glob_1.default)('*/', {
32 ignore: constants_1.constants.ignoredDirectories.map((directory) => `${directory}/`),
33 }, (error, directories) => {
34 if (error) {
35 reject(error);
36 }
37 else {
38 resolve(directories);
39 }
40 });
41 });
42}
43/**
44 * @return {Promise<Object>} The answers from inquirer.
45 */
46async function askQuestion() {
47 const subdirectories = await getSubdirectories();
48 if (subdirectories.length > 0) {
49 const manualEntryChoice = 'Manually enter path';
50 return (0, inquirer_1.prompt)([
51 {
52 name: questionRootDirectory,
53 type: 'list',
54 message: (0, common_tags_1.oneLine) `What is the root of your web app (i.e. which directory do
55 you deploy)?`,
56 choices: subdirectories.concat([new inquirer_1.Separator(), manualEntryChoice]),
57 },
58 {
59 name: questionManualInput,
60 when: (answers) => answers.globDirectory === manualEntryChoice,
61 message: ROOT_PROMPT,
62 },
63 ]);
64 }
65 return (0, inquirer_1.prompt)([
66 {
67 name: questionRootDirectory,
68 message: ROOT_PROMPT,
69 default: '.',
70 },
71 ]);
72}
73async function askRootOfWebApp() {
74 const { manualDirectoryInput, globDirectory } = await askQuestion();
75 try {
76 const stat = await fs_extra_1.default.stat(manualDirectoryInput || globDirectory);
77 (0, assert_1.default)(stat.isDirectory());
78 }
79 catch (error) {
80 throw new Error(errors_1.errors['glob-directory-invalid']);
81 }
82 return manualDirectoryInput || globDirectory;
83}
84exports.askRootOfWebApp = askRootOfWebApp;