forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-markdown-table.js
executable file
·61 lines (46 loc) · 1.4 KB
/
create-markdown-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
// Dependency: This script requires Nodejs.
// Install Node: https://nodejs.org/en/download/
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Markdown Table
// @raycast.packageName Conversions
// @raycast.mode silent
// Optional parameters:
// @raycast.icon 🧱
// @raycast.argument1 { "type": "text", "placeholder": "Columns" }
// @raycast.argument2 { "type": "text", "placeholder": "Rows" }
// Documentation:
// @raycast.description Create a markdown table template
// @raycast.author Ryan Nystrom
// @raycast.authorURL https://github.com/rnystrom
const child_process = require("child_process");
function pbcopy(data) {
return new Promise(function (resolve, reject) {
const child = child_process.spawn("pbcopy");
child.on("error", function (err) {
reject(err);
});
child.on("close", function (err) {
resolve(data);
});
child.stdin.write(data);
child.stdin.end();
});
}
let [columns, rows] = process.argv.slice(2);
columns = Math.max(parseInt(columns), 1);
rows = Math.max(parseInt(rows), 1);
let emptyTemplate = " ";
let headerTemplate = "---";
let rowArr = Array(columns + 1).fill("|");
let lines = [];
// header
lines.push(rowArr.join(emptyTemplate));
// header spacer |---|
lines.push(rowArr.join(headerTemplate));
// rows
for (let i = 0; i < rows; i++) {
lines.push(rowArr.join(emptyTemplate));
}
pbcopy(lines.join("\n"));