1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | const tmp = require("tmp");
|
9 | const open = require("opn");
|
10 | const exec_1 = require("./exec");
|
11 | const fs_1 = require("fs");
|
12 | const logger = require("./logger");
|
13 | const inquirer = require("inquirer");
|
14 | const testing = process.env.NODE_ENV === 'testing';
|
15 | async function handlePagination({ options, listEndpoint, payload }) {
|
16 | let hasNextPage = false;
|
17 | try {
|
18 |
|
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 |
|
28 |
|
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 | }
|
40 | exports.handlePagination = handlePagination;
|
41 |
|
42 |
|
43 |
|
44 | function openUrl(url) {
|
45 | testing ? console.log(url) : open(url, { wait: false });
|
46 | }
|
47 | exports.openUrl = openUrl;
|
48 |
|
49 |
|
50 |
|
51 | function 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 | }
|
62 | exports.userLeftMsgEmpty = userLeftMsgEmpty;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function 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 | }
|
84 | exports.openFileInEditor = openFileInEditor;
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function cleanFileContents(fileContents, commentMark = '#') {
|
90 | return fileContents
|
91 | .split('\n')
|
92 | .filter(line => !line.startsWith(commentMark))
|
93 | .join('\n')
|
94 | .trim();
|
95 | }
|
96 | exports.cleanFileContents = cleanFileContents;
|
97 | function getCurrentFolderName() {
|
98 | const cwdArr = process
|
99 | .cwd()
|
100 | .toString()
|
101 | .split('/');
|
102 | return cwdArr[cwdArr.length - 1];
|
103 | }
|
104 | exports.getCurrentFolderName = getCurrentFolderName;
|
105 |
|
106 |
|
107 |
|
108 | function userRanValidFlags(commands, options) {
|
109 | if (commands) {
|
110 | return commands.some(c => {
|
111 | return options[c] !== undefined;
|
112 | });
|
113 | }
|
114 | return false;
|
115 | }
|
116 | exports.userRanValidFlags = userRanValidFlags;
|
117 | async 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 | }
|
129 | exports.askUserToPaginate = askUserToPaginate;
|
130 |
|
\ | No newline at end of file |