UNPKG

4.26 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 = exec_1.spawnSync('git', ['config', '--global', 'core.editor']).stdout;
74 exec_1.execSyncInteractiveStream(`${editor} "${filePath}"`);
75 const newFileContents = fs_1.readFileSync(filePath).toString();
76 const commentMark = fileName.endsWith('.md') ? '<!--' : '#';
77 removeCallback();
78 return cleanFileContents(newFileContents, commentMark);
79 }
80 catch (err) {
81 logger.error('Could not use your editor to store a custom message\n', err);
82 }
83}
84exports.openFileInEditor = openFileInEditor;
85/**
86 * Removes # comments and trims new lines
87 * @param {string} commentMark - refers to the comment mark which is different for each file
88 */
89function cleanFileContents(fileContents, commentMark = '#') {
90 return fileContents
91 .split('\n')
92 .filter(line => !line.startsWith(commentMark))
93 .join('\n')
94 .trim();
95}
96exports.cleanFileContents = cleanFileContents;
97function getCurrentFolderName() {
98 const cwdArr = process
99 .cwd()
100 .toString()
101 .split('/');
102 return cwdArr[cwdArr.length - 1];
103}
104exports.getCurrentFolderName = getCurrentFolderName;
105/**
106 * Checks to see if the cli arguments are one of the accepted flags
107 */
108function userRanValidFlags(commands, options) {
109 if (commands) {
110 return commands.some(c => {
111 return options[c] !== undefined;
112 });
113 }
114 return false;
115}
116exports.userRanValidFlags = userRanValidFlags;
117async function askUserToPaginate(type) {
118 logger.log('\n');
119 const answers = await inquirer.prompt([
120 {
121 type: 'confirm',
122 message: `Would you like to see the next batch of ${type}`,
123 name: 'paginate',
124 },
125 ]);
126 logger.log('\n');
127 return answers.paginate;
128}
129exports.askUserToPaginate = askUserToPaginate;
130//# sourceMappingURL=utils.js.map
\No newline at end of file