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

115 lines
2.9 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';
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.
## 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`.
2023-04-14 08:13:40 +00:00
<https://docs.sheetjs.com/cli/sheet2csv.ts> can be compiled and run from Deno:
0) Download the test file <https://sheetjs.com/pres.numbers>:
```bash
curl -LO https://sheetjs.com/pres.numbers
2022-07-31 23:45:34 +00:00
```
2023-04-14 08:13:40 +00:00
1) Test the script with `deno run`:
2022-07-31 23:45:34 +00:00
```bash
2023-04-14 08:13:40 +00:00
deno run -r --allow-read https://docs.sheetjs.com/cli/sheet2csv.ts pres.numbers
2022-07-31 23:45:34 +00:00
```
2023-04-14 08:13:40 +00:00
If this worked, the program will print a CSV of the first worksheet.
2022-07-31 23:45:34 +00:00
2023-04-14 08:13:40 +00:00
2) Compile and run `sheet2csv`:
```bash
deno compile -r --allow-read https://docs.sheetjs.com/cli/sheet2csv.ts
./sheet2csv pres.numbers
```
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)
...
```
1) Download [`xlsx-cli.js`](pathname:///cli/xlsx-cli.js)
2) Install the dependencies:
<Tabs>
<TabItem value="npm" label="npm">
<pre><code parentName="pre" {...{"className": "language-bash"}}>{`\
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
</code></pre>
</TabItem>
<TabItem value="pnpm" label="pnpm">
<pre><code parentName="pre" {...{"className": "language-bash"}}>{`\
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
</code></pre>
</TabItem>
<TabItem value="yarn" label="Yarn" default>
<pre><code parentName="pre" {...{"className": "language-bash"}}>{`\
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
</code></pre>
</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.
</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` .
</TabItem>
</Tabs>