UNPKG

3.81 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.askExtensionsToCache = void 0;
14const assert_1 = __importDefault(require("assert"));
15const inquirer_1 = require("inquirer");
16const glob_1 = __importDefault(require("glob"));
17const ora_1 = __importDefault(require("ora"));
18const upath_1 = __importDefault(require("upath"));
19const errors_1 = require("../errors");
20const constants_1 = require("../constants");
21// The key used for the question/answer.
22const name = 'globPatterns';
23/**
24 * @param {string} globDirectory The directory used for the root of globbing.
25 * @return {Promise<Array<string>>} The unique file extensions corresponding
26 * to all of the files under globDirectory.
27 */
28async function getAllFileExtensions(globDirectory) {
29 const files = await new Promise((resolve, reject) => {
30 // Use a pattern to match any file that contains a '.', since that signifies
31 // the presence of a file extension.
32 (0, glob_1.default)('**/*.*', {
33 cwd: globDirectory,
34 nodir: true,
35 ignore: [
36 ...constants_1.constants.ignoredDirectories.map((directory) => `**/${directory}/**`),
37 ...constants_1.constants.ignoredFileExtensions.map((extension) => `**/*.${extension}`),
38 ],
39 }, (error, files) => {
40 if (error) {
41 reject(error);
42 }
43 else {
44 resolve(files);
45 }
46 });
47 });
48 const extensions = new Set();
49 for (const file of files) {
50 const extension = upath_1.default.extname(file);
51 if (extension) {
52 // Get rid of the leading . character.
53 extensions.add(extension.replace(/^\./, ''));
54 }
55 }
56 return [...extensions];
57}
58/**
59 * @param {string} globDirectory The directory used for the root of globbing.
60 * @return {Promise<Answers>} The answers from inquirer.
61 */
62async function askQuestion(globDirectory) {
63 // We need to get a list of extensions corresponding to files in the directory
64 // to use when asking the next question. That could potentially take some
65 // time, so we show a spinner and explanatory text.
66 const spinner = (0, ora_1.default)({
67 text: `Examining files in ${globDirectory}...`,
68 stream: process.stdout,
69 }).start();
70 const fileExtensions = await getAllFileExtensions(globDirectory);
71 spinner.stop();
72 (0, assert_1.default)(fileExtensions.length > 0, errors_1.errors['no-file-extensions-found']);
73 return (0, inquirer_1.prompt)([
74 {
75 name,
76 message: 'Which file types would you like to precache?',
77 type: 'checkbox',
78 choices: fileExtensions,
79 default: fileExtensions,
80 },
81 ]);
82}
83async function askExtensionsToCache(globDirectory) {
84 const answers = await askQuestion(globDirectory);
85 // The return value is an array of strings with the selected values
86 // and there is a default, the casting is safe.
87 const extensions = answers[name];
88 (0, assert_1.default)(extensions.length > 0, errors_1.errors['no-file-extensions-selected']);
89 // glob isn't happy with a single option inside of a {} group, so use a
90 // pattern without a {} group when there's only one extension.
91 const extensionsPattern = extensions.length === 1 ? extensions[0] : `{${extensions.join(',')}}`;
92 return [`**/*.${extensionsPattern}`];
93}
94exports.askExtensionsToCache = askExtensionsToCache;