lscripts 1.5.0 → 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.
Files changed (2) hide show
  1. package/dist/main.js +124 -0
  2. package/package.json +1 -1
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,7 +1,7 @@
1
1
  {
2
2
  "type": "commonjs",
3
3
  "name": "lscripts",
4
- "version": "1.5.0",
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
6
  "main": "./dist/main.js",
7
7
  "exports": {