docs.sheetjs.com/docz/docs/03-demos/06-desktop/09-cli.md

369 lines
9.3 KiB
Markdown
Raw Normal View History

2022-07-31 23:45:34 +00:00
---
title: Command-Line Tools
2023-02-28 11:40:44 +00:00
pagination_prev: demos/mobile/index
pagination_next: demos/data/index
sidebar_custom_props:
cli: true
2022-07-31 23:45:34 +00:00
---
import current from '/version.js';
2022-08-01 08:15:50 +00:00
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
2023-05-07 13:58:36 +00:00
import CodeBlock from '@theme/CodeBlock';
2022-07-31 23:45:34 +00:00
2022-08-25 08:22:28 +00:00
With the availability of JS engines and the success of server-side platforms,
it is feasible to build command-line tools for various workflows.
2022-07-31 23:45:34 +00:00
This demo covers a number of strategies for building standalone processors. The
goal is to generate CSV output from an arbitrary spreadsheet file.
2022-08-01 08:15:50 +00:00
## NodeJS
2022-08-25 08:22:28 +00:00
There are a few popular tools for compiling NodeJS scripts to CLI programs.
2022-08-01 08:15:50 +00:00
The demo script presents a friendly command line interface including flags:
```bash
$ ./xlsx-cli -h
Usage: xlsx-cli [options] <file> [sheetname]
Options:
-V, --version output the version number
-f, --file <file> use specified workbook
-s, --sheet <sheet> print specified sheet (default first sheet)
...
```
2023-05-09 08:08:01 +00:00
<details><summary><b>Tested Deployments</b> (click to show)</summary>
This demo was tested in the following deployments:
**`pkg`**
| Architecture | Version | Node Target | Date |
|:-------------|:--------|:------------|:-----------|
| `darwin-x64` | `5.8.1` | `18.5.0` | 2023-05-08 |
2023-06-05 20:12:53 +00:00
| `darwin-arm` | `5.8.1` | `18.5.0` | 2023-06-05 |
2023-08-18 20:39:12 +00:00
| `win10-x64` | `5.8.1` | `18.5.0` | 2023-05-08 |
2023-05-09 08:08:01 +00:00
| `linux-x64` | `5.8.1` | `18.5.0` | 2023-05-08 |
**`nexe`**
| Architecture | Version | Node Target | Date |
|:-------------|:-------------|:------------|:-----------|
| `darwin-x64` | `4.0.0-rc.2` | `14.15.3` | 2023-05-08 |
2023-06-05 20:12:53 +00:00
| `darwin-arm` | `4.0.0-rc.2` | `18.16.0` | 2023-06-05 |
2023-08-18 20:39:12 +00:00
| `win10-x64` | `4.0.0-rc.2` | `14.15.3` | 2023-05-08 |
2023-05-09 08:08:01 +00:00
| `linux-x64` | `4.0.0-rc.2` | `14.15.3` | 2023-05-08 |
**`boxednode`**
| Architecture | Version | Node Target | Date |
|:-------------|:--------|:------------|:-----------|
| `darwin-x64` | `2.0.1` | `20.1.0` | 2023-05-08 |
2023-06-05 20:12:53 +00:00
| `darwin-arm` | `2.0.1` | `20.2.0` | 2023-06-05 |
2023-08-28 22:40:53 +00:00
| `win10-x64` | `2.1.1` | `16.20.2` | 2023-08-27 |
2023-05-09 08:08:01 +00:00
| `linux-x64` | `2.0.1` | `20.1.0` | 2023-05-08 |
</details>
0) Download the test file <https://sheetjs.com/pres.numbers>:
```bash
curl -LO https://sheetjs.com/pres.numbers
```
2022-08-01 08:15:50 +00:00
1) Download [`xlsx-cli.js`](pathname:///cli/xlsx-cli.js)
2023-05-09 08:08:01 +00:00
```bash
curl -LO https://docs.sheetjs.com/cli/xlsx-cli.js
```
2022-08-01 08:15:50 +00:00
2) Install the dependencies:
2023-05-07 13:58:36 +00:00
<Tabs groupId="pm">
2022-08-01 08:15:50 +00:00
<TabItem value="npm" label="npm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-01 08:15:50 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-08-01 08:15:50 +00:00
</TabItem>
<TabItem value="pnpm" label="pnpm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-01 08:15:50 +00:00
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-08-01 08:15:50 +00:00
</TabItem>
<TabItem value="yarn" label="Yarn" default>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-01 08:15:50 +00:00
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-08-01 08:15:50 +00:00
</TabItem>
</Tabs>
3) Follow tooling steps:
<Tabs>
<TabItem value="nexe" label="Nexe">
2023-02-28 11:40:44 +00:00
Run `nexe` and manually specify NodeJS version 14.15.3
2022-08-01 08:15:50 +00:00
```bash
npx nexe -t 14.15.3 xlsx-cli.js
```
This generates `xlsx-cli` or `xlsx-cli.exe` depending on platform.
2023-06-05 20:12:53 +00:00
:::caution
When the demo was tested on `darwin-arm`, the `mac-arm64` pre-built package was
missing. The package must be built from source:
```bash
npx nexe xlsx-cli.js --build --python=$(which python3) --make="-j8"
```
:::
2022-08-01 08:15:50 +00:00
</TabItem>
<TabItem value="pkg" label="pkg">
2023-02-28 11:40:44 +00:00
Run `pkg`:
2022-08-01 08:15:50 +00:00
```bash
npx pkg xlsx-cli.js
```
This generates `xlsx-cli-linux`, `xlsx-cli-macos`, and `xlsx-cli-win.exe` .
2023-05-09 08:08:01 +00:00
</TabItem>
<TabItem value="boxednode" label="boxednode">
Run `boxednode`:
2023-08-28 22:40:53 +00:00
<Tabs groupId="os">
<TabItem value="unix" label="Linux/MacOS">
```bash
npx boxednode@2.1.1 -s xlsx-cli.js -t xlsx-cli
```
</TabItem>
<TabItem value="win" label="Windows">
2023-05-09 08:08:01 +00:00
```bash
2023-08-28 22:40:53 +00:00
npx boxednode@2.1.1 -s xlsx-cli.js -t xlsx-cli.exe -n 16.20.2
```
:::info pass
The Windows 10 build requires Visual Studio with "Desktop development with C++"
workload, Python 3, and NASM[^1].
:::
:::caution pass
When the demo was last tested on Windows, the build failed:
```
error MSB8020: The build tools for Visual Studio 2019 (Platform Toolset = 'v142') cannot be found. To build using the v142 build tools, please install Visual Studio 2019 build tools.
```
This error was fixed by installing the `v142` build tools through the Visual
Studio installer.
:::
:::caution pass
In the most recent Windows test against NodeJS `20.5.1`, the build failed due
to an issue in the OpenSSL dependency:
```
...\node-v20.5.1\deps\openssl\openssl\crypto\info.c(176,16): error C2153: integer literals must have at least one digit [...\node-v20.5.1\deps\openssl\openssl.vcxproj]
2023-05-09 08:08:01 +00:00
```
2023-08-28 22:40:53 +00:00
SheetJS libraries are compatible with NodeJS versions dating back to `v0.8`. The
workaround is to select NodeJS `v16.20.2` using the `-n` flag. This version was
was chosen since NodeJS `v18` upgraded the OpenSSL dependency.
:::
2022-08-01 08:15:50 +00:00
</TabItem>
</Tabs>
2023-05-09 08:08:01 +00:00
2023-08-28 22:40:53 +00:00
</TabItem>
</Tabs>
4) Run the generated program, passing `pres.numbers` as the argument. For
2023-06-05 20:12:53 +00:00
example, `nexe` generates `xlsx-cli` in macOS so the command is:
2023-05-09 08:08:01 +00:00
```bash
./xlsx-cli pres.numbers
```
2023-08-28 22:40:53 +00:00
`nexe` generates `xlsx-cli.exe` in Windows, so the command is:
```powershell
.\xlsx-cli.exe pres.numbers
```
2023-05-25 01:36:15 +00:00
## V8
The [V8](/docs/demos/engines/v8) demo covers standalone programs that embed the
V8 engine. This demo uses the Rust integration to generate a command line tool.
<details><summary><b>Tested Deployments</b> (click to show)</summary>
This demo was last tested in the following deployments:
2023-08-31 22:09:08 +00:00
| Architecture | V8 Version | Crate | Date |
|:-------------|:-------------|:---------|:-----------|
| `darwin-x64` | `11.4.183.2` | `0.71.2` | 2023-05-22 |
| `darwin-arm` | `11.4.183.2` | `0.71.2` | 2023-05-22 |
| `linux-x64` | `11.4.183.2` | `0.71.2` | 2023-05-23 |
| `linux-arm` | `11.7.439.6` | `0.75.1` | 2023-08-30 |
| `win10-x64` | `11.4.183.2` | `0.71.2` | 2023-05-23 |
| `win11-x64` | `11.7.439.6` | `0.75.1` | 2023-08-31 |
:::caution pass
Using crate version `0.71.2`, the Linux AArch64 build failed with an error:
```
error[E0080]: evaluation of constant value failed
|
1715 | assert!(size_of::<TypeId>() == size_of::<u64>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: size_of::<TypeId>() == size_of::<u64>()'
```
This was fixed in version `0.75.1` of the crate.
:::
2023-05-25 01:36:15 +00:00
</details>
0) Make a new folder for the project:
```bash
mkdir sheetjs2csv
cd sheetjs2csv
```
1) Download the following scripts:
- [`Cargo.toml`](pathname:///cli/Cargo.toml)
- [`snapshot.rs`](pathname:///cli/snapshot.rs)
- [`sheet2csv.rs`](pathname:///cli/sheet2csv.rs)
```bash
curl -LO https://docs.sheetjs.com/cli/Cargo.toml
curl -LO https://docs.sheetjs.com/cli/snapshot.rs
curl -LO https://docs.sheetjs.com/cli/sheet2csv.rs
```
2) Download the [standalone build](/docs/getting-started/installation/standalone):
<CodeBlock language="bash">{`\
curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}
</CodeBlock>
3) Build the V8 snapshot:
```bash
cargo build --bin snapshot
cargo run --bin snapshot
```
4) Build `sheet2csv` (`sheet2csv.exe` in Windows):
```bash
cargo build --release --bin sheet2csv
```
5) Download the test file <https://sheetjs.com/pres.numbers>:
```bash
curl -LO https://sheetjs.com/pres.numbers
```
6) Test the application:
<Tabs groupId="os">
<TabItem value="unix" label="Linux/MacOS">
```bash
mv target/release/sheet2csv .
./sheet2csv pres.numbers
```
</TabItem>
<TabItem value="win" label="Windows">
```bash
mv target/release/sheet2csv.exe .
2023-08-31 22:09:08 +00:00
.\sheet2csv.exe pres.numbers
2023-05-25 01:36:15 +00:00
```
</TabItem>
</Tabs>
2023-05-09 08:08:01 +00:00
## Deno
`deno compile` generates a standalone executable that includes the entire JS
runtime as well as user JS code.
When compiling, the `--allow-read` option must be specified to allow the script
to read files from the filesystem with `Deno.readFileSync`.
<https://docs.sheetjs.com/cli/sheet2csv.ts> can be compiled and run from Deno.
<details><summary><b>Tested Deployments</b> (click to show)</summary>
This demo was last tested in the following deployments:
| Architecture | Version | Date |
|:-------------|:---------|:-----------|
| `darwin-x64` | `1.33.2` | 2023-05-08 |
2023-06-05 20:12:53 +00:00
| `darwin-arm` | `1.34.1` | 2023-06-05 |
2023-08-18 20:39:12 +00:00
| `win10-x64` | `1.33.2` | 2023-05-08 |
2023-05-09 08:08:01 +00:00
| `linux-x64` | `1.33.2` | 2023-05-08 |
2023-08-31 22:09:08 +00:00
| `linux-arm` | `1.36.3` | 2023-08-30 |
2023-05-09 08:08:01 +00:00
</details>
0) Download the test file <https://sheetjs.com/pres.numbers>:
```bash
curl -LO https://sheetjs.com/pres.numbers
```
1) Test the script with `deno run`:
```bash
deno run -r --allow-read https://docs.sheetjs.com/cli/sheet2csv.ts pres.numbers
```
If this worked, the program will print a CSV of the first worksheet.
2) Compile and run `sheet2csv`:
```bash
deno compile -r --allow-read https://docs.sheetjs.com/cli/sheet2csv.ts
./sheet2csv pres.numbers
```
## Dedicated Engines
The following demos for JS engines produce standalone programs:
- [Duktape](/docs/demos/engines/duktape)
2023-05-23 06:28:14 +00:00
- [ChakraCore](/docs/demos/engines/chakra)
- [QuickJS](/docs/demos/engines/quickjs)
2023-05-09 08:08:01 +00:00
- [Goja](/docs/demos/engines/goja)
- [JavaScriptCore](/docs/demos/engines/jsc)
2023-08-28 22:40:53 +00:00
[^1]: Downloads can be found [at the main NASM project website](https://www.nasm.us/)