UNPKG

6.44 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 var desc = Object.getOwnPropertyDescriptor(m, k);
12 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13 desc = { enumerable: true, get: function() { return m[k]; } };
14 }
15 Object.defineProperty(o, k2, desc);
16}) : (function(o, m, k, k2) {
17 if (k2 === undefined) k2 = k;
18 o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21 Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23 o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26 if (mod && mod.__esModule) return mod;
27 var result = {};
28 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29 __setModuleDefault(result, mod);
30 return result;
31};
32var __importDefault = (this && this.__importDefault) || function (mod) {
33 return (mod && mod.__esModule) ? mod : { "default": mod };
34};
35Object.defineProperty(exports, "__esModule", { value: true });
36exports.app = void 0;
37const common_tags_1 = require("common-tags");
38const workboxBuild = __importStar(require("workbox-build"));
39const assert_1 = __importDefault(require("assert"));
40const chokidar_1 = __importDefault(require("chokidar"));
41const pretty_bytes_1 = __importDefault(require("pretty-bytes"));
42const upath_1 = __importDefault(require("upath"));
43const constants_js_1 = require("./lib/constants.js");
44const errors_js_1 = require("./lib/errors.js");
45const logger_js_1 = require("./lib/logger.js");
46const read_config_js_1 = require("./lib/read-config.js");
47const run_wizard_js_1 = require("./lib/run-wizard.js");
48/**
49 * Runs the specified build command with the provided configuration.
50 *
51 * @param {Object} options
52 */
53async function runBuildCommand({ command, config, watch }) {
54 const { count, filePaths, size, warnings } = await workboxBuild[command](config);
55 for (const warning of warnings) {
56 logger_js_1.logger.warn(warning);
57 }
58 if (filePaths.length === 1) {
59 logger_js_1.logger.log(`The service worker file was written to ${config.swDest}`);
60 }
61 else {
62 const message = filePaths
63 .sort()
64 .map((filePath) => ` • ${filePath}`)
65 .join(`\n`);
66 logger_js_1.logger.log(`The service worker files were written to:\n${message}`);
67 }
68 logger_js_1.logger.log(`The service worker will precache ${count} URLs, ` +
69 `totaling ${(0, pretty_bytes_1.default)(size)}.`);
70 if (watch) {
71 logger_js_1.logger.log(`\nWatching for changes...`);
72 }
73}
74const app = async (params) => {
75 var _a;
76 // This should not be a user-visible error, unless meow() messes something up.
77 (0, assert_1.default)(params && Array.isArray(params.input), errors_js_1.errors['missing-input']);
78 // Default to showing the help message if there's no command provided.
79 const [command = 'help', option] = params.input;
80 switch (command) {
81 case 'wizard': {
82 await (0, run_wizard_js_1.runWizard)(params.flags);
83 break;
84 }
85 case 'copyLibraries': {
86 (0, assert_1.default)(option, errors_js_1.errors['missing-dest-dir-param']);
87 const parentDirectory = upath_1.default.resolve(process.cwd(), option);
88 const dirName = await workboxBuild.copyWorkboxLibraries(parentDirectory);
89 const fullPath = upath_1.default.join(parentDirectory, dirName);
90 logger_js_1.logger.log(`The Workbox libraries were copied to ${fullPath}`);
91 logger_js_1.logger.log((0, common_tags_1.oneLine) `Add a call to workbox.setConfig({modulePathPrefix: '...'})
92 to your service worker to use these local libraries.`);
93 logger_js_1.logger.log(`See https://goo.gl/Fo9gPX for further documentation.`);
94 break;
95 }
96 case 'generateSW':
97 case 'injectManifest': {
98 const configPath = upath_1.default.resolve(process.cwd(), option || constants_js_1.constants.defaultConfigFile);
99 let configFromDisk;
100 try {
101 configFromDisk = (0, read_config_js_1.readConfig)(configPath);
102 }
103 catch (error) {
104 if (error instanceof Error) {
105 logger_js_1.logger.error(errors_js_1.errors['invalid-common-js-module']);
106 throw error;
107 }
108 }
109 logger_js_1.logger.log(`Using configuration from ${configPath}.`);
110 const config = configFromDisk;
111 // Determine whether we're in --watch mode, or one-off mode.
112 if ((_a = params === null || params === void 0 ? void 0 : params.flags) === null || _a === void 0 ? void 0 : _a.watch) {
113 const options = {
114 ignoreInitial: true,
115 };
116 if (config.globIgnores) {
117 options.ignored = config.globIgnores;
118 }
119 if (config.globDirectory) {
120 options.cwd = config.globDirectory;
121 }
122 if (config.globPatterns) {
123 chokidar_1.default
124 .watch(config.globPatterns, options)
125 .on('all', async () => {
126 await runBuildCommand({ command, config, watch: true });
127 })
128 .on('ready', async () => {
129 await runBuildCommand({ command, config, watch: true });
130 })
131 .on('error', (err) => {
132 logger_js_1.logger.error(err.toString());
133 });
134 }
135 }
136 else {
137 await runBuildCommand({ command, config, watch: false });
138 }
139 break;
140 }
141 case 'help': {
142 params.showHelp();
143 break;
144 }
145 default: {
146 throw new Error(errors_js_1.errors['unknown-command'] + ` ` + command);
147 }
148 }
149};
150exports.app = app;