@magda/scripts 4.2.1 → 4.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,117 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import util from "util";
4
-
5
- import Choices from "inquirer/lib/objects/choices.js";
6
- import InquirerAutocomplete from "inquirer-autocomplete-prompt";
7
- import stripAnsi from "strip-ansi";
8
- import style from "ansi-styles";
9
- import fuzzy from "fuzzy";
10
-
11
- const readdir_ = util.promisify(fs.readdir);
12
- const maxScanDepth = 2;
13
-
14
- /**
15
- * A customized UI input (A file picker).
16
- * It's modified from: https://github.com/adelsz/inquirer-fuzzy-path
17
- * Differences & improvements are:
18
- * - Support validator
19
- * - Fixed: cannot handle `ENOENT` correctly
20
- * - Limit the file searching to 2 level deep to improve the performance
21
- */
22
- class InquirerFuzzyPath extends InquirerAutocomplete {
23
- constructor(question, rl, answers) {
24
- const rootPath = question.rootPath || ".";
25
- const pathFilter = question.pathFilter || (() => true);
26
- const _question = Object.assign({}, question, {
27
- source: (_, pattern) => getPaths(rootPath, pattern, pathFilter)
28
- });
29
- super(_question, rl, answers);
30
- }
31
-
32
- search(searchTerm) {
33
- return super.search(searchTerm).then((value) => {
34
- this.currentChoices.getChoice = (choiceIndex) => {
35
- let choice = Choices.prototype.getChoice.call(
36
- this.currentChoices,
37
- choiceIndex
38
- );
39
- if (!choice) {
40
- return null;
41
- }
42
- return {
43
- value: stripAnsi(choice.value),
44
- name: stripAnsi(choice.name),
45
- short: stripAnsi(choice.name)
46
- };
47
- };
48
- });
49
- }
50
-
51
- onSubmit(line) {
52
- if (typeof this.opt.validate === "function") {
53
- const choice = this.currentChoices.getChoice(this.selected);
54
- if (!choice) {
55
- this.render(
56
- "You need to select a file. Make sure the json file is in the current directory or its sub-directory"
57
- );
58
- return;
59
- }
60
- const validationResult = this.opt.validate(
61
- this.currentChoices.getChoice(this.selected)
62
- );
63
- if (validationResult !== true) {
64
- this.render(validationResult || "You need to select a file");
65
- return;
66
- }
67
- }
68
- super.onSubmit(stripAnsi(line));
69
- }
70
- }
71
-
72
- function getPaths(rootPath, pattern, pathFilter) {
73
- const fuzzOptions = {
74
- pre: style.green.open,
75
- post: style.green.close
76
- };
77
-
78
- function nodeOption(nodePath, isDirectory) {
79
- return pathFilter(isDirectory, nodePath) ? [nodePath] : [];
80
- }
81
-
82
- async function listNodes(nodePath, depth = 0) {
83
- try {
84
- if (depth >= maxScanDepth) {
85
- return nodeOption(nodePath, false);
86
- }
87
- const nodes = await readdir_(nodePath);
88
- const currentNode = nodeOption(nodePath, true);
89
- if (nodes.length > 0) {
90
- const nodex = nodes.map((dirName) =>
91
- listNodes(path.join(nodePath, dirName), depth + 1)
92
- );
93
- const subNodes = await Promise.all(nodex);
94
- return subNodes.reduce(
95
- (acc, val) => acc.concat(val),
96
- currentNode
97
- );
98
- } else {
99
- return currentNode;
100
- }
101
- } catch (err) {
102
- if (err.code === "ENOENT" || err.code === "ENOTDIR") {
103
- return nodeOption(nodePath, false);
104
- } else {
105
- throw err;
106
- }
107
- }
108
- }
109
-
110
- const nodes = listNodes(rootPath);
111
- const filterPromise = nodes.then((nodeList) =>
112
- fuzzy.filter(pattern || "", nodeList, fuzzOptions).map((e) => e.string)
113
- );
114
- return filterPromise;
115
- }
116
-
117
- export default InquirerFuzzyPath;
@@ -1,101 +0,0 @@
1
- import List from "inquirer/lib/prompts/list.js";
2
- import chalk from "chalk";
3
- import figures from "figures";
4
- import cliCursor from "cli-cursor";
5
-
6
- /**
7
- * A customized UI input (selection list).
8
- * It's built on top of built-in `List` UI input with the following enhancement:
9
- * - Support `transformer`. Therefore, I can display the answer in a different way.
10
- * e.g. when you've selected `Yes. auto generate password`, I can display `the generated password`
11
- * instead of default `Yes. auto generate password`
12
- */
13
- class ListWithTransformer extends List {
14
- render(value) {
15
- // Render question
16
- var message = this.getQuestion();
17
- var transformer = this.opt.transformer;
18
-
19
- if (this.firstRender) {
20
- message += chalk.dim("(Use arrow keys)");
21
- }
22
-
23
- // Render choices or answer depending on the state
24
- if (this.status === "answered") {
25
- if (transformer) {
26
- message += transformer(value, this.answers);
27
- } else {
28
- message += chalk.cyan(
29
- this.opt.choices.getChoice(this.selected).short
30
- );
31
- }
32
- } else {
33
- var choicesStr = listRender(this.opt.choices, this.selected);
34
- var indexPosition = this.opt.choices.indexOf(
35
- this.opt.choices.getChoice(this.selected)
36
- );
37
- message +=
38
- "\n" +
39
- this.paginator.paginate(
40
- choicesStr,
41
- indexPosition,
42
- this.opt.pageSize
43
- );
44
- }
45
-
46
- this.firstRender = false;
47
-
48
- this.screen.render(message);
49
- }
50
-
51
- onSubmit(value) {
52
- this.status = "answered";
53
-
54
- // Rerender prompt
55
- this.render(value);
56
-
57
- this.screen.done();
58
- cliCursor.show();
59
- this.done(value);
60
- }
61
- }
62
-
63
- /**
64
- * Function for rendering list choices
65
- * @param {Number} pointer Position of the pointer
66
- * @return {String} Rendered content
67
- */
68
- function listRender(choices, pointer) {
69
- var output = "";
70
- var separatorOffset = 0;
71
-
72
- choices.forEach((choice, i) => {
73
- if (choice.type === "separator") {
74
- separatorOffset++;
75
- output += " " + choice + "\n";
76
- return;
77
- }
78
-
79
- if (choice.disabled) {
80
- separatorOffset++;
81
- output += " - " + choice.name;
82
- output +=
83
- " (" +
84
- (_.isString(choice.disabled) ? choice.disabled : "Disabled") +
85
- ")";
86
- output += "\n";
87
- return;
88
- }
89
-
90
- var isSelected = i - separatorOffset === pointer;
91
- var line = (isSelected ? figures.pointer + " " : " ") + choice.name;
92
- if (isSelected) {
93
- line = chalk.cyan(line);
94
- }
95
- output += line + " \n";
96
- });
97
-
98
- return output.replace(/\n$/, "");
99
- }
100
-
101
- export default ListWithTransformer;
@@ -1,16 +0,0 @@
1
- import Pwgen from "pwgen/lib/pwgen_module.js";
2
-
3
- function generatePassword(
4
- maxLength = 16,
5
- includeCapitalLetter = true,
6
- includeNumber = true
7
- ) {
8
- const pwgenGenerator = new Pwgen();
9
- pwgenGenerator.includeCapitalLetter = includeCapitalLetter;
10
- pwgenGenerator.includeNumber = includeNumber;
11
- pwgenGenerator.maxLength = maxLength;
12
-
13
- return pwgenGenerator.generate();
14
- }
15
-
16
- export default generatePassword;