Skip to content

Commit 7bd562d

Browse files
committed
Interactively choose script when none is specified
1 parent 2853977 commit 7bd562d

File tree

2 files changed

+105
-34
lines changed

2 files changed

+105
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ npm install edit-script
1414
$ edit-script --help
1515
Edit npm scripts from the command line without worrying about json escaping.
1616
17+
edit-script
1718
edit-script <script>
1819
```
1920

cli.js

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ var findPkg = require('pkg-up');
77
var script = process.argv[2];
88

99
// help message:
10-
if (script === '--help') {
10+
if (process.argv[2] === '--help') {
1111
console.log(
1212
`Edit npm scripts from the command line without worrying about json escaping.
1313
14+
edit-script
1415
edit-script <script>`
1516
);
1617
process.exit();
@@ -20,37 +21,24 @@ var pkgPath;
2021
var pkg = {}
2122
var scripts = {};
2223

23-
if (!script) {
24-
console.error('Error: Must pass a script name');
25-
process.exit(1);
26-
}
24+
const NEW_SCRIPT_SYMBOL = Symbol('Create new script');
25+
const EXIT_SYMBOL = Symbol('Exit');
2726

27+
// Find package.json path:
2828
findPkg()
2929
.then(function (p) {
3030
if (!p) throw new Error('No package.json file found!');
3131
pkgPath = p;
32+
// Load package.json:
3233
return fs.readJson(pkgPath);
3334
})
3435
.then(function (data) {
36+
// Assign global variables:
3537
pkg = data;
3638
scripts = pkg.scripts;
37-
38-
return inquirer.prompt([
39-
{
40-
type: 'confirm',
41-
name: 'create',
42-
message: `The script "${script}" does not exist. Create it?`,
43-
when: !scripts[script],
44-
},
45-
]);
46-
})
47-
.then(function (answers) {
48-
if (!scripts[script] && !answers.create) {
49-
console.log('Aborting');
50-
process.exit();
51-
}
5239
})
53-
.then(function () {
40+
.then(getScriptName)
41+
.then(function editScript() {
5442
return inquirer.prompt([
5543
{
5644
type: 'editor',
@@ -59,18 +47,100 @@ findPkg()
5947
default: scripts[script],
6048
},
6149
])
62-
})
63-
.then(function (answers) {
64-
var val = answers.script.trim();
65-
if (!val) {
66-
console.log('Deleting script.');
67-
delete scripts[script];
50+
.then(function (answers) {
51+
var val = answers.script.trim();
52+
if (!val) {
53+
console.log('Deleting script.');
54+
delete scripts[script];
55+
} else {
56+
scripts[script] = val;
57+
}
58+
return fs.writeJson(pkgPath, pkg)
59+
})
60+
.catch(function (err) {
61+
console.error(err);
62+
process.exit(1);
63+
});
64+
});
65+
66+
function getScriptName() {
67+
if (script) {
68+
// Verify creation if the script does not exist:
69+
return inquirer.prompt([
70+
{
71+
type: 'confirm',
72+
name: 'create',
73+
message: `The script "${script}" does not exist. Create it?`,
74+
when: !scripts[script],
75+
},
76+
])
77+
.then(function (answers) {
78+
if (!script && !scripts[script] && !answers.create) {
79+
console.log('Aborting');
80+
process.exit();
81+
}
82+
});
6883
} else {
69-
scripts[script] = val;
84+
// Get choices:
85+
var choices = Object.keys(scripts).map(function (key) {
86+
return {
87+
name: pad(key, scripts[key]),
88+
value: key,
89+
short: key,
90+
}
91+
});
92+
// Add aditional choices:
93+
choices.push(new inquirer.Separator());
94+
choices.push({
95+
name: 'Create a new script',
96+
value: NEW_SCRIPT_SYMBOL,
97+
});
98+
choices.push({
99+
name: 'Exit edit-script',
100+
value: EXIT_SYMBOL,
101+
})
102+
// Prompt for script name:
103+
return inquirer.prompt([
104+
{
105+
type: 'list',
106+
name: 'script',
107+
message: 'Select a script to edit:',
108+
choices: choices,
109+
},
110+
])
111+
.then(function (answers) {
112+
switch (answers.script) {
113+
case NEW_SCRIPT_SYMBOL:
114+
// Get script name:
115+
return inquirer.prompt([{
116+
type: 'input',
117+
name: 'name',
118+
message: 'Enter the script name:',
119+
validate: function (val) {
120+
if (!val) return 'Script name must not be empty';
121+
else return true;
122+
},
123+
}])
124+
.then(function (answers) {
125+
// Set it:
126+
script = answers.name;
127+
})
128+
break;
129+
case EXIT_SYMBOL:
130+
process.exit();
131+
default:
132+
script = answers.script;
133+
}
134+
})
70135
}
71-
return fs.writeJson(pkgPath, pkg)
72-
})
73-
.catch(function (err) {
74-
console.error(err);
75-
process.exit(1);
76-
});
136+
}
137+
138+
function pad(str1, str2) {
139+
var padLen = 60 - (str1.length + str2.length);
140+
// Ensure at least one space:
141+
var pad = ' ';
142+
for (var i = 1; i < padLen; i++) {
143+
pad += ' ';
144+
}
145+
return str1 + pad + str2;
146+
}

0 commit comments

Comments
 (0)