Skip to content

Commit f2a8e02

Browse files
committed
✨ Presists previous entries for quick reuse
1 parent 767ad5c commit f2a8e02

File tree

5 files changed

+226
-5
lines changed

5 files changed

+226
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,8 @@ Which can be configured like so:
112112
]
113113
}
114114
```
115+
116+
## You might also like...
117+
118+
- [ScriptPal](https://github.com/zeropoly/scriptpal): A simple npm script palette for lazy people
119+
- [Enquirer](https://github.com/enquirer/enquirer): Stylish, intuitive and user-friendly prompts

bin/hash.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function hash(str) {
2+
return str
3+
.split("")
4+
.reduce(function(hash, c) {
5+
return ((hash << 5) - hash + c.charCodeAt(0)) | 0;
6+
}, 0)
7+
.toString();
8+
};

bin/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@ const { prompt, Input, AutoComplete } = require("enquirer");
55
const meow = require("meow");
66
const chalk = require("chalk");
77
const simpleGit = require("simple-git/promise");
8+
const Conf = require("conf");
89

910
const presets = require("./presets");
1011
const welcome = require("./welcome");
12+
const hash = require("./hash");
1113
const { getConfig, hasFile } = require("./file-manager");
1214

1315
const git = simpleGit().outputHandler((command, stdout, stderr) => {
1416
stdout.pipe(process.stdout);
1517
stderr.pipe(process.stderr);
1618
});
1719

18-
function getStepPrompt(step) {
20+
function getStepPrompt(step, previous) {
1921
switch (step.type) {
2022
case "option":
2123
return new AutoComplete({
2224
name: "step",
2325
message: step.message,
24-
choices: step.options.map(option => option.description)
26+
choices: step.options.map(option => option.description),
27+
initial:
28+
step.options.findIndex(option => option.value === previous) || 0
2529
})
2630
.run()
2731
.then(choice => {
@@ -34,7 +38,7 @@ function getStepPrompt(step) {
3438
case "text":
3539
return new Input({
3640
message: step.message,
37-
initial: step.initial
41+
initial: step.initial || previous
3842
})
3943
.run()
4044
.then(choice => choice || "");
@@ -70,6 +74,7 @@ const getPresetPrompt = async () => {
7074
async function main(input, flags) {
7175
if (!flags.nowelcome) welcome();
7276

77+
const history = new Conf();
7378
let config = {};
7479

7580
try {
@@ -86,11 +91,17 @@ async function main(input, flags) {
8691
config = presets[presetKey];
8792
}
8893

89-
const message = await config.steps.reduce(async (accum, step) => {
94+
const message = await config.steps.reduce(async (accum, step, i) => {
9095
const message = await accum;
91-
const prompt = getStepPrompt(step);
96+
97+
const stepHash = hash(`${config.name}${step.message}${i}`);
98+
const previous = history.get(stepHash);
99+
100+
const prompt = getStepPrompt(step, previous);
92101
const result = await prompt;
93102

103+
history.set(stepHash, result);
104+
94105
return `${message}${step.before || ""}${result}${step.after || ""}`;
95106
}, "");
96107

package-lock.json

Lines changed: 196 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
],
2727
"dependencies": {
2828
"chalk": "^3.0.0",
29+
"conf": "^6.2.1",
2930
"enquirer": "^2.3.2",
3031
"find-config": "^1.0.0",
3132
"gradient-string": "^1.2.0",

0 commit comments

Comments
 (0)