@barmark/cli 0.0.0 → 0.1.1
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/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +77 -0
- package/dist/args.js +22 -0
- package/dist/check.js +36 -0
- package/dist/cli.js +5 -0
- package/dist/format.js +41 -0
- package/dist/help.js +58 -0
- package/dist/inputs.js +70 -0
- package/dist/io.js +4 -0
- package/dist/main.js +41 -0
- package/dist/node-io.js +44 -0
- package/dist/output.js +46 -0
- package/dist/render.js +56 -0
- package/dist/report.js +13 -0
- package/dist/transpose.js +42 -0
- package/package.json +46 -9
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@barmark/cli` are recorded here. The format
|
|
4
|
+
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the
|
|
5
|
+
package uses [semantic versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.1] - 2026-11-02
|
|
10
|
+
|
|
11
|
+
Uses `barmark` 0.9.0 and `@barmark/render` 0.7.2, pinned exactly: charts
|
|
12
|
+
may now write a swing amount (`feel: swing 58%`), and `render` shows it.
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- **`format --write` replaces each file whole.** The new text goes to a
|
|
17
|
+
temporary file beside the chart, with its permissions, which then
|
|
18
|
+
takes its name; a failure part-way (a full disk, a folder it can't
|
|
19
|
+
write) leaves the chart as it was and reports "cannot write <file>".
|
|
20
|
+
- **`transpose --out` writes nothing when the chart has errors**, and
|
|
21
|
+
says "<file> not written: the chart has errors".
|
|
22
|
+
- **An unknown flag reads `unknown flag "--strct"`**, with how to pass a
|
|
23
|
+
file name that starts with `-`, instead of Node's message, which ended
|
|
24
|
+
on an unbalanced quote.
|
|
25
|
+
|
|
26
|
+
## [0.1.0] - 2026-11-02
|
|
27
|
+
|
|
28
|
+
The first release of the `barmark` command. Uses `barmark` 0.8.0 and
|
|
29
|
+
`@barmark/render` 0.7.1, pinned exactly.
|
|
30
|
+
|
|
31
|
+
### Added
|
|
32
|
+
|
|
33
|
+
- **The `barmark` command:** `check`, `format`, `render` and `transpose`
|
|
34
|
+
for `.chart.md` files, on npm and as standalone binaries.
|
|
35
|
+
|
|
36
|
+
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/cli-v0.1.1...HEAD
|
|
37
|
+
[0.1.1]: https://github.com/kevinpeckham/barmark/compare/cli-v0.1.0...cli-v0.1.1
|
|
38
|
+
[0.1.0]: https://github.com/kevinpeckham/barmark/releases/tag/cli-v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Peckham
|
|
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,77 @@
|
|
|
1
|
+
# @barmark/cli
|
|
2
|
+
|
|
3
|
+
The `barmark` command: check, format, render and transpose
|
|
4
|
+
[Barmark](https://www.barmark.dev) chord charts (`.chart.md` files) in a
|
|
5
|
+
terminal or in CI. It runs the same `barmark` parser and `@barmark/render`
|
|
6
|
+
renderer as barmark.dev, at exact versions, so a chart means the same
|
|
7
|
+
thing everywhere.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
With Node 20.6 or later:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npx @barmark/cli check . # run once
|
|
15
|
+
npm install --save-dev @barmark/cli # or keep it in a project
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Without Node, download the binary for your system from the
|
|
19
|
+
[GitHub release](https://github.com/kevinpeckham/barmark/releases?q=cli-v)
|
|
20
|
+
(`barmark-linux-x64`, `barmark-linux-arm64`, `barmark-darwin-x64`,
|
|
21
|
+
`barmark-darwin-arm64`, `barmark-windows-x64.exe`). The binaries are not
|
|
22
|
+
signed yet; on macOS, clear the download flag before the first run:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
chmod +x barmark-darwin-arm64
|
|
26
|
+
xattr -d com.apple.quarantine barmark-darwin-arm64
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Commands
|
|
30
|
+
|
|
31
|
+
Folders mean every `*.chart.md` file inside them, skipping `node_modules`,
|
|
32
|
+
dot-folders and symlinks. `-` reads from stdin.
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
barmark check songs # errors and warnings, file:line
|
|
36
|
+
barmark check --strict --format github songs # warnings fail too; PR annotations
|
|
37
|
+
barmark format song.chart.md # print it in standard form
|
|
38
|
+
barmark format --write songs # rewrite the files that change
|
|
39
|
+
barmark format --check songs # list them and fail, for CI
|
|
40
|
+
barmark render song.chart.md --out song.html # a standalone page, font included
|
|
41
|
+
barmark transpose song.chart.md --to E # or --semitones -2
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`check --format json` prints the diagnostics for other tools. `render`
|
|
45
|
+
takes the renderer's options as flags: `--theme auto|light|dark`,
|
|
46
|
+
`--numerals nashville|roman`, `--no-bar-numbers`, `--rehearsal-letters`,
|
|
47
|
+
`--no-wrap`, `--diagnostics`, and `--font-url <url>` to link the font
|
|
48
|
+
instead of embedding it. `barmark <command> --help` lists each command's
|
|
49
|
+
flags; `barmark --version` prints the versions of the CLI and both
|
|
50
|
+
libraries.
|
|
51
|
+
|
|
52
|
+
## Exit codes
|
|
53
|
+
|
|
54
|
+
| Code | Meaning |
|
|
55
|
+
|---|---|
|
|
56
|
+
| 0 | Done (warnings are allowed unless `--strict`) |
|
|
57
|
+
| 1 | The charts have problems: an error, a warning with `--strict`, or a file `format --check` would change |
|
|
58
|
+
| 2 | The command was used wrongly: an unknown flag, a missing or unreadable file |
|
|
59
|
+
|
|
60
|
+
## In CI
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
- uses: actions/setup-node@v4
|
|
64
|
+
with:
|
|
65
|
+
node-version: 22
|
|
66
|
+
- run: npx -y @barmark/cli@0.1.0 check --strict --format github songs
|
|
67
|
+
- run: npx -y @barmark/cli@0.1.0 format --check songs
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Pin the version: a new release can read a chart differently, and a pinned
|
|
71
|
+
version turns that into a pull request you review.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT. `render` embeds Edwin, which `@barmark/render` ships under the SIL
|
|
76
|
+
Open Font License (`fonts/OFL.txt` there; the binaries' release carries it
|
|
77
|
+
as `Edwin-OFL.txt`).
|
package/dist/args.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Flags and positionals, with parseArgs' complaints turned into usage errors. */
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { UsageError } from "./io.js";
|
|
4
|
+
export function parseFlags(args, options) {
|
|
5
|
+
try {
|
|
6
|
+
return parseArgs({ args, options, allowPositionals: true, allowNegative: false, strict: true });
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
// Node's own message for this one ends on an unbalanced quote.
|
|
10
|
+
const unknown = /^Unknown option '([^']+)'/.exec(String(error?.message));
|
|
11
|
+
if (unknown)
|
|
12
|
+
throw new UsageError(`unknown flag "${unknown[1]}"; a file name starting with - goes after --`);
|
|
13
|
+
throw new UsageError(error instanceof Error ? (error.message.split("\n")[0] ?? "") : String(error));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** A flag that takes one of a few values. */
|
|
17
|
+
export function oneOf(flag, value, allowed) {
|
|
18
|
+
if (allowed.includes(value))
|
|
19
|
+
return value;
|
|
20
|
+
const list = `${allowed.slice(0, -1).join(", ")} or ${allowed.at(-1)}`;
|
|
21
|
+
throw new UsageError(`--${flag} must be ${list}, not "${value}"`);
|
|
22
|
+
}
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** barmark check: each chart's errors and warnings, as text, GitHub annotations or JSON. */
|
|
2
|
+
import { parseChart } from "barmark";
|
|
3
|
+
import { oneOf, parseFlags } from "./args.js";
|
|
4
|
+
import { readCharts } from "./inputs.js";
|
|
5
|
+
import { githubLine, summary, textLine } from "./report.js";
|
|
6
|
+
const FORMATS = ["text", "github", "json"];
|
|
7
|
+
export async function check(args, io) {
|
|
8
|
+
const { values, positionals } = parseFlags(args, {
|
|
9
|
+
strict: { type: "boolean", default: false },
|
|
10
|
+
format: { type: "string", default: "text" },
|
|
11
|
+
});
|
|
12
|
+
const format = oneOf("format", values.format, FORMATS);
|
|
13
|
+
const results = (await readCharts(positionals, io, { folders: true })).map(({ path, text }) => ({
|
|
14
|
+
path,
|
|
15
|
+
diagnostics: parseChart(text).diagnostics,
|
|
16
|
+
}));
|
|
17
|
+
const all = results.flatMap((r) => r.diagnostics);
|
|
18
|
+
const errors = all.filter((d) => d.severity === "error").length;
|
|
19
|
+
const warnings = all.length - errors;
|
|
20
|
+
if (format === "json") {
|
|
21
|
+
const files = results.map(({ path, diagnostics }) => ({
|
|
22
|
+
file: path,
|
|
23
|
+
diagnostics: diagnostics.map(({ line, severity, message }) => ({ line, severity, message })),
|
|
24
|
+
}));
|
|
25
|
+
io.stdout(`${JSON.stringify(files, null, 2)}\n`);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const line = format === "github" ? githubLine : textLine;
|
|
29
|
+
for (const { path, diagnostics } of results)
|
|
30
|
+
for (const d of diagnostics)
|
|
31
|
+
io.stdout(`${line(path, d)}\n`);
|
|
32
|
+
if (format === "text")
|
|
33
|
+
io.stdout(`${summary(results.length, errors, warnings)}\n`);
|
|
34
|
+
}
|
|
35
|
+
return errors || (values.strict && warnings) ? 1 : 0;
|
|
36
|
+
}
|
package/dist/cli.js
ADDED
package/dist/format.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** barmark format: charts in standard form, printed, rewritten in place, or checked. */
|
|
2
|
+
import { printChart } from "barmark";
|
|
3
|
+
import { parseFlags } from "./args.js";
|
|
4
|
+
import { readCharts } from "./inputs.js";
|
|
5
|
+
import { UsageError } from "./io.js";
|
|
6
|
+
import { replaceFile } from "./output.js";
|
|
7
|
+
function refuseMisuse(flags, positionals) {
|
|
8
|
+
if (flags.write && flags.check)
|
|
9
|
+
throw new UsageError("--write and --check can't be used together");
|
|
10
|
+
if (flags.write && positionals.includes("-"))
|
|
11
|
+
throw new UsageError("--write can't rewrite stdin");
|
|
12
|
+
}
|
|
13
|
+
/** Rewrite (--write) or list (--check) each chart printChart would change; how many it found. */
|
|
14
|
+
function rewriteAll(charts, write, io) {
|
|
15
|
+
let changed = 0;
|
|
16
|
+
for (const { path, text } of charts) {
|
|
17
|
+
const canonical = printChart(text);
|
|
18
|
+
if (canonical === text)
|
|
19
|
+
continue;
|
|
20
|
+
changed++;
|
|
21
|
+
if (write)
|
|
22
|
+
replaceFile(path, canonical, io);
|
|
23
|
+
io.stdout(`${write ? "formatted" : "would format"} ${path}\n`);
|
|
24
|
+
}
|
|
25
|
+
return changed;
|
|
26
|
+
}
|
|
27
|
+
export async function format(args, io) {
|
|
28
|
+
const { values, positionals } = parseFlags(args, {
|
|
29
|
+
write: { type: "boolean", default: false },
|
|
30
|
+
check: { type: "boolean", default: false },
|
|
31
|
+
});
|
|
32
|
+
refuseMisuse(values, positionals);
|
|
33
|
+
const many = values.write || values.check;
|
|
34
|
+
const charts = await readCharts(positionals, io, { folders: many });
|
|
35
|
+
if (many)
|
|
36
|
+
return rewriteAll(charts, values.write, io) && values.check ? 1 : 0;
|
|
37
|
+
if (charts.length > 1)
|
|
38
|
+
throw new UsageError("several charts need --write or --check");
|
|
39
|
+
io.stdout(printChart(charts[0]?.text ?? ""));
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
package/dist/help.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** The help text, one block for the command and one per subcommand. */
|
|
2
|
+
export const HELP = `Usage: barmark <command> [options]
|
|
3
|
+
|
|
4
|
+
Commands:
|
|
5
|
+
check <paths…> Report each chart's errors and warnings
|
|
6
|
+
format <paths…> Print or rewrite charts in standard form
|
|
7
|
+
render <file> Write a chart as a standalone HTML page
|
|
8
|
+
transpose <file> Print a chart in another key
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
--help Show help (also: barmark <command> --help)
|
|
12
|
+
--version Show the versions of the CLI and its libraries
|
|
13
|
+
|
|
14
|
+
Exit codes: 0 done, 1 the charts have problems, 2 usage.
|
|
15
|
+
`;
|
|
16
|
+
/** Each command's own help: `barmark <command> --help`. */
|
|
17
|
+
export const COMMAND_HELP = {
|
|
18
|
+
check: `Usage: barmark check <paths…> [options]
|
|
19
|
+
|
|
20
|
+
Report each chart's errors and warnings. Folders mean every *.chart.md
|
|
21
|
+
inside them; - reads stdin.
|
|
22
|
+
|
|
23
|
+
--strict Warnings fail too (exit 1)
|
|
24
|
+
--format text|github|json
|
|
25
|
+
text: path:line: severity: message (default)
|
|
26
|
+
github: annotations on a pull request
|
|
27
|
+
json: an array of { file, diagnostics }
|
|
28
|
+
`,
|
|
29
|
+
format: `Usage: barmark format <paths…> [options]
|
|
30
|
+
|
|
31
|
+
Charts in standard form. With one chart and no option, prints it.
|
|
32
|
+
|
|
33
|
+
--write Rewrite each chart that changes, and list it
|
|
34
|
+
--check List each chart that would change; exit 1 if any
|
|
35
|
+
`,
|
|
36
|
+
render: `Usage: barmark render <file> [options]
|
|
37
|
+
|
|
38
|
+
A standalone HTML page of the chart, with the Edwin font embedded.
|
|
39
|
+
|
|
40
|
+
--out <file> Write to a file instead of stdout
|
|
41
|
+
--theme auto|light|dark Colors (default auto: follows the reader's system)
|
|
42
|
+
--numerals nashville|roman
|
|
43
|
+
Show chords as degrees of the key
|
|
44
|
+
--no-bar-numbers Hide bar numbers
|
|
45
|
+
--rehearsal-letters Letter each section A, B, C…
|
|
46
|
+
--no-wrap Keep each row on one line
|
|
47
|
+
--diagnostics List the chart's problems below it
|
|
48
|
+
--font-url <url> Link the font instead of embedding it
|
|
49
|
+
`,
|
|
50
|
+
transpose: `Usage: barmark transpose <file> (--to <key> | --semitones <n>) [options]
|
|
51
|
+
|
|
52
|
+
The chart in another key. Problems go to stderr.
|
|
53
|
+
|
|
54
|
+
--to <key> The new key, e.g. E or Bb
|
|
55
|
+
--semitones <n> Move by a whole number of semitones, e.g. -2
|
|
56
|
+
--out <file> Write to a file instead of stdout
|
|
57
|
+
`,
|
|
58
|
+
};
|
package/dist/inputs.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** The charts a command reads: named files, every chart under named folders, and stdin. */
|
|
2
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { join, relative, resolve } from "node:path";
|
|
4
|
+
import { UsageError } from "./io.js";
|
|
5
|
+
const STDIN = "<stdin>";
|
|
6
|
+
/**
|
|
7
|
+
* Every `*.chart.md` under a folder, sorted, skipping node_modules and dot-folders.
|
|
8
|
+
* Symlinks are skipped too, so `format --write` never rewrites a file outside the folder.
|
|
9
|
+
*/
|
|
10
|
+
function chartsUnder(dir) {
|
|
11
|
+
const found = [];
|
|
12
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
13
|
+
const path = join(dir, entry.name);
|
|
14
|
+
if (entry.isSymbolicLink())
|
|
15
|
+
continue;
|
|
16
|
+
if (entry.isDirectory()) {
|
|
17
|
+
if (entry.name !== "node_modules" && !entry.name.startsWith("."))
|
|
18
|
+
found.push(...chartsUnder(path));
|
|
19
|
+
}
|
|
20
|
+
else if (entry.name.endsWith(".chart.md"))
|
|
21
|
+
found.push(path);
|
|
22
|
+
}
|
|
23
|
+
return found.sort();
|
|
24
|
+
}
|
|
25
|
+
/** The files a path names: itself, or the charts under it. */
|
|
26
|
+
function filesFor(path, io, folders) {
|
|
27
|
+
const full = resolve(io.cwd, path);
|
|
28
|
+
const stat = statSync(full, { throwIfNoEntry: false });
|
|
29
|
+
if (!stat)
|
|
30
|
+
throw new UsageError(`cannot read ${path}`);
|
|
31
|
+
if (!stat.isDirectory())
|
|
32
|
+
return [full];
|
|
33
|
+
if (!folders)
|
|
34
|
+
throw new UsageError(`${path} is a folder`);
|
|
35
|
+
const charts = chartsUnder(full);
|
|
36
|
+
if (!charts.length)
|
|
37
|
+
throw new UsageError(`no charts found in ${path}`);
|
|
38
|
+
return charts;
|
|
39
|
+
}
|
|
40
|
+
function readText(full, shown) {
|
|
41
|
+
try {
|
|
42
|
+
return readFileSync(full, "utf8");
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
throw new UsageError(`cannot read ${shown}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export async function readCharts(paths, io, opts) {
|
|
49
|
+
if (!paths.length)
|
|
50
|
+
throw new UsageError("no chart given");
|
|
51
|
+
const charts = [];
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
for (const path of paths) {
|
|
54
|
+
if (path === "-") {
|
|
55
|
+
if (seen.has(STDIN))
|
|
56
|
+
throw new UsageError("- (stdin) given twice");
|
|
57
|
+
seen.add(STDIN);
|
|
58
|
+
charts.push({ path: STDIN, text: await io.readStdin() });
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const full of filesFor(path, io, opts.folders)) {
|
|
62
|
+
if (seen.has(full))
|
|
63
|
+
continue;
|
|
64
|
+
seen.add(full);
|
|
65
|
+
const shown = full === resolve(io.cwd, path) ? path : relative(io.cwd, full);
|
|
66
|
+
charts.push({ path: shown, text: readText(full, shown) });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return charts;
|
|
70
|
+
}
|
package/dist/io.js
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** The barmark command: dispatch to a subcommand, and turn every failure into one line and an exit code. */
|
|
2
|
+
import { check } from "./check.js";
|
|
3
|
+
import { format } from "./format.js";
|
|
4
|
+
import { COMMAND_HELP, HELP } from "./help.js";
|
|
5
|
+
import { UsageError } from "./io.js";
|
|
6
|
+
import { render } from "./render.js";
|
|
7
|
+
import { transpose } from "./transpose.js";
|
|
8
|
+
/** Each subcommand by name. */
|
|
9
|
+
export const COMMANDS = { check, format, render, transpose };
|
|
10
|
+
export async function main(argv, io) {
|
|
11
|
+
const [name, ...args] = argv;
|
|
12
|
+
if (name === "--version" || name === "-v") {
|
|
13
|
+
const { cli, barmark, render } = io.versions;
|
|
14
|
+
io.stdout(`@barmark/cli ${cli} (barmark ${barmark}, @barmark/render ${render})\n`);
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
if (name === "--help" || name === "-h") {
|
|
18
|
+
io.stdout(HELP);
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
if (name === undefined) {
|
|
22
|
+
io.stderr(HELP);
|
|
23
|
+
return 2;
|
|
24
|
+
}
|
|
25
|
+
// Own keys only: "constructor" and friends are not commands.
|
|
26
|
+
const command = Object.hasOwn(COMMANDS, name) ? COMMANDS[name] : undefined;
|
|
27
|
+
const help = Object.hasOwn(COMMAND_HELP, name) ? COMMAND_HELP[name] : undefined;
|
|
28
|
+
if (help && (args.includes("--help") || args.includes("-h"))) {
|
|
29
|
+
io.stdout(help);
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (!command)
|
|
34
|
+
throw new UsageError(`unknown command "${name}"; run barmark --help`);
|
|
35
|
+
return await command(args, io);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
io.stderr(`barmark: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
39
|
+
return 2;
|
|
40
|
+
}
|
|
41
|
+
}
|
package/dist/node-io.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** The Io a Node install runs with: real streams, the installed font and the installed versions. */
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/** The version in the package.json two folders above a package's entry (`dist/index.js`). */
|
|
5
|
+
function versionAt(entry) {
|
|
6
|
+
const manifest = fileURLToPath(new URL("../package.json", import.meta.resolve(entry)));
|
|
7
|
+
return JSON.parse(readFileSync(manifest, "utf8")).version;
|
|
8
|
+
}
|
|
9
|
+
function versions() {
|
|
10
|
+
const own = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
11
|
+
return { cli: own.version, barmark: versionAt("barmark"), render: versionAt("@barmark/render") };
|
|
12
|
+
}
|
|
13
|
+
async function readStdin() {
|
|
14
|
+
const chunks = [];
|
|
15
|
+
for await (const chunk of process.stdin)
|
|
16
|
+
chunks.push(Buffer.from(chunk));
|
|
17
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
18
|
+
}
|
|
19
|
+
/** A reader that stops early (`barmark render x | head`) is not an error: stop quietly. */
|
|
20
|
+
function quietOnClosedPipe(stream) {
|
|
21
|
+
stream.on("error", (error) => {
|
|
22
|
+
if (error.code !== "EPIPE")
|
|
23
|
+
throw error;
|
|
24
|
+
process.exit(process.exitCode ?? 0);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** The process's streams and folder: the part of an Io every runtime shares. */
|
|
28
|
+
export function processIo() {
|
|
29
|
+
quietOnClosedPipe(process.stdout);
|
|
30
|
+
quietOnClosedPipe(process.stderr);
|
|
31
|
+
return {
|
|
32
|
+
stdout: (text) => process.stdout.write(text),
|
|
33
|
+
stderr: (text) => process.stderr.write(text),
|
|
34
|
+
readStdin,
|
|
35
|
+
cwd: process.cwd(),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function nodeIo() {
|
|
39
|
+
return {
|
|
40
|
+
...processIo(),
|
|
41
|
+
font: () => readFileSync(fileURLToPath(import.meta.resolve("@barmark/render/fonts/Edwin-Roman.otf"))),
|
|
42
|
+
versions: versions(),
|
|
43
|
+
};
|
|
44
|
+
}
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Where a command's result goes: stdout, or the file named by --out. */
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
5
|
+
import { UsageError } from "./io.js";
|
|
6
|
+
export function writeOutput(text, out, io) {
|
|
7
|
+
if (out === undefined) {
|
|
8
|
+
io.stdout(text);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
writeFileSync(resolve(io.cwd, out), text);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new UsageError(`cannot write ${out}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Replaces a file whole: the text goes to a temporary file beside it, with
|
|
20
|
+
* its permissions, which then takes its name. A failure part-way leaves the
|
|
21
|
+
* old file as it was, never half-written. The temporary name is random and
|
|
22
|
+
* opened exclusively ("wx"), so nothing already there, a planted link
|
|
23
|
+
* included, is ever written through or removed.
|
|
24
|
+
*/
|
|
25
|
+
export function replaceFile(path, text, io) {
|
|
26
|
+
const target = resolve(io.cwd, path);
|
|
27
|
+
const temp = join(dirname(target), `.${basename(target)}.${randomBytes(8).toString("hex")}.tmp`);
|
|
28
|
+
let created = false;
|
|
29
|
+
try {
|
|
30
|
+
writeFileSync(temp, text, { flag: "wx", mode: statSync(target).mode & 0o777 });
|
|
31
|
+
created = true;
|
|
32
|
+
renameSync(temp, target);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
if (created)
|
|
36
|
+
rmSync(temp, { force: true });
|
|
37
|
+
throw new UsageError(`cannot write ${path}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The one chart a single-chart command reads. */
|
|
41
|
+
export function onlyChart(charts, command) {
|
|
42
|
+
const [chart, ...rest] = charts;
|
|
43
|
+
if (!chart || rest.length)
|
|
44
|
+
throw new UsageError(`${command} takes one chart`);
|
|
45
|
+
return chart;
|
|
46
|
+
}
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** barmark render: a chart as a standalone HTML page, with Edwin embedded unless linked. */
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { renderChart } from "@barmark/render";
|
|
4
|
+
import { parseChart } from "barmark";
|
|
5
|
+
import { oneOf, parseFlags } from "./args.js";
|
|
6
|
+
import { readCharts } from "./inputs.js";
|
|
7
|
+
import { onlyChart, writeOutput } from "./output.js";
|
|
8
|
+
const THEMES = ["auto", "light", "dark"];
|
|
9
|
+
const NUMERALS = ["nashville", "roman"];
|
|
10
|
+
const escapeHtml = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
11
|
+
function htmlDocument(title, body) {
|
|
12
|
+
return `<!doctype html>
|
|
13
|
+
<html lang="en">
|
|
14
|
+
<head>
|
|
15
|
+
<meta charset="utf-8">
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
17
|
+
<title>${escapeHtml(title)}</title>
|
|
18
|
+
</head>
|
|
19
|
+
<body>
|
|
20
|
+
${body}
|
|
21
|
+
</body>
|
|
22
|
+
</html>
|
|
23
|
+
`;
|
|
24
|
+
}
|
|
25
|
+
export async function render(args, io) {
|
|
26
|
+
const { values, positionals } = parseFlags(args, {
|
|
27
|
+
out: { type: "string" },
|
|
28
|
+
theme: { type: "string", default: "auto" },
|
|
29
|
+
numerals: { type: "string" },
|
|
30
|
+
"no-bar-numbers": { type: "boolean", default: false },
|
|
31
|
+
"rehearsal-letters": { type: "boolean", default: false },
|
|
32
|
+
"no-wrap": { type: "boolean", default: false },
|
|
33
|
+
diagnostics: { type: "boolean", default: false },
|
|
34
|
+
"font-url": { type: "string" },
|
|
35
|
+
});
|
|
36
|
+
const options = {
|
|
37
|
+
fontUrl: values["font-url"] ?? `data:font/otf;base64,${Buffer.from(io.font()).toString("base64")}`,
|
|
38
|
+
};
|
|
39
|
+
const theme = oneOf("theme", values.theme, THEMES);
|
|
40
|
+
if (theme !== "auto")
|
|
41
|
+
options.theme = theme;
|
|
42
|
+
if (values.numerals !== undefined)
|
|
43
|
+
options.numerals = oneOf("numerals", values.numerals, NUMERALS);
|
|
44
|
+
if (values["no-bar-numbers"])
|
|
45
|
+
options.barNumbers = false;
|
|
46
|
+
if (values["rehearsal-letters"])
|
|
47
|
+
options.rehearsalLetters = true;
|
|
48
|
+
if (values["no-wrap"])
|
|
49
|
+
options.wrap = false;
|
|
50
|
+
if (values.diagnostics)
|
|
51
|
+
options.diagnostics = true;
|
|
52
|
+
const { path, text } = onlyChart(await readCharts(positionals, io, { folders: false }), "render");
|
|
53
|
+
const title = parseChart(text).title ?? basename(path).replace(/\.chart\.md$|\.md$/, "");
|
|
54
|
+
writeOutput(htmlDocument(title, renderChart(text, options)), values.out, io);
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function textLine(path, d) {
|
|
2
|
+
return `${path}:${d.line}: ${d.severity}: ${d.message}`;
|
|
3
|
+
}
|
|
4
|
+
/** GitHub's workflow-command escaping for a message. */
|
|
5
|
+
const escapeData = (text) => text.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
|
|
6
|
+
export function githubLine(path, d) {
|
|
7
|
+
const file = escapeData(path).replace(/:/g, "%3A").replace(/,/g, "%2C");
|
|
8
|
+
return `::${d.severity} file=${file},line=${d.line}::${escapeData(d.message)}`;
|
|
9
|
+
}
|
|
10
|
+
const count = (n, noun) => `${n} ${noun}${n === 1 ? "" : "s"}`;
|
|
11
|
+
export function summary(files, errors, warnings) {
|
|
12
|
+
return `${count(files, "file")}, ${count(errors, "error")}, ${count(warnings, "warning")}`;
|
|
13
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** barmark transpose: a chart in another key, by key name or by semitones. */
|
|
2
|
+
import { transposeChart } from "barmark";
|
|
3
|
+
import { parseFlags } from "./args.js";
|
|
4
|
+
import { readCharts } from "./inputs.js";
|
|
5
|
+
import { UsageError } from "./io.js";
|
|
6
|
+
import { onlyChart, writeOutput } from "./output.js";
|
|
7
|
+
import { textLine } from "./report.js";
|
|
8
|
+
/** `--semitones -2`: join a negative number to its flag, which parseArgs would read as a flag. */
|
|
9
|
+
const joinNegative = (args) => args.flatMap((arg, i) => arg === "--semitones" && /^-\d/.test(args[i + 1] ?? "")
|
|
10
|
+
? [`--semitones=${args[i + 1]}`]
|
|
11
|
+
: i > 0 && args[i - 1] === "--semitones" && /^-\d/.test(arg)
|
|
12
|
+
? []
|
|
13
|
+
: [arg]);
|
|
14
|
+
function moveOf(to, semitones) {
|
|
15
|
+
if (to !== undefined && semitones !== undefined)
|
|
16
|
+
throw new UsageError("give --to or --semitones, not both");
|
|
17
|
+
if (to !== undefined)
|
|
18
|
+
return { to };
|
|
19
|
+
if (semitones === undefined)
|
|
20
|
+
throw new UsageError("give --to <key> or --semitones <n>");
|
|
21
|
+
if (!/^[+-]?\d+$/.test(semitones))
|
|
22
|
+
throw new UsageError(`--semitones must be a whole number, not "${semitones}"`);
|
|
23
|
+
return { semitones: Number(semitones) };
|
|
24
|
+
}
|
|
25
|
+
export async function transpose(args, io) {
|
|
26
|
+
const { values, positionals } = parseFlags(joinNegative(args), {
|
|
27
|
+
to: { type: "string" },
|
|
28
|
+
semitones: { type: "string" },
|
|
29
|
+
out: { type: "string" },
|
|
30
|
+
});
|
|
31
|
+
const by = moveOf(values.to, values.semitones);
|
|
32
|
+
const { path, text } = onlyChart(await readCharts(positionals, io, { folders: false }), "transpose");
|
|
33
|
+
const result = transposeChart(text, by);
|
|
34
|
+
for (const d of result.diagnostics)
|
|
35
|
+
io.stderr(`${textLine(path, d)}\n`);
|
|
36
|
+
const failed = result.diagnostics.some((d) => d.severity === "error");
|
|
37
|
+
if (failed && values.out !== undefined)
|
|
38
|
+
io.stderr(`${values.out} not written: the chart has errors\n`);
|
|
39
|
+
else
|
|
40
|
+
writeOutput(result.text, values.out, io);
|
|
41
|
+
return failed ? 1 : 0;
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
"name": "@barmark/cli",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The barmark command: check, format, render and transpose barmark chord charts (.chart.md) from the terminal and in CI.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"barmark": "./dist/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"CHANGELOG.md"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=20.6"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"barmark",
|
|
21
|
+
"chord-chart",
|
|
22
|
+
"chords",
|
|
23
|
+
"music",
|
|
24
|
+
"cli",
|
|
25
|
+
"formatter",
|
|
26
|
+
"linter"
|
|
27
|
+
],
|
|
28
|
+
"author": "Kevin Peckham",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"homepage": "https://github.com/kevinpeckham/barmark/tree/main/packages/cli#readme",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/kevinpeckham/barmark.git",
|
|
34
|
+
"directory": "packages/cli"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/kevinpeckham/barmark/issues"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"barmark": "0.9.0",
|
|
41
|
+
"@barmark/render": "0.7.2"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
45
|
+
"prepack": "bun run build",
|
|
46
|
+
"compile": "bun scripts/compile.ts"
|
|
47
|
+
}
|
|
11
48
|
}
|