Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
import inquirer from 'inquirer';
17
import SearchList from 'inquirer-search-list';
18
 
19
import { getNoteNames } from './noteTypes.mjs';
20
import { getAllComponents } from './components.mjs';
21
import {
22
    formatComponent,
23
    formatIssueNumber,
24
    validateComponent,
25
    validateIssueNumber,
26
 } from './helpers.mjs';
27
 
28
/**
29
 * A Search List which accepts an initial value.
30
 */
31
class SearchListWithInitialValue extends SearchList {
32
    constructor(options, ...args) {
33
        super(options, ...args);
34
 
35
        if (options.default) {
36
            const pointer = this.filterList.findIndex((item) => {
37
                return item.value === options.default;
38
            });
39
            if (pointer > -1) {
40
                this.pointer = pointer;
41
            }
42
        }
43
    }
44
}
45
 
46
inquirer.registerPrompt('search-list', SearchListWithInitialValue);
47
 
48
/**
49
 * Get the issue type prompt.
50
 *
51
 * @param {string} defaultData The initially selected value
52
 * @returns {Object}
53
 */
54
export const getTypePrompt = (defaultData) => ({
55
    default: defaultData,
56
    type: 'search-list',
57
    message: 'Type of change',
58
    name: 'type',
59
    choices: getNoteNames(),
60
    validate: (selection) => {
61
        if (selection.length < 1) {
62
            return 'You must select at least one type of change';
63
        }
64
 
65
        return true;
66
    },
67
});
68
 
69
/**
70
 * Get the component prompt.
71
 *
72
 * @param {string} [defaultValue='core'] The initally selected value.
73
 * @returns
74
 */
75
export const getComponentsPrompt = (defaultValue) => {
76
    if (!defaultValue ) {
77
        defaultValue = 'core';
78
    }
79
 
80
    return {
81
        choices: getAllComponents(),
82
        default: defaultValue,
83
        type: 'search-list',
84
        message: 'Component',
85
        name: 'components',
86
        validate: validateComponent,
87
        filter: formatComponent,
88
    };
89
};
90
 
91
/**
92
 * Get the issue number prompt as an inline input.
93
 *
94
 * @param {string} defaultData
95
 * @returns {object}
96
 */
97
export const getIssuePrompt = (defaultData) => ({
98
    default: defaultData,
99
    type: 'input',
100
    message: 'Tracker issue number',
101
    name: 'issueNumber',
102
    validate: validateIssueNumber,
103
    filter: formatIssueNumber,
104
});
105
 
106
/**
107
 * Get a message prompt.
108
 *
109
 * @param {string} defaultData
110
 * @returns
111
 */
112
export const getMessagePromptEditor = (defaultData) => ({
113
    default: defaultData,
114
    type: process.stdin.isTTY ? 'editor' : 'input',
115
    postfix: '.md',
116
    message: 'Message',
117
    name: 'message',
118
    waitUserInput: false,
119
    validate: (input) => {
120
        if (!input) {
121
            return 'You must provide a message';
122
        }
123
        return true;
124
    },
125
    // Remove any trailing whitespace.
126
    filter: (input) => input.split('\n').map((line) => line.trimEnd()).join('\n'),
127
});
128
 
129
/**
130
 * Get a message prompt.
131
 *
132
 * @param {string} defaultData
133
 * @returns
134
 */
135
export const getMessagePromptInput = (defaultData) => ({
136
    default: defaultData,
137
    type: 'input',
138
    message: 'Message (leave empty to use editor)',
139
    name: 'message',
140
    filter: (input) => input.trim(),
141
});
142
 
143
/**
144
 * Get a prompt to ask the user if they wish to add another entry.
145
 *
146
 * @returns {Object}
147
 */
148
export const getAddAnotherPrompt = () => ({
149
    type: 'confirm',
150
    message: 'Do you want to add another note?',
151
    default: false,
152
    name: 'addAnother',
153
});