@gutenye/script.js 1.0.0 → 1.0.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2024 Guten Ye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,35 +1,95 @@
1
- # 🌟 Script.js 🌟
1
+ # 🧩 Script.js 🧩
2
2
 
3
- [![Stars](https://img.shields.io/github/stars/gutenye/script.js?style=social)](https://github.com/gutenye/script.js) [![NPM Version](https://img.shields.io/npm/v/@gutenye/script.js)](https://www.npmjs.com/package/@gutenye/script.js) [![License](https://img.shields.io/github/license/gutenye/script.js?color=blue)](https://github.com/gutenye/script.js/blob/main/LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-blue)](https://github.com/gutenye/script.js#-contribute)
4
-
5
- Write shell scripts in JavaScript
3
+ **Write shell scripts in JavaScript** and leverage the power of JavaScript’s simplicity and flexibility to make your shell scripting easier and faster. Why struggle with the complexity of Bash or other shell scripting languages when you already know JavaScript? Script.js allows you to write and run complex shell scripts using JavaScript, saving you time and reducing errors.
6
4
 
7
5
  **Show your ❤️ and support by starring this project and following the author, [Guten Ye](https://github.com/gutenye)!**
8
6
 
7
+ [![Stars](https://img.shields.io/github/stars/gutenye/script.js?style=social)](https://github.com/gutenye/script.js) [![NPM Version](https://img.shields.io/npm/v/@gutenye/script.js)](https://www.npmjs.com/package/@gutenye/script.js) [![License](https://img.shields.io/github/license/gutenye/script.js?color=blue)](https://github.com/gutenye/script.js/blob/main/LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-blue)](https://github.com/gutenye/script.js#-contribute)
8
+
9
9
  ## 🌟 Features
10
10
 
11
- - **Javascript**: ..
12
- - **Autocomplete**: ..
13
- - **Fast**: ..
11
+ - **Fast to write**: The most important factor in script writing is **speed**. With Script.js, you can quickly open the editor, write a few lines of code, and run it instantly. Thanks to global commands, you don’t need to worry about import statements, and more. Writing a script is enjoyable and efficient.
12
+ - **Fun to run**: A major benefit of using a script is **autocompletion**. Script.js includes built-in autocompletion support. Running a script is fun and seamless.
13
+ - **JavaScript Power**: Writing complex scripts in JavaScript and access the entire JavaScript ecosystem.
14
+ - **Familiar Syntax**: Write your shell commands just like you would in Bash, with full support for redirection, pipes, environment variables, and more.
15
+ - **Subcommands**: Create and organize subcommands effortlessly.
16
+ - **Help Documentation**: Auto-generate command help documentation for better user experience.
17
+ - **Fast Execution**: Fast to execute your scripts.
18
+
19
+ ## 🚀 Getting Started
20
+
21
+ ### 1️⃣ Install
22
+
23
+ First, make sure [Bun](https://bun.sh) and [Carapace](https://github.com/carapace-sh/carapace-bin) are installed.
24
+
25
+ To install Script.js, run:
26
+
27
+ ```sh
28
+ npm install -g @gutenye/script.js
29
+ ```
30
+
31
+ ### 2️⃣ Write Your First Script
32
+
33
+ Create a file named `hello.js` and add the following code:
34
+
35
+ ```ts
36
+ #!/usr/bin/env script.js
37
+
38
+ app
39
+ .name('hello.js')
40
+ .enableCompletion()
41
+
42
+ app.command('greetings [files...]')
43
+ .completion({
44
+ positionalany: ['$files'],
45
+ })
46
+ .action((files, options) => {
47
+ $`
48
+ ls -l ${files}
49
+ echo ${files} | wc -l
50
+ `
51
+ })
52
+ ```
53
+
54
+ ### 3️⃣ Run the script
55
+
56
+ You can use `<Tab>` to autocomplete arguments or options while using the script.
57
+
58
+ ```sh
59
+ chmod +x hello.js # Make the script executable
60
+ ./hello.js # First run to create completion file
61
+ ./hello.js <Tab> # Use Tab key for autocompletion
62
+ ```
14
63
 
15
64
  ## 📖 Documentation
16
65
 
17
- - [Getting Started](./docs/Getting%20Started.md)
18
- - [Completion](./docs/Completion.md)
66
+ - [Create Commands](./docs/Create%20Commands.md): Create commands and subcommands
67
+ - [Completion](./docs/Completion.md): Customize the autocompletion
68
+ - [Global Commands](./docs/Global%20Commands.md): List of global variables and commands
69
+
70
+ ## 🙇 Thanks
71
+
72
+ - [Bun](https://github.com/oven-sh/bun): Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
73
+ - [Carapace](https://github.com/carapace-sh/carapace-bin): A multi-shell completion binary
74
+ - [Zurk](https://github.com/webpod/zurk): A generic process spawner
75
+ - [Zx](https://github.com/google/zx): A tool for writing better scripts
76
+ - [Commander.js](https://github.com/tj/commander.js): node.js command-line interfaces made easy
19
77
 
20
78
  ## 🤝 Contribute
21
79
 
22
- We welcome contributions from the community! Whether its reporting bugs, suggesting features, or submitting pull requests, your help is appreciated.
80
+ We love contributions! Whether youre fixing bugs, adding features, or improving documentation, your involvement makes this project better.
81
+
82
+ **How to Contribute:**
23
83
 
24
84
  1. Fork the Repository
25
85
  2. Open a Pull Request on Github
26
86
 
27
87
  ---
28
88
 
29
- Thank you for using Script.js! 🔐 ✨ If you found it helpful, please ⭐️ star the project ️️⭐ on GitHub. If you have any questions or encounter issues, please refer to the documentation or report an issue on GitHub.
89
+ Thank you for using Script.js! If you found it helpful, please ⭐️ star the project ️️⭐ on GitHub. If you have any questions, encounter issues, please refer to the documentation or report an issue on GitHub.
30
90
 
31
- **Thanks to all the people who contribute:**
91
+ **Special Thanks to All Contributors:**
32
92
 
33
93
  [![](https://contrib.rocks/image?repo=gutenye/script.js)](https://github.com/gutenye/script.js/graphs/contributors)
34
94
 
35
- [⬆ Back to top ⬆](#readme)
95
+ [⬆ Back to top ⬆](#readme)
package/package.json CHANGED
@@ -1,19 +1,12 @@
1
1
  {
2
2
  "name": "@gutenye/script.js",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Write shell scripts in JavaScript",
5
5
  "keywords": [
6
6
  "shell",
7
7
  "script",
8
8
  "bash",
9
- "bin",
10
- "binary",
11
- "child",
12
- "process",
13
9
  "exec",
14
- "execute",
15
- "invoke",
16
- "call",
17
10
  "spawn",
18
11
  "zx",
19
12
  "bunshell"
@@ -28,19 +21,18 @@
28
21
  "!**/__tests__"
29
22
  ],
30
23
  "bin": {
31
- "gutenye-script.js": "src/script.ts"
24
+ "script.js": "src/script.ts"
32
25
  },
33
26
  "scripts": {
34
27
  "test": "bun test",
35
28
  "lint": "biome check --fix",
36
- "lint:ci": "biome ci --reporter=github",
37
- "build": "rm -rf build; tsc --project tsconfig.build.json; tsc-alias --project tsconfig.build.json"
29
+ "lint:ci": "biome ci --reporter=github"
38
30
  },
39
31
  "publishConfig": {
40
32
  "access": "public"
41
33
  },
42
34
  "dependencies": {
43
- "@gutenye/commander-completion-carapace": "^1.0.3",
35
+ "@gutenye/commander-completion-carapace": "^1.0.9",
44
36
  "chalk": "^5.3.0",
45
37
  "commander": "^12.1.0",
46
38
  "csv-parse": "^5.5.6",
@@ -54,7 +46,7 @@
54
46
  "typescript": "^5.7.2"
55
47
  },
56
48
  "devDependencies": {
57
- "@biomejs/biome": "1.9.4",
49
+ "@biomejs/biome": "^1.9.4",
58
50
  "@semantic-release/changelog": "^6.0.3",
59
51
  "@semantic-release/git": "^10.0.1",
60
52
  "@types/bun": "latest",
package/src/app.ts CHANGED
@@ -9,7 +9,7 @@ app.Option = Option
9
9
 
10
10
  export async function start() {
11
11
  if (!scriptPath) {
12
- echo('Error: missing script path, usage: gutenye-script.js <script>')
12
+ echo('Error: missing script path, usage: script.js <script>')
13
13
  process.exit(1)
14
14
  }
15
15
 
package/src/command.ts CHANGED
@@ -16,7 +16,7 @@ export class Command extends BaseCommand {
16
16
  }
17
17
  }
18
18
 
19
- actionLinux(fn) {
19
+ actionLin(fn) {
20
20
  this._actionLinux = fn
21
21
  this._actionOS()
22
22
  return this
package/src/fileSystem.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { fs } from '#/utils'
1
+ import { fs, path } from '#/utils'
2
2
 
3
- export { fs }
3
+ export { fs, path }
4
4
 
5
5
  export const cp = fs.copy
6
6
 
package/src/script.ts CHANGED
@@ -4,7 +4,7 @@ import 'zx/globals'
4
4
 
5
5
  // Variables
6
6
  globalThis.HOME = os.homedir()
7
- globalThis.PWD = process.cwd()
7
+ globalThis.CWD = process.cwd()
8
8
  globalThis.ENV = process.env
9
9
 
10
10
  // App
@@ -25,11 +25,10 @@ globalThis.$l = $l
25
25
  import { exitWithError } from './exit'
26
26
  globalThis.exitWithError = exitWithError
27
27
 
28
- globalThis.mixins = mixins
29
-
30
28
  // Filesystem
31
- import { fs, cp, ls, mkdir, mv, rm } from './fileSystem'
29
+ import { fs, path, cp, ls, mkdir, mv, rm } from './fileSystem'
32
30
  globalThis.fs = fs
31
+ globalThis.path = path
33
32
  globalThis.cp = cp
34
33
  globalThis.mv = mv
35
34
  globalThis.rm = rm
@@ -50,4 +49,7 @@ globalThis.colors = colors
50
49
  import * as csv from './csv'
51
50
  globalThis.csv = csv
52
51
 
52
+ import * as yaml from './yaml'
53
+ globalThis.yaml = yaml
54
+
53
55
  start()
@@ -1 +1,2 @@
1
1
  export { default as fs } from './fs'
2
+ export { default as path } from './path'
@@ -0,0 +1,54 @@
1
+ import nodePath from 'node:path'
2
+ import fs from './fs'
3
+
4
+ // suffix name
5
+ // a.js -> a-suffix.js
6
+ function nameAddSuffix(path: string, suffix: string) {
7
+ const { dir, name, ext } = nodePath.parse(path)
8
+ return nodePath.join(dir, `${name}${suffix}${ext}`)
9
+ }
10
+
11
+ // replace name
12
+ // a.js -> name.js
13
+ function replaceName(path: string, name: string) {
14
+ const { dir, ext } = nodePath.parse(path)
15
+ return nodePath.join(dir, `${name}${ext}`)
16
+ }
17
+
18
+ // replace ext
19
+ // a.js -> a.py
20
+ function replaceExt(path: string, newExt: string) {
21
+ const { dir, name } = nodePath.parse(path)
22
+ return nodePath.join(dir, `${name}.${newExt}`)
23
+ }
24
+
25
+ /**
26
+ * If path exists, return a new path with a number appended to it.
27
+ */
28
+ async function genUniquePath(path: string): Promise<string> {
29
+ const { dir, name, ext } = nodePath.parse(path)
30
+ let newPath = path
31
+ let index = 1
32
+ while (await fs.pathExists(newPath)) {
33
+ newPath = nodePath.join(dir, `${name} (${index})${ext}`)
34
+ index++
35
+ }
36
+ return newPath
37
+ }
38
+
39
+ /*
40
+ * Check path has ext
41
+ */
42
+ function hasExt(path: string, exts: string[]) {
43
+ const ext = nodePath.extname(path).slice(1)
44
+ return exts.includes(ext)
45
+ }
46
+
47
+ export default {
48
+ ...nodePath,
49
+ nameAddSuffix,
50
+ replaceName,
51
+ replaceExt,
52
+ genUniquePath,
53
+ hasExt,
54
+ }
package/src/yaml.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { merge as mergeObject } from 'lodash-es'
2
+ import Yaml from 'yaml'
3
+ import { fs } from '#/utils'
4
+
5
+ export const parse = Yaml.parse
6
+
7
+ export const stringify = Yaml.stringify
8
+
9
+ export async function readFile(path: string) {
10
+ const text = (await fs.inputFile(path, 'utf8')) as string
11
+ if (!text) {
12
+ throw new Error(`input file not found: ${path}`)
13
+ }
14
+ return Yaml.parse(text)
15
+ }
16
+
17
+ export async function writeFile(path: string, data: any) {
18
+ const text = Yaml.stringify(data)
19
+ await fs.outputFile(path, text)
20
+ }
21
+
22
+ export async function mergeFiles(inputPaths: string[], outputPath: string) {
23
+ const outputObject = {}
24
+ for (const inputPath of inputPaths) {
25
+ const text = (await fs.inputFile(inputPath, 'utf8')) as string
26
+ if (!text) {
27
+ throw new Error(`input file not found: ${inputPath}`)
28
+ }
29
+ const object = Yaml.parse(text)
30
+ mergeObject(outputObject, object)
31
+ }
32
+ const outputText = Yaml.stringify(outputObject)
33
+ await fs.outputFile(outputPath, outputText)
34
+ }