Skip to content

Commit

Permalink
chore: add utility for highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-Karia committed Aug 10, 2024
1 parent 3eb18f1 commit b8f1959
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 22 deletions.
37 changes: 32 additions & 5 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { codeToHtml } from 'shiki/index.mjs';

var unchangedCLICommands = [
'test',
'login',
Expand Down Expand Up @@ -615,21 +617,46 @@ function npmToBun(_m, command) {
return "".concat(cmd, " ").concat(filtered.join(' ')).concat(cmd === 'npm' ? "\n# couldn't auto-convert command" : '').replace('=', ' ');
}

function highlight(command, theme) {
if (theme === void 0) { theme = "dark"; }
codeToHtml(command, {
lang: 'javascript',
theme: "github-".concat(theme)
}).then(function (html) {
return html;
});
return "";
}

/**
* Converts between npm and yarn command
*/
function convert(str, to) {
function convert(str, to, highlighting, theme) {
if (highlighting === void 0) { highlighting = false; }
if (theme === void 0) { theme = "dark"; }
if (to === 'npm') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM);
var convertedCommand = str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM);
if (highlighting)
return highlight(convertedCommand, theme);
return convertedCommand;
}
else if (to === 'pnpm') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm);
var convertedCommand = str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm);
if (highlighting)
return highlight(convertedCommand, theme);
return convertedCommand;
}
else if (to === 'bun') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun);
var convertedCommand = str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun);
if (highlighting)
return highlight(convertedCommand, theme);
return convertedCommand;
}
else {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
var convertedCommand = str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
if (highlighting)
return highlight(convertedCommand, theme);
return convertedCommand;
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

43 changes: 34 additions & 9 deletions dist/npm-to-yarn.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/npm-to-yarn.umd.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/types/highlight.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function highlight(command: string, theme?: "light" | "dark"): string;
2 changes: 1 addition & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Converts between npm and yarn command
*/
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string;
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun', highlighting?: boolean, theme?: "light" | "dark"): string;
12 changes: 12 additions & 0 deletions src/highlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { codeToHtml } from "shiki/index.mjs"

export function highlight (command: string, theme: "light" | "dark" = "dark"): string {
codeToHtml(command, {
lang: 'javascript',
theme: `github-${theme}`
}).then((html) => {
return html
})

return ""
}
28 changes: 23 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@ import { npmToYarn } from './npmToYarn'
import { npmToPnpm } from './npmToPnpm'
import { npmToBun } from './npmToBun'

import { highlight } from './highlight.js'

/**
* Converts between npm and yarn command
*/
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun', highlighting = false, theme : "light" | "dark" = "dark"): string {
if (to === 'npm') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM)
const convertedCommand = str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM)
if (highlighting)
return highlight(convertedCommand, theme)

return convertedCommand
} else if (to === 'pnpm') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm)
const convertedCommand = str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm)
if (highlighting)
return highlight(convertedCommand, theme)

return convertedCommand
} else if (to === 'bun') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun)
const convertedCommand = str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun)
if (highlighting)
return highlight(convertedCommand, theme)

return convertedCommand
} else {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn)
const convertedCommand = str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn)
if (highlighting)
return highlight(convertedCommand, theme)

return convertedCommand
}
}

0 comments on commit b8f1959

Please sign in to comment.