UNPKG

4.37 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const tmp = require("tmp");
9const open = require("opn");
10const exec_1 = require("./exec");
11const fs_1 = require("fs");
12const logger = require("./logger");
13const inquirer = require("inquirer");
14const testing = process.env.NODE_ENV === 'testing';
15async function handlePagination({ options, listEndpoint, payload }) {
16 let hasNextPage = false;
17 try {
18 // If no pageSize, assume user removed limit and fetch all prs
19 var data = await (options.allPages
20 ? options.GitHub.paginate(listEndpoint.endpoint.merge(payload))
21 : listEndpoint(payload));
22 hasNextPage = data.headers && data.headers.link && data.headers.link.includes('rel="next"');
23 data = data.data || data;
24 }
25 catch (err) {
26 if (err && err.status === '404') {
27 // Some times a repo is found, but you can't list its prs
28 // Due to the repo being disabled (e.g., private repo with debt)
29 logger.warn(`Can't list pull requests for ${options.user}/${options.repo}`);
30 }
31 else {
32 throw new Error(`Error listing data\n${err}`);
33 }
34 }
35 return {
36 data,
37 hasNextPage,
38 };
39}
40exports.handlePagination = handlePagination;
41/**
42 * Opens url in browser
43 */
44function openUrl(url) {
45 testing ? console.log(url) : open(url, { wait: false });
46}
47exports.openUrl = openUrl;
48/**
49 * Checks if string has been merged with a common flag or is empty
50 */
51function userLeftMsgEmpty(string) {
52 return (!string ||
53 string === '--title' ||
54 string === '-t' ||
55 string === '--message' ||
56 string === '-m' ||
57 string === '--comment' ||
58 string === '-c' ||
59 string === '--description' ||
60 string === '-D');
61}
62exports.userLeftMsgEmpty = userLeftMsgEmpty;
63/**
64 * Allows users to add text from their editor of choice rather than the terminal
65 *
66 * @example
67 * openFileInEditor('temp-gh-issue-title.txt', '# Add a pr title msg on the next line')
68 */
69function openFileInEditor(fileName, msg) {
70 try {
71 var { name: filePath, removeCallback } = tmp.fileSync({ postfix: `-${fileName}` });
72 fs_1.writeFileSync(filePath, msg);
73 const editor = process.env.EDITOR ||
74 process.env.VISUAL ||
75 exec_1.spawnSync('git', ['config', '--global', 'core.editor']).stdout;
76 if (editor) {
77 exec_1.execSyncInteractiveStream(`${editor} "${filePath}"`);
78 }
79 const newFileContents = fs_1.readFileSync(filePath).toString();
80 const commentMark = fileName.endsWith('.md') ? '<!--' : '#';
81 removeCallback();
82 return cleanFileContents(newFileContents, commentMark);
83 }
84 catch (err) {
85 logger.error('Could not use your editor to store a custom message\n', err);
86 }
87}
88exports.openFileInEditor = openFileInEditor;
89/**
90 * Removes # comments and trims new lines
91 * @param {string} commentMark - refers to the comment mark which is different for each file
92 */
93function cleanFileContents(fileContents, commentMark = '#') {
94 return fileContents
95 .split('\n')
96 .filter(line => !line.startsWith(commentMark))
97 .join('\n')
98 .trim();
99}
100exports.cleanFileContents = cleanFileContents;
101function getCurrentFolderName() {
102 const cwdArr = process
103 .cwd()
104 .toString()
105 .split('/');
106 return cwdArr[cwdArr.length - 1];
107}
108exports.getCurrentFolderName = getCurrentFolderName;
109/**
110 * Checks to see if the cli arguments are one of the accepted flags
111 */
112function userRanValidFlags(commands, options) {
113 if (commands) {
114 return commands.some(c => {
115 return options[c] !== undefined;
116 });
117 }
118 return false;
119}
120exports.userRanValidFlags = userRanValidFlags;
121async function askUserToPaginate(type) {
122 logger.log('\n');
123 const answers = await inquirer.prompt([
124 {
125 type: 'confirm',
126 message: `Would you like to see the next batch of ${type}`,
127 name: 'paginate',
128 },
129 ]);
130 logger.log('\n');
131 return answers.paginate;
132}
133exports.askUserToPaginate = askUserToPaginate;
134//# sourceMappingURL=utils.js.map
\No newline at end of file