yavascript 0.0.12 → 0.0.13
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 +124 -58
- package/bin/aarch64-apple-darwin/yavascript +0 -0
- package/bin/aarch64-unknown-linux-gnu/yavascript +0 -0
- package/bin/aarch64-unknown-linux-musl/yavascript +0 -0
- package/bin/aarch64-unknown-linux-static/yavascript +0 -0
- package/bin/x86_64-apple-darwin/yavascript +0 -0
- package/bin/x86_64-pc-windows-static/yavascript.exe +0 -0
- package/bin/x86_64-unknown-linux-gnu/yavascript +0 -0
- package/bin/x86_64-unknown-linux-musl/yavascript +0 -0
- package/bin/x86_64-unknown-linux-static/yavascript +0 -0
- package/dist/index-arm64.js +7527 -6867
- package/dist/index-x86_64.js +7527 -6867
- package/dist/primordials-arm64.js +7816 -7159
- package/dist/primordials-x86_64.js +7816 -7159
- package/package.json +1 -1
- package/yavascript.d.ts +1820 -665
package/README.md
CHANGED
|
@@ -1,81 +1,141 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 
|
|
2
2
|
|
|
3
|
-
YavaScript is a cross-platform bash-like script runner and repl which is distributed as a single
|
|
4
|
-
statically-linked binary. Scripts can be written in [JavaScript](https://en.wikipedia.org/wiki/JavaScript), [TypeScript](https://www.typescriptlang.org/), [JSX/TSX](https://react.dev/learn/writing-markup-with-jsx), [CoffeeScript](https://coffeescript.org/) or [Civet](https://civet.dev/).
|
|
3
|
+
YavaScript is a cross-platform bash-like script runner and repl which is distributed as a single statically-linked program, weighing in at about 4MB. Scripts can be written in [JavaScript](https://en.wikipedia.org/wiki/JavaScript) or [JS-related languages](#languages).
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
a bash script, such as:
|
|
5
|
+
> YavaScript is the name of the program. YavaScript is not a new language. YavaScript uses normal JavaScript.
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
7
|
+
## Why?
|
|
8
|
+
|
|
9
|
+
YavaScript exists as an alternative to bash scripts. Instead of writing scripts using shell syntax and running them with bash, you write them in JavaScript and run them with YavaScript.
|
|
10
|
+
|
|
11
|
+
At only ~4MB and with no dependencies (not even Node.js), YavaScript is easy to install or include in a Docker image. As such, it's suitable for use in all the places you would use shell scripts now. It's a great fit for those sort of "environment-level infrastructure" scripts that every Git repo ends up needing, like "build the app", "pull the latest docker images", "install/use the correct versions of languages and tools", etc.
|
|
12
|
+
|
|
13
|
+
YavaScript has built-in APIs for all the things you'd normally want to do in a bash script, such as:
|
|
14
|
+
|
|
15
|
+
- Running programs
|
|
16
|
+
- Using environment variables
|
|
17
|
+
- Working with files/folders
|
|
18
|
+
- Resolving globs into lists of paths
|
|
16
19
|
- Printing stylized text
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
- Parse command-line flags
|
|
27
|
-
- Work with Arrays (lists)
|
|
28
|
-
- Work with Objects (key/value dictionaries)
|
|
29
|
-
- Work with Typed Arrays (byte buffers)
|
|
30
|
-
- Reliably get the path to the currently-running file
|
|
31
|
-
- Strongly-typed interfaces and functions (via TypeScript)
|
|
20
|
+
|
|
21
|
+
As well as APIs for things which are difficult or cumbersome in bash, like:
|
|
22
|
+
|
|
23
|
+
- (De)serialize JSON, CSV, YAML, and TOML
|
|
24
|
+
- Parse command-line flags into a structured object
|
|
25
|
+
- Safely manipulate and resolve path strings
|
|
26
|
+
- Work with raw byte buffers (typed arrays)
|
|
27
|
+
- Reliably get the path to the currently-running script
|
|
28
|
+
- Typed interfaces and functions (via TypeScript)
|
|
32
29
|
- Cross-file import/export using ECMAScript Modules
|
|
33
|
-
- Split strings on delimeters
|
|
34
|
-
- Pretty-print complex structures
|
|
35
30
|
- Call low-level POSIX C APIs like fputs, sprintf, isatty
|
|
36
|
-
- Perform work in threads
|
|
37
|
-
- Import packages from npm (via "npm:" imports) or local node_modules
|
|
38
31
|
|
|
39
|
-
You'll also find analogues to familiar CLI tools, like
|
|
32
|
+
You'll also find cross-platform analogues to familiar CLI tools, like `mkdir`, `rm`, `chmod`, `dirname`, `which`, and more.
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
- basename
|
|
43
|
-
- cat
|
|
44
|
-
- ls
|
|
45
|
-
- realpath
|
|
46
|
-
- readlink
|
|
34
|
+
## APIs
|
|
47
35
|
|
|
48
|
-
|
|
36
|
+
**For the full API documentation, see [here](/meta/generated-docs/README.md).**
|
|
49
37
|
|
|
50
|
-
|
|
51
|
-
this program, or online at https://github.com/suchipi/yavascript/blob/main/yavascript.d.ts.
|
|
52
|
-
This file contains TypeScript type definitions which can be given to your IDE
|
|
53
|
-
to assist you when writing scripts, even if you aren't writing your scripts in TypeScript.
|
|
38
|
+
## Example
|
|
54
39
|
|
|
55
40
|
Here's an example of a script using YavaScript:
|
|
56
41
|
|
|
57
42
|
```js
|
|
58
43
|
#!/usr/bin/env yavascript
|
|
59
44
|
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
45
|
+
// Searches upwards from this file to find the root of the Git repository
|
|
46
|
+
const repoRoot = GitRepo.findRoot(__filename);
|
|
47
|
+
|
|
48
|
+
cd(repoRoot);
|
|
49
|
+
|
|
50
|
+
// Check if there are changes to the repo
|
|
51
|
+
const diffResult = exec("git diff --quiet", { failOnNonZeroStatus: false });
|
|
52
|
+
const isWorkingTreeDirty = diffResult.status !== 0;
|
|
53
|
+
|
|
54
|
+
// If there are, check whether .js files in lib/ have a matching .d.ts file. This is a contrived hypothetical thing you might run in CI.
|
|
55
|
+
if (isWorkingTreeDirty) {
|
|
56
|
+
const jsFiles = glob("lib/**/*.js");
|
|
57
|
+
for (const filePath of jsFiles) {
|
|
58
|
+
// filePath is an instance of the Path class: https://github.com/suchipi/yavascript/blob/main/meta/generated-docs/path.md#path-class
|
|
59
|
+
const dtsFilePath = filePath.replaceLast(
|
|
60
|
+
filePath.basename().replace(/\.js$/, ".d.ts")
|
|
61
|
+
);
|
|
62
|
+
if (!exists(dtsFilePath)) {
|
|
63
|
+
const displayPath = quote(dtsFilePath.relativeTo(repoRoot));
|
|
64
|
+
let message = `Expected ${displayPath} to exist, but it didn't. Please add .d.ts files for all .js files under 'lib/'.`;
|
|
65
|
+
|
|
66
|
+
// ANSI escape sequence helpers
|
|
67
|
+
message = bold(yellow(message));
|
|
68
|
+
|
|
69
|
+
// Writes to stderr
|
|
70
|
+
console.error(messsage);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
69
73
|
}
|
|
70
74
|
|
|
75
|
+
// Prepare some info for a deployment automation tool...
|
|
71
76
|
const branchName = $(`git rev-parse --abbrev-ref HEAD`).stdout.trim();
|
|
72
|
-
|
|
73
77
|
const gitInfo = { branchName, isWorkingTreeDirty };
|
|
78
|
+
|
|
79
|
+
// `echo` and `print` are aliases for `console.log`, for discoverability.
|
|
74
80
|
echo(gitInfo);
|
|
75
81
|
|
|
82
|
+
// YAML.stringify works like JSON.stringify. We also have CSV and TOML!
|
|
76
83
|
writeFile("git-info.yml", YAML.stringify(gitInfo));
|
|
84
|
+
|
|
85
|
+
// Need something lower-level? Use builtin POSIX APIs from QuickJS.
|
|
86
|
+
import * as std from "quickjs:std";
|
|
87
|
+
import * as os from "quickjs:os";
|
|
88
|
+
|
|
89
|
+
console.log(`Finished at ${std.strftime(64, "%Y-%m-%dT%H:%M:%S", Date.now())}`);
|
|
90
|
+
console.log(os.lstat(".gitignore").size);
|
|
91
|
+
console.log("Is tty?", os.isatty(std.in));
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## How is that different from \_\_\_\_?
|
|
95
|
+
|
|
96
|
+
There are several other projects that bring a shell-like environment to JS, such as [zx](https://github.com/google/zx), [ShellJS](https://www.npmjs.com/package/shelljs), and [Bun Shell](https://bun.sh/docs/runtime/shell). The main difference between those and YavaScript is that YavaScript is very small, fully cross-platform, and brings its own JavaScript engine. The effect of those differences is that you can rely on YavaScript in places where you couldn't always rely on zx/shelljs/bun, like in your bootstrapping script that installs Node, or your smallest Docker containers. Or even on tiny constrained systems, like your router!
|
|
97
|
+
|
|
98
|
+
## Supported Platforms
|
|
99
|
+
|
|
100
|
+
- macOS (10.16 or higher)
|
|
101
|
+
- Intel Processors (x86_64)
|
|
102
|
+
- Apple Silicon (aarch64)
|
|
103
|
+
- Linux
|
|
104
|
+
- aarch64 or x86_64
|
|
105
|
+
- glibc, muslc, or statically-linked
|
|
106
|
+
- Windows (MinGW)
|
|
107
|
+
- x86_64
|
|
108
|
+
|
|
109
|
+
## Installation
|
|
110
|
+
|
|
111
|
+
You can find the binary for your platform on [the releases page](https://github.com/suchipi/yavascript/releases). As YavaScript is fully self-contained in one small file, it's trivial to install and uninstall; simply place it somewhere specified in your [`PATH`](https://superuser.com/a/284351).
|
|
112
|
+
|
|
113
|
+
## Languages
|
|
114
|
+
|
|
115
|
+
YavaScript can load and run any of these languages with no ahead-of-time compilation step needed:
|
|
116
|
+
|
|
117
|
+
- JavaScript
|
|
118
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
|
119
|
+
- [JSX/TSX](https://react.dev/learn/writing-markup-with-jsx)
|
|
120
|
+
- [CoffeeScript](https://coffeescript.org/)
|
|
121
|
+
- [Civet](https://civet.dev/)
|
|
122
|
+
|
|
123
|
+
## TypeScript Types
|
|
124
|
+
|
|
125
|
+
YavaScript comes with a TypeScript type definition (`.d.ts`) file.
|
|
126
|
+
|
|
127
|
+
The `.d.ts` file contains documented TypeScript type definitions which can be given to your IDE to assist you when writing scripts, even if you aren't writing your scripts in TypeScript.
|
|
128
|
+
|
|
129
|
+
You can [view the `.d.ts` file online](./yavascript.d.ts), but if you have YavaScript installed, you should instead run `yavascript --print-types` to obtain the `.d.ts` file for your specific release.
|
|
130
|
+
|
|
131
|
+
You can put this comment at the top of your script to instruct VS Code to load the type information from the `.d.ts` file, which will improve the quality of Intellisense, error checking, and autocomplete, even if you aren't using TypeScript:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
/// <reference path="./yavascript.d.ts" />
|
|
77
135
|
```
|
|
78
136
|
|
|
137
|
+
## QuickJS
|
|
138
|
+
|
|
79
139
|
YavaScript is powered by a fork of the QuickJS JavaScript Engine, originally
|
|
80
140
|
written by Fabrice Bellard. QuickJS is a small, fast JavaScript engine
|
|
81
141
|
supporting the ES2020 specification.
|
|
@@ -83,15 +143,21 @@ supporting the ES2020 specification.
|
|
|
83
143
|
- Original QuickJS engine: https://bellard.org/quickjs/
|
|
84
144
|
- The fork we use: https://github.com/suchipi/quickjs/
|
|
85
145
|
|
|
86
|
-
## Compiling
|
|
146
|
+
## Compiling from Source
|
|
87
147
|
|
|
88
148
|
You'll need to install these prerequisites:
|
|
89
149
|
|
|
90
|
-
- [
|
|
91
|
-
- [
|
|
92
|
-
- [
|
|
150
|
+
- [Node.js](https://nodejs.org/en)
|
|
151
|
+
- [Ninja](https://ninja-build.org/)
|
|
152
|
+
- [fnm](https://github.com/Schniz/fnm) (optional)
|
|
153
|
+
|
|
154
|
+
Then run `meta/build.sh`. The compiled output will be in the `dist` folder:
|
|
155
|
+
|
|
156
|
+
> If you didn't install fnm, use `env SKIP_FNM_USE=1 meta/build.sh` instead.
|
|
93
157
|
|
|
94
|
-
|
|
158
|
+
- `dist/yavascript`: The binary for your platform
|
|
159
|
+
- `dist/bin/*`: Binaries for all supported platforms
|
|
160
|
+
- Other files in `dist/`: Intermediate build artifacts
|
|
95
161
|
|
|
96
162
|
### Building the Docker image
|
|
97
163
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|