lscripts 1.3.0 → 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.
Files changed (2) hide show
  1. package/lscripts.js +73 -13
  2. package/package.json +2 -1
package/lscripts.js CHANGED
@@ -3,19 +3,71 @@
3
3
  import inquirer from 'inquirer'
4
4
  import inquirerPrompt from 'inquirer-autocomplete-prompt'
5
5
  import fs from 'fs'
6
- import { exit } from 'process'
6
+ import { exit, argv } from 'process'
7
7
  import { spawn } from 'child_process'
8
+ import clipboard from 'clipboardy'
9
+ import lscriptsPackageJson from './package.json' with { type: "json" }
8
10
 
9
11
  const rootDir = '/'
10
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
+
11
64
  const findPackageJsonInWorkingDirOrParent = (currentDir = './') => {
12
- if (fs.existsSync(`${currentDir}package.json`)) return fs.readFileSync(`${currentDir}package.json`)
65
+ if (fs.existsSync(`${currentDir}package.json`)) return JSON.parse(fs.readFileSync(`${currentDir}package.json`))
13
66
  if (fs.realpathSync(currentDir) === rootDir) return null
14
67
  return findPackageJsonInWorkingDirOrParent(`${currentDir}../`)
15
68
  }
16
69
 
17
- const getScriptsFromPackageJson = (packageJson) =>
18
- Object.entries(JSON.parse(packageJson).scripts)
70
+ const getScriptsFromPackageJson = (packageJson) => Object.entries(packageJson.scripts)
19
71
 
20
72
  const getPackageManagerEngine = (currentDir = './') => {
21
73
  if (fs.existsSync(`${currentDir}package-lock.json`)) return 'npm'
@@ -25,6 +77,8 @@ const getPackageManagerEngine = (currentDir = './') => {
25
77
  return getPackageManagerEngine((`${currentDir}../`))
26
78
  }
27
79
 
80
+ parseArguments()
81
+
28
82
  const packageJson = findPackageJsonInWorkingDirOrParent()
29
83
 
30
84
  if (!packageJson) {
@@ -58,18 +112,24 @@ inquirer.registerPrompt('autocomplete', inquirerPrompt)
58
112
  inquirer.prompt({
59
113
  type: 'autocomplete',
60
114
  name: 'script',
61
- message: 'What script do you want to copy to your clipboard ?',
115
+ message: isCopyModeEnabled
116
+ ? 'What script do you want to copy to your clipboard ?'
117
+ : 'What script do you want to run ?',
62
118
  emptyText: `Sorry, I couldn't find the script you are looking for...`,
63
119
  searchText: 'Looking for the script...',
64
120
  pageSize: scripts.length,
65
121
  source
66
122
  }).then(({ script }) => {
67
- const child = spawn(runner, ['run', script.match(regex)[1]], {
68
- stdio: 'inherit',
69
- shell: true
70
- })
71
-
72
- child.on('exit', (code) => {
73
- process.exit(code);
74
- })
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)
75
135
  })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "lscripts",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
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": "lscripts.js",
7
7
  "bin": {
@@ -26,6 +26,7 @@
26
26
  "cli"
27
27
  ],
28
28
  "dependencies": {
29
+ "clipboardy": "^4.0.0",
29
30
  "inquirer": "^9.1.4",
30
31
  "inquirer-autocomplete-prompt": "^3.0.0"
31
32
  },