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 CHANGED
@@ -1,81 +1,141 @@
1
- # yavascript
1
+ # ![YavaScript logo](meta/assets/logo.png)
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
- There are APIs available for all the things you'd normally want to do in
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
- - Running programs and getting their stdout/stderr/status
10
- - Reading/writing environment variables
11
- - Checking if files/folders exist
12
- - Removing/creating/copying files/folders
13
- - Reading and changing the current working directory
14
- - Reading and resolving symbolic links
15
- - Using globs to get large lists of files
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
- - Clearing the terminal
18
- - Fetching files from the internet
19
-
20
- Additionally, you can do other things that are either not present in bash or are cumbersome to use in bash, namely:
21
-
22
- - Serialize and deserialize JSON, CSV, and YAML
23
- - Removing ANSI control characters from a string
24
- - Split path strings into a list of segments and rejoin them into a string
25
- - Check if a path is absolute and resolve relative paths
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
- - dirname
42
- - basename
43
- - cat
44
- - ls
45
- - realpath
46
- - readlink
34
+ ## APIs
47
35
 
48
- ...and more.
36
+ **For the full API documentation, see [here](/meta/generated-docs/README.md).**
49
37
 
50
- To view the APIs, consult the file yavascript.d.ts which was distributed with
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
- // This comment is optional; it tells VS Code to load the specified TypeScript definitions.
61
- /// <reference path="./yavascript.d.ts" />
62
-
63
- let isWorkingTreeDirty;
64
- try {
65
- exec(`git diff --quiet`);
66
- isWorkingTreeDirty = false;
67
- } catch (error) {
68
- isWorkingTreeDirty = true;
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
- - [node.js](https://nodejs.org/en)
91
- - [ninja](https://ninja-build.org/)
92
- - [bat](https://github.com/sharkdp/bat)
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
- Then run `meta/build.sh` to build binaries for the current platform (will be output in `dist`), or `meta/build-all.sh` to build binaries for all platforms (will be output in `bin`).
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