-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support reading and creating a config file
- Loading branch information
Showing
8 changed files
with
196 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |