Skip to content

Commit

Permalink
Support reading and creating a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan committed May 14, 2024
1 parent d4185ba commit cd3a122
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.1.0](https://github.com/omrilotan/isbot/compare/v2.0.0...v2.1.0)

- Support reading and creating a config file (`colombo --config`)

## [2.0.0](https://github.com/omrilotan/isbot/compare/v1.3.7...v2.0.0)

- Support adding custom headers to HTTP requests (see help)
Expand Down
43 changes: 31 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# colombo [![](https://img.shields.io/npm/v/colombo.svg)](https://www.npmjs.com/package/colombo)

## 🕵️‍♂️ Use source-map to view Javascript source code from CLI
## 🕵️‍♂️ Use remote source-map to view Javascript source code from CLI

Run and follow instructions:

Expand All @@ -10,18 +10,37 @@ npx colombo

![](https://user-images.githubusercontent.com/516342/103102893-ef27da00-4626-11eb-928a-9c67c077520d.gif)

```bash
$ colombo [file[:line[:column]]] [options]
<!-- Edit man file to change the following content>
<!-- MAN START -->

```txt
NAME
colombo - Read source code from sourcemap location
SYNOPSIS
colombo [file[:line[:column]]] [options]
DESCRIPTION
Use remote source-map to view Javascript source code from CLI
Examples:
EXAMPLES
$ colombo https://cdn.example.com/app.d0892a2.js:1:9694
$ colombo https://cdn.example.com/app.d0892a2.js
$ colombo --help
$ colombo https://cdn.example.com/app.d0892a20d266460d6c63.js
$ colombo https://cdn.example.com/app.d0892a20d266460d6c63.js:1:9694
$ colombo https://cdn.example.com/app.d0892a20d266460d6c63.js:1:9694 -H "Access-Token: 1234"
OPTIONS
--help Show help
--version, -V Show version number
--config Read / Add persistant configuration
--header, -H "key: value" Add a header to the request
Options:
--header, -H "key: value" Add a header to the request
--version, -V Show version number
--help Show help
EXAMPLES
$ colombo --help
$ colombo https://cdn.example.com/app.d0892a2.js:1:9694 -H "Access-Token: 1234"
$ colombo --config
$ colombo --config -H "access-token: 1234"
```

<!-- MAN END -->

Persistant configuration is stored in `~/.colomborc`.
34 changes: 6 additions & 28 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node

import { parseArgs } from "node:util";
import { readFile } from "node:fs/promises";
import inquirer from "inquirer";
import chalk from "chalk";
import semver from "semver";
import { config } from "./lib/config/index.js";
import { getSourceCodeMapUrl } from "./lib/getSourceCodeMapUrl/index.js";
import { loader } from "./lib/loader/index.js";
import { update } from "./lib/update/index.js";
Expand All @@ -13,7 +13,6 @@ const { satisfies } = semver;
const { bold, red } = chalk;
const { prompt } = inquirer;

let updateMessage;
start();

async function start() {
Expand Down Expand Up @@ -49,35 +48,17 @@ async function start() {
)
.catch(() => null);

const {
values: { header = [], help = false, version: showVersion = false },
positionals: [arg = ""],
} = parseArgs({
args: process.argv.slice(2),
options: {
header: {
type: "string",
short: "H",
multiple: true,
},
version: {
type: "boolean",
short: "V",
},
help: {
type: "boolean",
},
},
allowPositionals: true,
strict: false,
});
const { headers, help, showVersion, arg, configured } = await config(
process.argv,
);

if (help) {
console.log(await readFile(new URL("./man", import.meta.url), "utf8"));
process.exit(0);
}
if (showVersion) {
console.log([name, version].join("@"));
}
if (configured || help || showVersion) {
process.exit(0);
}

Expand All @@ -95,9 +76,6 @@ async function start() {
match || {};
const clean = file.replace(/:\d+:\d+$/, "").replace(/\?.*/, "");
let url;
const headers = new Headers(
header.map((header) => header.split(":").map((value) => value.trim())),
);
if (!headers.has("User-Agent")) {
headers.set(
"User-Agent",
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export async function colombo({ url, column, line, headers }) {
}
}

/**
* Retrieve source map data from a remote URL
*/
async function getData(url, { headers } = {}) {
try {
const response = await fetch(url, { headers });
Expand Down
116 changes: 116 additions & 0 deletions lib/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { parseArgs } from "node:util";
import { homedir } from "node:os";
import { join } from "node:path";
import fs, { readFile, writeFile } from "node:fs/promises";
import inquirer from "inquirer";
import chalk from "chalk";

const { prompt } = inquirer;
const { green: add, red: remove, white: same } = chalk;

export async function config(argv) {
const configPath = join(homedir(), ".colomborc");
const fileExsits = await fs.stat(configPath).catch(() => false);
const configContent = fileExsits ? await readFile(configPath, "utf8") : "";

const configurations = configContent ? JSON.parse(configContent) : {};

const {
values: {
header = [],
help = false,
version: showVersion = false,
config = false,
},
positionals: [arg = ""],
} = parseArgs({
args: argv.slice(2),
options: {
header: {
type: "string",
short: "H",
multiple: true,
},
version: {
type: "boolean",
short: "V",
},
help: {
type: "boolean",
},
config: {
type: "boolean",
},
},
allowPositionals: true,
strict: false,
});

const headerEntries = [
...(configurations.headers || []).map((header) =>
Object.entries(header).at(0),
),
...header
.map((header) => header.split(":").map((value) => value.trim()))
.map(([key, value]) => [key.toLowerCase(), value]),
];

if (config) {
const newConfig = {};
if (headerEntries) {
newConfig.headers = headerEntries.map(([key, value]) => ({
[key]: value,
}));
}
const newConfigString = JSON.stringify(newConfig, null, 2);
if (
configContent &&
newConfigString &&
configContent.trim() !== newConfigString
) {
console.log(compare(configContent, newConfigString));
const { overwrite } = await prompt([
{
name: "overwrite",
message: "Overwrite existing configuration (Y/n)?",
type: "string",
default: "Yes",
},
]);
if (/^y/i.test(overwrite)) {
await writeFile(configPath, JSON.stringify(newConfig, null, 2));
}
} else if (configContent) {
console.log("Config:", "\n", configContent);
} else if (newConfigString) {
await writeFile(configPath, newConfigString);
console.log("New config:", "\n", newConfigString);
}
}

return {
arg,
configured: config,
headers: new Headers(headerEntries),
help,
showVersion,
};
}

function compare(before, after) {
const linesBefore = before.trim().split("\n");
const linesAfter = after.split("\n");
const linesCount = Math.max(linesBefore.length, linesAfter.length);
const output = ["Changes to config:"];
for (let index = 0; index < linesCount; index++) {
const lineBefore = linesBefore.at(index);
const lineAfter = linesAfter.at(index);
if (lineBefore === lineAfter) {
output.push(same(lineBefore || lineAfter));
} else {
lineBefore && output.push(remove(`- ${lineBefore}`));
lineAfter && output.push(add(`+ ${lineAfter}`));
}
}
return output.join("\n");
}
32 changes: 21 additions & 11 deletions man
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# colombo: Read source code from sourcemap location
NAME
colombo - Read source code from sourcemap location

$ colombo [file[:line[:column]]] [options]
SYNOPSIS
colombo [file[:line[:column]]] [options]

Examples:
DESCRIPTION
Use remote source-map to view Javascript source code from CLI

$ colombo --help
$ colombo https://cdn.example.com/app.d0892a20d266460d6c63.js
$ colombo https://cdn.example.com/app.d0892a20d266460d6c63.js:1:9694
$ colombo https://cdn.example.com/app.d0892a20d266460d6c63.js:1:9694 -H "Access-Token: 1234"
EXAMPLES
$ colombo https://cdn.example.com/app.d0892a2.js:1:9694
$ colombo https://cdn.example.com/app.d0892a2.js

Options:
--header, -H "key: value" Add a header to the request
--version, -V Show version number
--help Show help
OPTIONS
--help Show help
--version, -V Show version number
--config Read / Add persistant configuration
--header, -H "key: value" Add a header to the request


EXAMPLES
$ colombo --help
$ colombo https://cdn.example.com/app.d0892a2.js:1:9694 -H "Access-Token: 1234"
$ colombo --config
$ colombo --config -H "access-token: 1234"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colombo",
"version": "2.0.0",
"version": "2.1.0",
"description": "🕵️‍♂️ View Javascript source code using source-map from CLI",
"keywords": [
"sourcemap",
Expand Down
14 changes: 14 additions & 0 deletions scripts/man.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

import { readFile, writeFile } from "fs/promises";
import { join } from "path";

const root = join(new URL(".", import.meta.url).pathname, "..");
const manFile = await readFile(join(root, "man"), "utf8");
const readmeFile = await readFile(join(root, "README.md"), "utf8");
const updatedReadmeFile = readmeFile.replace(
/(<!-- MAN START -->)([\s\S]*?)(<!-- MAN END -->)/gim,
["$1", "```man", manFile.trim(), "```", "$3"].join("\n"),
);

await writeFile(join(root, "README.md"), updatedReadmeFile);

0 comments on commit cd3a122

Please sign in to comment.