scriptpal 1.3.1 → 1.4.0
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.
- package/README.md +13 -0
- package/bin/detect-pkg-manager.js +25 -0
- package/bin/file-manager.js +2 -4
- package/bin/index.js +87 -92
- package/bin/main.js +45 -0
- package/bin/prompts.js +46 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -42,13 +42,26 @@ npx scriptpal
|
|
|
42
42
|
- `--clipboard`, `-c` Copy command to clipboard
|
|
43
43
|
- `--help` Help me 🙏
|
|
44
44
|
|
|
45
|
+
### Subcommands
|
|
46
|
+
|
|
47
|
+
`list` List all scripts found in local `package.json`.
|
|
48
|
+
|
|
49
|
+
It's possible to also run arbitrary scripts from your `package.json` by passing them as sub-commands, similar to `yarn`.
|
|
50
|
+
|
|
51
|
+
For example: `scriptpal test` will run `npm run test`.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
|
|
45
55
|
## Examples
|
|
46
56
|
|
|
47
57
|
- `$ scriptpal --nowelcome`
|
|
48
58
|
- `$ npx scriptpal`
|
|
49
59
|
- `$ scriptpal --last --preset="emoji"`
|
|
60
|
+
- `$ scriptpal list`
|
|
61
|
+
- `$ scriptpal start`
|
|
50
62
|
|
|
51
63
|
## You might also like...
|
|
52
64
|
|
|
53
65
|
- [CommitPal](https://github.com/zeropoly/commitpal): A delightful CLI tool for building complex commit messages
|
|
54
66
|
- [Enquirer](https://github.com/enquirer/enquirer): Stylish, intuitive and user-friendly prompts
|
|
67
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const { hasFile } = require("./file-manager");
|
|
2
|
+
|
|
3
|
+
function getPackageManager() {
|
|
4
|
+
const packageManagers = [
|
|
5
|
+
{ id: "yarn", file: hasFile("yarn.lock") },
|
|
6
|
+
{ id: "bun", file: hasFile("bun.lockb") },
|
|
7
|
+
{ id: "pnpm", file: hasFile("pnpm-lock.yaml") },
|
|
8
|
+
{ id: "npm", file: hasFile("package-lock.json") },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
let largest = packageManagers[0];
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < packageManagers.length; i++) {
|
|
14
|
+
if (
|
|
15
|
+
packageManagers[i].file &&
|
|
16
|
+
packageManagers[i].file.split("/").length > largest
|
|
17
|
+
) {
|
|
18
|
+
largest = i;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return packageManagers[largest] || "npm";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = { getPackageManager };
|
package/bin/file-manager.js
CHANGED
|
@@ -6,9 +6,7 @@ const findConfig = require("find-config");
|
|
|
6
6
|
|
|
7
7
|
function getPackageJson(path = "package.json") {
|
|
8
8
|
if (!hasFile(path)) {
|
|
9
|
-
throw new Error(`${chalk.bgRed(path)} ${chalk.red(
|
|
10
|
-
"not found"
|
|
11
|
-
)}\n`);
|
|
9
|
+
throw new Error(`${chalk.bgRed(path)} ${chalk.red("not found")}\n`);
|
|
12
10
|
}
|
|
13
11
|
|
|
14
12
|
const configRaw = findConfig.read(path);
|
|
@@ -26,5 +24,5 @@ function hasFile(path) {
|
|
|
26
24
|
|
|
27
25
|
module.exports = {
|
|
28
26
|
getPackageJson,
|
|
29
|
-
hasFile
|
|
27
|
+
hasFile,
|
|
30
28
|
};
|
package/bin/index.js
CHANGED
|
@@ -2,60 +2,42 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
4
|
const { spawnSync } = require("child_process");
|
|
5
|
-
const { AutoComplete, Snippet, Confirm } = require("enquirer");
|
|
6
5
|
const clipboardy = require("clipboardy");
|
|
7
|
-
const meow = require("meow");
|
|
8
6
|
const Conf = require("conf");
|
|
9
7
|
const chalk = require("chalk");
|
|
8
|
+
const { Command, Option, CommanderError } = require("commander");
|
|
10
9
|
|
|
10
|
+
const { version } = require("../package.json");
|
|
11
11
|
const welcome = require("./welcome");
|
|
12
|
-
const { getPackageJson
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
fields: [
|
|
39
|
-
{
|
|
40
|
-
name: "parameters",
|
|
41
|
-
message: "parameters",
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
template: `${script} \${parameters}`,
|
|
45
|
-
}).run();
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
script,
|
|
49
|
-
parameters,
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
async function main(input, flags) {
|
|
12
|
+
const { getPackageJson } = require("./file-manager");
|
|
13
|
+
const { promptShouldRerunPrevious, promptGetCommand } = require("./prompts");
|
|
14
|
+
const { getPackageManager } = require("./detect-pkg-manager");
|
|
15
|
+
|
|
16
|
+
let packageJson;
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
packageJson = getPackageJson();
|
|
20
|
+
|
|
21
|
+
if (!packageJson.scripts) {
|
|
22
|
+
throw new Error(chalk.red('No "scripts" found in package.json'));
|
|
23
|
+
}
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error(error);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function spawnScript(pkgManager, args) {
|
|
30
|
+
const spawn = spawnSync(pkgManager, args, { stdio: "inherit" });
|
|
31
|
+
|
|
32
|
+
if (spawn.error) {
|
|
33
|
+
throw new Error(spawn.error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function main(flags) {
|
|
54
38
|
if (!flags.nowelcome) welcome();
|
|
55
39
|
|
|
56
|
-
const packageJson = getPackageJson();
|
|
57
40
|
const choices = Object.keys(packageJson.scripts);
|
|
58
|
-
|
|
59
41
|
const config = new Conf();
|
|
60
42
|
const previous = config.get(`${process.cwd()}.previous`);
|
|
61
43
|
|
|
@@ -74,68 +56,81 @@ async function main(input, flags) {
|
|
|
74
56
|
? previous
|
|
75
57
|
: await promptGetCommand(choices);
|
|
76
58
|
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
let args = !isYarn ? ["run", script] : [script];
|
|
80
|
-
|
|
59
|
+
const pkgManager = getPackageManager();
|
|
60
|
+
let args = !pkgManager === "npm" ? ["run", script] : [script];
|
|
81
61
|
args = parameters ? [...args, parameters] : args;
|
|
82
62
|
|
|
83
63
|
if (flags.clipboard) {
|
|
84
|
-
await clipboardy.write(`${
|
|
64
|
+
await clipboardy.write(`${pkgManager} ${args.join(" ")}`);
|
|
85
65
|
console.log("Copied to clipboard 👉 📋");
|
|
86
66
|
return 0;
|
|
87
67
|
}
|
|
88
68
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (spawn.error) {
|
|
92
|
-
throw new Error(spawn.error);
|
|
93
|
-
}
|
|
69
|
+
spawnScript(pkgManager, args);
|
|
94
70
|
|
|
95
71
|
config.set(`${process.cwd()}.previous`, { script, parameters });
|
|
96
72
|
}
|
|
97
73
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
74
|
+
async function runLocalScript(script) {
|
|
75
|
+
const pkgManager = getPackageManager();
|
|
76
|
+
const args = !pkgManager === "npm" ? ["run", script] : [script];
|
|
77
|
+
|
|
78
|
+
spawnScript(pkgManager, args);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function list() {
|
|
82
|
+
const packageJson = getPackageJson();
|
|
83
|
+
Object.entries(packageJson.scripts).forEach(([key, value]) => {
|
|
84
|
+
console.log(`· ${chalk.greenBright(key)}: ${value}`);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const program = new Command();
|
|
89
|
+
|
|
90
|
+
program
|
|
91
|
+
.enablePositionalOptions()
|
|
92
|
+
.name("scriptpal")
|
|
93
|
+
.version(version, "-v, --version")
|
|
94
|
+
.option("-l, --last", "Run previous command")
|
|
95
|
+
.option("-c, --clipboard", "Copy command to clipboard")
|
|
96
|
+
.option("-n, --nowelcome", "Omit welcome message")
|
|
97
|
+
.addHelpText(
|
|
98
|
+
"after",
|
|
99
|
+
`
|
|
100
|
+
Examples
|
|
101
|
+
$ scriptpal --last
|
|
102
|
+
$ scriptpal --clipboard
|
|
103
|
+
$ scriptpal --last --clipboard
|
|
104
|
+
$ scriptpal -lcn
|
|
105
|
+
$ scriptpal --nowelcome
|
|
106
|
+
$ npx scriptpal
|
|
107
|
+
$ scriptpal --last --preset="emoji"`
|
|
108
|
+
)
|
|
109
|
+
.action(async (options) => await main(options));
|
|
110
|
+
|
|
111
|
+
program
|
|
112
|
+
.command("list")
|
|
113
|
+
.description("List available scripts from package.json")
|
|
114
|
+
.action(() => list());
|
|
115
|
+
|
|
116
|
+
// console.log(packageJson.scripts);
|
|
117
|
+
Object.keys(packageJson.scripts)
|
|
118
|
+
.filter((script) => !["list"].includes(script))
|
|
119
|
+
.forEach((script) => {
|
|
120
|
+
program
|
|
121
|
+
.usage("[global options] <file-paths>...")
|
|
122
|
+
.command(script)
|
|
123
|
+
.description(
|
|
124
|
+
`Runs local script "${script}" detected in local package.json`
|
|
125
|
+
)
|
|
126
|
+
.action(() => runLocalScript(script));
|
|
127
|
+
});
|
|
132
128
|
|
|
133
129
|
(async () => {
|
|
134
130
|
try {
|
|
135
|
-
await
|
|
131
|
+
await program.parseAsync(process.argv);
|
|
136
132
|
} catch (error) {
|
|
137
133
|
console.error(chalk.red(error));
|
|
138
|
-
console.log(error);
|
|
139
134
|
process.exit(1);
|
|
140
135
|
}
|
|
141
136
|
})();
|
package/bin/main.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const clipboardy = require("clipboardy");
|
|
3
|
+
const Conf = require("conf");
|
|
4
|
+
const welcome = require("./welcome");
|
|
5
|
+
const { promptShouldRerunPrevious, promptGetCommand } = require("./prompts");
|
|
6
|
+
const { getPackageManager } = require("./detect-pkg-manager");
|
|
7
|
+
const { packageJson } = require(".");
|
|
8
|
+
|
|
9
|
+
async function main(flags) {
|
|
10
|
+
if (!flags.nowelcome) welcome();
|
|
11
|
+
|
|
12
|
+
const choices = Object.keys(packageJson.scripts);
|
|
13
|
+
|
|
14
|
+
const config = new Conf();
|
|
15
|
+
const previous = config.get(`${process.cwd()}.previous`);
|
|
16
|
+
|
|
17
|
+
let shouldRerunPrevious = false;
|
|
18
|
+
|
|
19
|
+
if (!previous && flags.last) {
|
|
20
|
+
console.log("Previous command not found, continuing...\n");
|
|
21
|
+
} else if (previous && !flags.last) {
|
|
22
|
+
shouldRerunPrevious = await promptShouldRerunPrevious(previous);
|
|
23
|
+
} else {
|
|
24
|
+
shouldRerunPrevious = true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { script, parameters } =
|
|
28
|
+
(previous || flags.last) && shouldRerunPrevious
|
|
29
|
+
? previous
|
|
30
|
+
: await promptGetCommand(choices);
|
|
31
|
+
|
|
32
|
+
const pkgManager = getPackageManager();
|
|
33
|
+
let args = !pkgManager === "npm" ? ["run", script] : [script];
|
|
34
|
+
args = parameters ? [...args, parameters] : args;
|
|
35
|
+
|
|
36
|
+
if (flags.clipboard) {
|
|
37
|
+
await clipboardy.write(`${pkgManager} ${args.join(" ")}`);
|
|
38
|
+
console.log("Copied to clipboard 👉 📋");
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// spawnScript(pkgManager, args);
|
|
43
|
+
config.set(`${process.cwd()}.previous`, { script, parameters });
|
|
44
|
+
}
|
|
45
|
+
exports.main = main;
|
package/bin/prompts.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const chalk = require("chalk");
|
|
2
|
+
const { AutoComplete, Snippet, Confirm } = require("enquirer");
|
|
3
|
+
|
|
4
|
+
const promptShouldRerunPrevious = async (previous) => {
|
|
5
|
+
const previousCommand = `${previous.script} ${
|
|
6
|
+
previous.parameters || ""
|
|
7
|
+
}`.trim();
|
|
8
|
+
|
|
9
|
+
return await new Confirm({
|
|
10
|
+
message: `Would you like to rerun the previous command?\n${chalk.greenBright(
|
|
11
|
+
previousCommand
|
|
12
|
+
)}`,
|
|
13
|
+
}).run();
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const promptGetCommand = async (choices) => {
|
|
17
|
+
const script = await new AutoComplete({
|
|
18
|
+
message: "Which script would you like to run? 🤷♂️",
|
|
19
|
+
limit: 18,
|
|
20
|
+
choices,
|
|
21
|
+
}).run();
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
values: { parameters },
|
|
25
|
+
} = await new Snippet({
|
|
26
|
+
message: "Would you like to add parameters?",
|
|
27
|
+
required: false,
|
|
28
|
+
fields: [
|
|
29
|
+
{
|
|
30
|
+
name: "parameters",
|
|
31
|
+
message: "parameters",
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
template: `${script} \${parameters}`,
|
|
35
|
+
}).run();
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
script,
|
|
39
|
+
parameters,
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
module.exports = {
|
|
44
|
+
promptShouldRerunPrevious,
|
|
45
|
+
promptGetCommand,
|
|
46
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scriptpal",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "A simple npm script palette for lazy people (like me)",
|
|
5
5
|
"main": "./bin/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"scriptpal": "./bin/index.js"
|
|
7
|
+
"scriptpal": "./bin/index.js",
|
|
8
|
+
"spal": "./bin/index.js"
|
|
8
9
|
},
|
|
9
10
|
"scripts": {
|
|
10
11
|
"start": "node ./bin/index",
|
|
@@ -29,10 +30,10 @@
|
|
|
29
30
|
"dependencies": {
|
|
30
31
|
"chalk": "^3.0.0",
|
|
31
32
|
"clipboardy": "^2.1.0",
|
|
33
|
+
"commander": "^8.2.0",
|
|
32
34
|
"conf": "^6.2.1",
|
|
33
35
|
"enquirer": "^2.3.2",
|
|
34
36
|
"find-config": "^1.0.0",
|
|
35
|
-
"gradient-string": "^1.2.0"
|
|
36
|
-
"meow": "^6.0.0"
|
|
37
|
+
"gradient-string": "^1.2.0"
|
|
37
38
|
}
|
|
38
39
|
}
|