UNPKG

3.2 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
7 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
8 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
9 * Google as part of the polymer project is also subject to an additional IP
10 * rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
15}) : (function(o, m, k, k2) {
16 if (k2 === undefined) k2 = k;
17 o[k2] = m[k];
18}));
19var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20 Object.defineProperty(o, "default", { enumerable: true, value: v });
21}) : function(o, v) {
22 o["default"] = v;
23});
24var __importStar = (this && this.__importStar) || function (mod) {
25 if (mod && mod.__esModule) return mod;
26 var result = {};
27 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28 __setModuleDefault(result, mod);
29 return result;
30};
31Object.defineProperty(exports, "__esModule", { value: true });
32exports.throwUnreachable = exports.wait = exports.runNpm = exports.fileKind = exports.isHttpUrl = void 0;
33const child_process_1 = require("child_process");
34const fsExtra = __importStar(require("fs-extra"));
35const url_1 = require("url");
36const util_1 = require("util");
37/** Return whether the given string is a valid HTTP URL. */
38function isHttpUrl(str) {
39 try {
40 const url = new url_1.URL(str);
41 // Note an absolute Windows file path will parse as a URL (e.g.
42 // 'C:\\foo\\bar' => {protocol: 'c:', pathname: '\\foo\\bar', ...})
43 return url.protocol === 'http:' || url.protocol === 'https:';
44 }
45 catch (e) {
46 return false;
47 }
48}
49exports.isHttpUrl = isHttpUrl;
50async function fileKind(path) {
51 try {
52 const stat = await fsExtra.stat(path);
53 if (stat.isDirectory()) {
54 return 'dir';
55 }
56 if (stat.isFile()) {
57 return 'file';
58 }
59 }
60 catch (e) {
61 if (e.code === 'ENOENT') {
62 return undefined;
63 }
64 throw e;
65 }
66}
67exports.fileKind = fileKind;
68const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
69async function runNpm(args, options) {
70 return util_1.promisify(child_process_1.execFile)(npmCmd, args, options).then(({ stdout }) => stdout);
71}
72exports.runNpm = runNpm;
73/**
74 * Promisified version of setTimeout.
75 */
76exports.wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
77/**
78 * A function that should never be called. But if it somehow is anyway, throw an
79 * exception with the given message.
80 */
81function throwUnreachable(_unreachable, message) {
82 throw new Error(message);
83}
84exports.throwUnreachable = throwUnreachable;
85//# sourceMappingURL=util.js.map
\No newline at end of file