formatarc 0.1.0
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 +21 -0
- package/README.md +103 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +62 -0
- package/dist/converter.d.ts +7 -0
- package/dist/converter.js +71 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FormatArc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# formatarc
|
|
2
|
+
|
|
3
|
+
Convert JSON, YAML, and CSV from the terminal. No config, no dependencies to manage — just pipe or pass your data.
|
|
4
|
+
|
|
5
|
+
**Web version → [formatarc.com](https://formatarc.com)**
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g formatarc
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or run directly with `npx`:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx formatarc json-format '{"a":1}'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
formatarc <tool> [input or file]
|
|
23
|
+
cat file | formatarc <tool>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Tools
|
|
27
|
+
|
|
28
|
+
| Command | Description |
|
|
29
|
+
|---------|-------------|
|
|
30
|
+
| `json-format` | Pretty-print JSON |
|
|
31
|
+
| `yaml-to-json` | Convert YAML to JSON |
|
|
32
|
+
| `json-to-yaml` | Convert JSON to YAML |
|
|
33
|
+
| `csv-to-json` | Convert CSV (with header row) to JSON |
|
|
34
|
+
|
|
35
|
+
### Examples
|
|
36
|
+
|
|
37
|
+
Format JSON:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
formatarc json-format '{"name":"FormatArc","tools":["json","yaml","csv"]}'
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"name": "FormatArc",
|
|
46
|
+
"tools": [
|
|
47
|
+
"json",
|
|
48
|
+
"yaml",
|
|
49
|
+
"csv"
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Convert a YAML file to JSON:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
formatarc yaml-to-json config.yaml
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Pipe from curl:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
curl -s https://api.example.com/data | formatarc json-format
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Convert CSV from stdin:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
cat users.csv | formatarc csv-to-json
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Programmatic API
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { convert } from "formatarc";
|
|
76
|
+
|
|
77
|
+
const result = convert("json-format", '{"a":1}');
|
|
78
|
+
console.log(result.output);
|
|
79
|
+
// {
|
|
80
|
+
// "a": 1
|
|
81
|
+
// }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `convert(tool, input)`
|
|
85
|
+
|
|
86
|
+
Returns `{ output: string, error: string }`.
|
|
87
|
+
|
|
88
|
+
- `output` — the converted result (empty string on error)
|
|
89
|
+
- `error` — error message (empty string on success)
|
|
90
|
+
|
|
91
|
+
## Web Version
|
|
92
|
+
|
|
93
|
+
For a browser-based experience with no signup and no data upload:
|
|
94
|
+
|
|
95
|
+
**[formatarc.com](https://formatarc.com)**
|
|
96
|
+
|
|
97
|
+
- JSON Formatter, YAML ↔ JSON, CSV → JSON
|
|
98
|
+
- Runs entirely in the browser
|
|
99
|
+
- Multilingual (English / Japanese)
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync, existsSync } from "fs";
|
|
3
|
+
import { convert, isValidTool } from "./converter.js";
|
|
4
|
+
const USAGE = `
|
|
5
|
+
formatarc — convert JSON, YAML, and CSV from the terminal
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
formatarc <tool> [input]
|
|
9
|
+
formatarc <tool> <file>
|
|
10
|
+
cat file | formatarc <tool>
|
|
11
|
+
|
|
12
|
+
Tools:
|
|
13
|
+
json-format Pretty-print JSON
|
|
14
|
+
yaml-to-json Convert YAML to JSON
|
|
15
|
+
json-to-yaml Convert JSON to YAML
|
|
16
|
+
csv-to-json Convert CSV to JSON
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
formatarc json-format '{"a":1}'
|
|
20
|
+
formatarc yaml-to-json config.yaml
|
|
21
|
+
cat data.csv | formatarc csv-to-json
|
|
22
|
+
|
|
23
|
+
Web version: https://formatarc.com
|
|
24
|
+
`.trim();
|
|
25
|
+
function main() {
|
|
26
|
+
const args = process.argv.slice(2);
|
|
27
|
+
if (args.length === 0 || args[0] === "--help" || args[0] === "-h") {
|
|
28
|
+
console.log(USAGE);
|
|
29
|
+
process.exit(0);
|
|
30
|
+
}
|
|
31
|
+
const toolArg = args[0];
|
|
32
|
+
if (!isValidTool(toolArg)) {
|
|
33
|
+
console.error(`Unknown tool: ${toolArg}\n`);
|
|
34
|
+
console.error(USAGE);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
const tool = toolArg;
|
|
38
|
+
let input;
|
|
39
|
+
if (args[1]) {
|
|
40
|
+
if (existsSync(args[1])) {
|
|
41
|
+
input = readFileSync(args[1], "utf-8");
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
input = args[1];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else if (!process.stdin.isTTY) {
|
|
48
|
+
input = readFileSync(0, "utf-8");
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
console.error("No input provided. Pass a string, a file path, or pipe from stdin.\n");
|
|
52
|
+
console.error(USAGE);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
const result = convert(tool, input);
|
|
56
|
+
if (result.error) {
|
|
57
|
+
console.error(result.error);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
process.stdout.write(result.output + "\n");
|
|
61
|
+
}
|
|
62
|
+
main();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type ConvertResult = {
|
|
2
|
+
output: string;
|
|
3
|
+
error: string;
|
|
4
|
+
};
|
|
5
|
+
export type Tool = "json-format" | "yaml-to-json" | "json-to-yaml" | "csv-to-json";
|
|
6
|
+
export declare function isValidTool(value: string): value is Tool;
|
|
7
|
+
export declare function convert(tool: Tool, input: string): ConvertResult;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import Papa from "papaparse";
|
|
2
|
+
import YAML from "yaml";
|
|
3
|
+
const TOOLS = ["json-format", "yaml-to-json", "json-to-yaml", "csv-to-json"];
|
|
4
|
+
export function isValidTool(value) {
|
|
5
|
+
return TOOLS.includes(value);
|
|
6
|
+
}
|
|
7
|
+
export function convert(tool, input) {
|
|
8
|
+
if (!input.trim()) {
|
|
9
|
+
return { output: "", error: "Input is empty." };
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
switch (tool) {
|
|
13
|
+
case "json-format":
|
|
14
|
+
return {
|
|
15
|
+
output: JSON.stringify(JSON.parse(input), null, 2),
|
|
16
|
+
error: "",
|
|
17
|
+
};
|
|
18
|
+
case "yaml-to-json":
|
|
19
|
+
return {
|
|
20
|
+
output: JSON.stringify(YAML.parse(input), null, 2),
|
|
21
|
+
error: "",
|
|
22
|
+
};
|
|
23
|
+
case "json-to-yaml":
|
|
24
|
+
return {
|
|
25
|
+
output: YAML.stringify(JSON.parse(input)),
|
|
26
|
+
error: "",
|
|
27
|
+
};
|
|
28
|
+
case "csv-to-json":
|
|
29
|
+
return convertCsv(input);
|
|
30
|
+
default:
|
|
31
|
+
return { output: "", error: `Unknown tool: ${tool}` };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
return { output: "", error: formatError(err, input, tool) };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function convertCsv(input) {
|
|
39
|
+
const parsed = Papa.parse(input, {
|
|
40
|
+
header: true,
|
|
41
|
+
skipEmptyLines: "greedy",
|
|
42
|
+
transformHeader: (h) => h.trim(),
|
|
43
|
+
});
|
|
44
|
+
if (!parsed.meta.fields?.length || parsed.meta.fields.every((f) => !f)) {
|
|
45
|
+
return { output: "", error: "CSV header row is missing." };
|
|
46
|
+
}
|
|
47
|
+
if (parsed.errors.length > 0) {
|
|
48
|
+
const first = parsed.errors[0];
|
|
49
|
+
const row = typeof first.row === "number" ? ` (row ${first.row + 1})` : "";
|
|
50
|
+
return { output: "", error: `CSV parse error: ${first.message}${row}` };
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
output: JSON.stringify(parsed.data, null, 2),
|
|
54
|
+
error: "",
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function formatError(err, input, tool) {
|
|
58
|
+
if (err instanceof YAML.YAMLParseError) {
|
|
59
|
+
const line = err.linePos?.[0]?.line;
|
|
60
|
+
return line ? `YAML syntax error near line ${line}.` : "YAML syntax error.";
|
|
61
|
+
}
|
|
62
|
+
if (err instanceof Error && err.name === "SyntaxError") {
|
|
63
|
+
const match = err.message.match(/position\s+(\d+)/i);
|
|
64
|
+
if (match) {
|
|
65
|
+
const line = input.slice(0, Number(match[1])).split("\n").length;
|
|
66
|
+
return `Invalid JSON near line ${line}.`;
|
|
67
|
+
}
|
|
68
|
+
return "Invalid JSON.";
|
|
69
|
+
}
|
|
70
|
+
return "Failed to parse input. Check the format.";
|
|
71
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "formatarc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Convert JSON, YAML, and CSV from the terminal. Browser-based version at formatarc.com.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"json",
|
|
7
|
+
"yaml",
|
|
8
|
+
"csv",
|
|
9
|
+
"formatter",
|
|
10
|
+
"converter",
|
|
11
|
+
"json-formatter",
|
|
12
|
+
"yaml-to-json",
|
|
13
|
+
"csv-to-json",
|
|
14
|
+
"cli"
|
|
15
|
+
],
|
|
16
|
+
"author": "FormatArc",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/m-naoki-m/formatarc.git"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://formatarc.com",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/converter.js",
|
|
25
|
+
"types": "./dist/converter.d.ts",
|
|
26
|
+
"bin": {
|
|
27
|
+
"formatarc": "./dist/cli.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"papaparse": "^5.5.2",
|
|
40
|
+
"yaml": "^2.8.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/papaparse": "^5.3.15",
|
|
44
|
+
"typescript": "^5.8.3"
|
|
45
|
+
}
|
|
46
|
+
}
|