lscripts 1.4.2 → 1.5.1
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/dist/main.js +124 -0
- package/package.json +13 -9
- package/lscripts.js +0 -135
package/dist/main.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const search_1 = __importDefault(require("@inquirer/search"));
|
|
8
|
+
const clipboardy_1 = __importDefault(require("clipboardy"));
|
|
9
|
+
const node_fs_1 = require("node:fs");
|
|
10
|
+
const node_process_1 = require("node:process");
|
|
11
|
+
const node_child_process_1 = require("node:child_process");
|
|
12
|
+
const pkg = require('../package.json');
|
|
13
|
+
const rootDir = '/';
|
|
14
|
+
let isCopyModeEnabled = false;
|
|
15
|
+
const useCopyMode = () => {
|
|
16
|
+
isCopyModeEnabled = true;
|
|
17
|
+
};
|
|
18
|
+
const displayHelpMessage = () => {
|
|
19
|
+
console.log(`Usage: lscripts [option]
|
|
20
|
+
Simply list and run a script form the package.json of your current node project.
|
|
21
|
+
|
|
22
|
+
option alias description
|
|
23
|
+
----------- ------- -----------------------------------------------------------------
|
|
24
|
+
--copy -c Enables copy mode: when you pick your script, it will be copied
|
|
25
|
+
to your clipboard instead of being ran immediately
|
|
26
|
+
--version -v Displays your installed version of lscripts
|
|
27
|
+
--help -h Displays this help message
|
|
28
|
+
|
|
29
|
+
You can only pass one argument at a time to lscripts command.`);
|
|
30
|
+
(0, node_process_1.exit)(0);
|
|
31
|
+
};
|
|
32
|
+
const displayVersion = () => {
|
|
33
|
+
console.log(`You are currently using lscripts version ${pkg.version}`);
|
|
34
|
+
(0, node_process_1.exit)(0);
|
|
35
|
+
};
|
|
36
|
+
const possibleFlags = [
|
|
37
|
+
{ flags: ['--copy', '-c'], callback: useCopyMode },
|
|
38
|
+
{ flags: ['--help', '-h'], callback: displayHelpMessage },
|
|
39
|
+
{ flags: ['--version', '-v'], callback: displayVersion }
|
|
40
|
+
];
|
|
41
|
+
const parseArguments = () => {
|
|
42
|
+
if (node_process_1.argv.length < 3)
|
|
43
|
+
return;
|
|
44
|
+
if (node_process_1.argv.length > 3) {
|
|
45
|
+
console.log('You can only pass one argument at a time to lscripts.');
|
|
46
|
+
console.log(`Run 'lscripts --help' for more information.`);
|
|
47
|
+
return (0, node_process_1.exit)(1);
|
|
48
|
+
}
|
|
49
|
+
const arg = node_process_1.argv[2];
|
|
50
|
+
const flag = possibleFlags.find(({ flags }) => flags.includes(arg));
|
|
51
|
+
if (!flag) {
|
|
52
|
+
console.log(`Unkown argument "${arg}".`);
|
|
53
|
+
console.log(`Run 'lscripts --help' for more information.`);
|
|
54
|
+
return (0, node_process_1.exit)(1);
|
|
55
|
+
}
|
|
56
|
+
flag.callback();
|
|
57
|
+
};
|
|
58
|
+
const findPackageJsonInWorkingDirOrParent = (currentDir = './') => {
|
|
59
|
+
if ((0, node_fs_1.existsSync)(`${currentDir}package.json`))
|
|
60
|
+
return JSON.parse((0, node_fs_1.readFileSync)(`${currentDir}package.json`));
|
|
61
|
+
if ((0, node_fs_1.realpathSync)(currentDir) === rootDir)
|
|
62
|
+
return null;
|
|
63
|
+
return findPackageJsonInWorkingDirOrParent(`${currentDir}../`);
|
|
64
|
+
};
|
|
65
|
+
const getScriptsFromPackageJson = (packageJson) => Object.entries(packageJson.scripts);
|
|
66
|
+
const getPackageManagerEngine = (currentDir = './') => {
|
|
67
|
+
if ((0, node_fs_1.existsSync)(`${currentDir}package-lock.json`))
|
|
68
|
+
return 'npm';
|
|
69
|
+
if ((0, node_fs_1.existsSync)(`${currentDir}yarn.lock`))
|
|
70
|
+
return 'yarn';
|
|
71
|
+
if ((0, node_fs_1.existsSync)(`${currentDir}pnpm-lock.yaml`))
|
|
72
|
+
return 'pnpm';
|
|
73
|
+
if ((0, node_fs_1.realpathSync)(currentDir) === rootDir)
|
|
74
|
+
return null;
|
|
75
|
+
return getPackageManagerEngine((`${currentDir}../`));
|
|
76
|
+
};
|
|
77
|
+
parseArguments();
|
|
78
|
+
const packageJson = findPackageJsonInWorkingDirOrParent();
|
|
79
|
+
if (!packageJson) {
|
|
80
|
+
console.log('No package.json was found in current directory or parent directories.');
|
|
81
|
+
console.log('Make sure you are in a NodeJS project before running lscripts.');
|
|
82
|
+
(0, node_process_1.exit)(1);
|
|
83
|
+
}
|
|
84
|
+
const scripts = getScriptsFromPackageJson(packageJson);
|
|
85
|
+
const runner = getPackageManagerEngine();
|
|
86
|
+
if (!runner) {
|
|
87
|
+
console.log('Please install your dependencies at least once before using lscripts.');
|
|
88
|
+
console.log('lscripts uses the lock file to determine which package manager you are using.');
|
|
89
|
+
console.log('lscripts is currently only compatible with npm, yarn and pnpm.');
|
|
90
|
+
(0, node_process_1.exit)(1);
|
|
91
|
+
}
|
|
92
|
+
const regex = /^(.+) - /;
|
|
93
|
+
process.on('uncaughtException', (error) => {
|
|
94
|
+
if (error instanceof Error && error.name === 'ExitPromptError')
|
|
95
|
+
console.log('👋 until next time!');
|
|
96
|
+
else
|
|
97
|
+
throw error;
|
|
98
|
+
});
|
|
99
|
+
const source = async (input) => {
|
|
100
|
+
return scripts.reduce((acc, [name, command]) => {
|
|
101
|
+
const script = `${name} - "${command}"`;
|
|
102
|
+
if (input === undefined || script.includes(input))
|
|
103
|
+
acc.push(script);
|
|
104
|
+
return acc;
|
|
105
|
+
}, []);
|
|
106
|
+
};
|
|
107
|
+
(0, search_1.default)({
|
|
108
|
+
message: isCopyModeEnabled
|
|
109
|
+
? 'What script do you want to copy to your clipboard ?'
|
|
110
|
+
: 'What script do you want to run ?',
|
|
111
|
+
pageSize: scripts.length,
|
|
112
|
+
source
|
|
113
|
+
}).then((script) => {
|
|
114
|
+
const command = `${runner} run ${script.match(regex)[1]}`;
|
|
115
|
+
if (isCopyModeEnabled) {
|
|
116
|
+
clipboardy_1.default.writeSync(command);
|
|
117
|
+
console.log('✅ Command copied to your clipboard!');
|
|
118
|
+
console.log('You can paste it and run it!');
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
console.log(`🚀 ${command}`);
|
|
122
|
+
(0, node_child_process_1.spawn)(command, { stdio: 'inherit', shell: true })
|
|
123
|
+
.on('exit', process.exit);
|
|
124
|
+
});
|
package/package.json
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "
|
|
2
|
+
"type": "commonjs",
|
|
3
3
|
"name": "lscripts",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.1",
|
|
5
5
|
"description": "lscripts is a command tool that allows you to list and choose a script from your package.json easely.",
|
|
6
|
-
"main": "
|
|
6
|
+
"main": "./dist/main.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
"require": "./dist/main.js"
|
|
9
|
+
},
|
|
7
10
|
"bin": {
|
|
8
|
-
"lscripts": "
|
|
11
|
+
"lscripts": "./dist/main.js"
|
|
9
12
|
},
|
|
10
13
|
"files": [
|
|
11
|
-
"
|
|
14
|
+
"dist/*",
|
|
12
15
|
"package.json"
|
|
13
16
|
],
|
|
14
17
|
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
15
19
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
16
20
|
"prepare": "husky"
|
|
17
21
|
},
|
|
@@ -26,14 +30,14 @@
|
|
|
26
30
|
"cli"
|
|
27
31
|
],
|
|
28
32
|
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"inquirer-autocomplete-prompt": "^3.0.0"
|
|
33
|
+
"@inquirer/search": "^3.1.1",
|
|
34
|
+
"clipboardy": "^4.0.0"
|
|
32
35
|
},
|
|
33
36
|
"devDependencies": {
|
|
34
37
|
"@commitlint/cli": "^19.8.1",
|
|
35
38
|
"@commitlint/config-conventional": "^19.8.1",
|
|
36
39
|
"husky": "^9.1.7",
|
|
37
|
-
"semantic-release": "^24.2.7"
|
|
40
|
+
"semantic-release": "^24.2.7",
|
|
41
|
+
"typescript": "^5.9.2"
|
|
38
42
|
}
|
|
39
43
|
}
|
package/lscripts.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import inquirer from 'inquirer'
|
|
4
|
-
import inquirerPrompt from 'inquirer-autocomplete-prompt'
|
|
5
|
-
import fs from 'fs'
|
|
6
|
-
import { exit, argv } from 'process'
|
|
7
|
-
import { spawn } from 'child_process'
|
|
8
|
-
import clipboard from 'clipboardy'
|
|
9
|
-
import lscriptsPackageJson from './package.json' with { type: "json" }
|
|
10
|
-
|
|
11
|
-
const rootDir = '/'
|
|
12
|
-
|
|
13
|
-
let isCopyModeEnabled = false
|
|
14
|
-
const useCopyMode = () => {
|
|
15
|
-
isCopyModeEnabled = true
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const displayHelpMessage = () => {
|
|
19
|
-
console.log(`Usage: lscripts [option]
|
|
20
|
-
Simply list and run a script form the package.json of your current node project.
|
|
21
|
-
|
|
22
|
-
option alias description
|
|
23
|
-
----------- ------- -----------------------------------------------------------------
|
|
24
|
-
--copy -c Enables copy mode: when you pick your script, it will be copied
|
|
25
|
-
to your clipboard instead of being ran immediately
|
|
26
|
-
--version -v Displays your installed version of lscripts
|
|
27
|
-
--help -h Displays this help message
|
|
28
|
-
|
|
29
|
-
You can only pass one argument at a time to lscripts command.`)
|
|
30
|
-
exit(0)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const displayVersion = () => {
|
|
34
|
-
console.log(`You are currently using lscripts version ${lscriptsPackageJson.version}`)
|
|
35
|
-
exit(0)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const possibleFlags = [
|
|
39
|
-
{ flags: ['--copy', '-c'], callback: useCopyMode },
|
|
40
|
-
{ flags: ['--help', '-h'], callback: displayHelpMessage },
|
|
41
|
-
{ flags: ['--version', '-v'], callback: displayVersion }
|
|
42
|
-
]
|
|
43
|
-
|
|
44
|
-
const parseArguments = () => {
|
|
45
|
-
if (argv.length < 3) return
|
|
46
|
-
if (argv.length > 3) {
|
|
47
|
-
console.log('You can only pass one argument at a time to lscripts.')
|
|
48
|
-
console.log(`Run 'lscripts --help' for more information.`)
|
|
49
|
-
return exit(1)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const arg = argv[2]
|
|
53
|
-
const flag = possibleFlags.find(({ flags }) => flags.includes(arg))
|
|
54
|
-
|
|
55
|
-
if (!flag) {
|
|
56
|
-
console.log(`Unkown argument "${arg}".`)
|
|
57
|
-
console.log(`Run 'lscripts --help' for more information.`)
|
|
58
|
-
return exit(1)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
flag.callback()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const findPackageJsonInWorkingDirOrParent = (currentDir = './') => {
|
|
65
|
-
if (fs.existsSync(`${currentDir}package.json`)) return JSON.parse(fs.readFileSync(`${currentDir}package.json`))
|
|
66
|
-
if (fs.realpathSync(currentDir) === rootDir) return null
|
|
67
|
-
return findPackageJsonInWorkingDirOrParent(`${currentDir}../`)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const getScriptsFromPackageJson = (packageJson) => Object.entries(packageJson.scripts)
|
|
71
|
-
|
|
72
|
-
const getPackageManagerEngine = (currentDir = './') => {
|
|
73
|
-
if (fs.existsSync(`${currentDir}package-lock.json`)) return 'npm'
|
|
74
|
-
if (fs.existsSync(`${currentDir}yarn.lock`)) return 'yarn'
|
|
75
|
-
if (fs.existsSync(`${currentDir}pnpm-lock.yaml`)) return 'pnpm'
|
|
76
|
-
if (fs.realpathSync(currentDir) === rootDir) return null
|
|
77
|
-
return getPackageManagerEngine((`${currentDir}../`))
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
parseArguments()
|
|
81
|
-
|
|
82
|
-
const packageJson = findPackageJsonInWorkingDirOrParent()
|
|
83
|
-
|
|
84
|
-
if (!packageJson) {
|
|
85
|
-
console.log('No package.json was found in current directory or parent directories.')
|
|
86
|
-
console.log('Make sure you are in a NodeJS project before running lscripts.')
|
|
87
|
-
exit(1)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const scripts = getScriptsFromPackageJson(packageJson)
|
|
91
|
-
|
|
92
|
-
const runner = getPackageManagerEngine()
|
|
93
|
-
|
|
94
|
-
if (!runner) {
|
|
95
|
-
console.log('Please install your dependencies at least once before using lscripts.')
|
|
96
|
-
console.log('lscripts uses the lock file to determine which package manager you are using.')
|
|
97
|
-
console.log('lscripts is currently only compatible with npm, yarn and pnpm.')
|
|
98
|
-
exit(1)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const regex = /^(.+) - /
|
|
102
|
-
|
|
103
|
-
const source = async (_, input) => {
|
|
104
|
-
return scripts.reduce((acc, [name, command]) => {
|
|
105
|
-
const script = `${name} - "${command}"`
|
|
106
|
-
if (script.includes(input) || input === undefined) acc.push(script)
|
|
107
|
-
return acc
|
|
108
|
-
}, [])
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
inquirer.registerPrompt('autocomplete', inquirerPrompt)
|
|
112
|
-
inquirer.prompt({
|
|
113
|
-
type: 'autocomplete',
|
|
114
|
-
name: 'script',
|
|
115
|
-
message: isCopyModeEnabled
|
|
116
|
-
? 'What script do you want to copy to your clipboard ?'
|
|
117
|
-
: 'What script do you want to run ?',
|
|
118
|
-
emptyText: `Sorry, I couldn't find the script you are looking for...`,
|
|
119
|
-
searchText: 'Looking for the script...',
|
|
120
|
-
pageSize: scripts.length,
|
|
121
|
-
source
|
|
122
|
-
}).then(({ script }) => {
|
|
123
|
-
const command = `${runner} run ${script.match(regex)[1]}`
|
|
124
|
-
|
|
125
|
-
if (isCopyModeEnabled) {
|
|
126
|
-
clipboard.writeSync(command)
|
|
127
|
-
console.log('✅ Command copied to your clipboard!')
|
|
128
|
-
console.log('You can paste it and run it!')
|
|
129
|
-
return
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
console.log(`🚀 ${command}`)
|
|
133
|
-
spawn(command, { stdio: 'inherit', shell: true })
|
|
134
|
-
.on('exit', process.exit)
|
|
135
|
-
})
|