UNPKG

@sapphire/docusaurus-plugin-npm2yarn2pnpm

Version:

Docusaurus Remark plugin for converting NPM Commands to Yarn and Pnpm

411 lines (406 loc) 12.1 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/npm2bun.ts var unchangedCLICommands = ["init", "run", "test", "login", "logout", "link", "publish", "cache"]; function parseNpmInstall(command, isShortHand = false) { if (/^install *$/.test(command)) { return "install"; } return command.replace(isShortHand ? "i " : "install ", "add ").replace(/(\s*)--save(?!-)/, "$1--save-prod").replace("--no-package-lock", ""); } __name(parseNpmInstall, "parseNpmInstall"); var npmToBunTable = { install(command) { return parseNpmInstall(command); }, i(command) { return parseNpmInstall(command, true); }, uninstall(command) { return command.replace("uninstall", "remove").replace(/(\s*)--save(?!-)/, "$1--save-prod").replace("--no-package-lock", ""); }, version(command) { return command.replace(/(major|minor|patch)/, "--$1"); }, rebuild(command) { return command.replace("rebuild", "add --force"); } }; function convert(_, command) { command = (command ?? "").trim(); const firstCommand = (/\w+/.exec(command) || [""])[0]; if (unchangedCLICommands.includes(firstCommand)) { return `bun ${command}`; } if (firstCommand in npmToBunTable) { const converter = npmToBunTable[firstCommand]; if (typeof converter === "function") { return `bun ${converter(command)}`; } return `bun ${command.replace(firstCommand, converter)}`; } return `bun ${command} # couldn't auto-convert command`; } __name(convert, "convert"); function npmToBun(str) { return str.replace(/npm(?: +([^&\n\r]*))?/gm, convert); } __name(npmToBun, "npmToBun"); // src/npm2pnpm.ts var unchangedCLICommands2 = ["init", "run", "test", "login", "logout", "link", "publish", "cache"]; function parseNpmInstall2(command, isShortHand = false) { if (/^install *$/.test(command)) { return "install"; } return command.replace(isShortHand ? "i " : "install ", "add ").replace(/(\s*)--save(?!-)/, "$1--save-prod").replace("--no-package-lock", ""); } __name(parseNpmInstall2, "parseNpmInstall"); var npmToPnpmTable = { install(command) { return parseNpmInstall2(command); }, i(command) { return parseNpmInstall2(command, true); }, uninstall(command) { return command.replace("uninstall", "remove").replace(/(\s*)--save(?!-)/, "$1--save-prod").replace("--no-package-lock", ""); }, version(command) { return command.replace(/(major|minor|patch)/, "--$1"); }, rebuild(command) { return command.replace("rebuild", "add --force"); } }; function convert2(_, command) { command = (command ?? "").trim(); const firstCommand = (/\w+/.exec(command) || [""])[0]; if (unchangedCLICommands2.includes(firstCommand)) { return `pnpm ${command}`; } if (firstCommand in npmToPnpmTable) { const converter = npmToPnpmTable[firstCommand]; if (typeof converter === "function") { return `pnpm ${converter(command)}`; } return `pnpm ${command.replace(firstCommand, converter)}`; } return `pnpm ${command} # couldn't auto-convert command`; } __name(convert2, "convert"); function npmToPnpm(str) { return str.replace(/npm(?: +([^&\n\r]*))?/gm, convert2); } __name(npmToPnpm, "npmToPnpm"); // src/npm2yarn.ts var unchangedCLICommands3 = ["test", "login", "logout", "link", "publish", "cache"]; var yarnCLICommands = [ "init", "run", "add", "audit", "autoclean", "bin", "check", "config", "create", "dedupe", "generate-lock-entry", "global", "help", "import", "info", "install", "licenses", "list", "lockfile", "outdated", "owner", "pack", "policies", "prune", "remove", "self-update", "tag", "team", "link", "unlink", "upgrade", "upgrade-interactive", "version", "versions", "why", "workspace", "workspaces" ]; function parseNpmInstall3(command, isShortHand = false) { if (/^install *$/.test(command)) { return "install"; } let yarnAddCommand = command.replace(isShortHand ? "i " : "install ", "add ").replace("--save-dev", "--dev").replace(/\s*--save(?!-)/, "").replace("--no-package-lock", "--no-lockfile").replace("--save-optional", "--optional").replace("--save-exact", "--exact"); if (/ -(?:-global|g)(?![^\b])/.test(yarnAddCommand)) { yarnAddCommand = yarnAddCommand.replace(/ -(?:-global|g)(?![^\b])/, ""); yarnAddCommand = `global ${yarnAddCommand}`; } return yarnAddCommand; } __name(parseNpmInstall3, "parseNpmInstall"); var npmToYarnTable = { install(command) { return parseNpmInstall3(command); }, i(command) { return parseNpmInstall3(command, true); }, uninstall(command) { let yarnRemoveCommand = command.replace("uninstall", "remove").replace("--save-dev", "--dev").replace(/\s*--save(?!-)/, "").replace("--no-package-lock", "--no-lockfile"); if (/ -(?:-global|g)(?![^\b])/.test(yarnRemoveCommand)) { yarnRemoveCommand = yarnRemoveCommand.replace(/ -(?:-global|g)(?![^\b])/, ""); yarnRemoveCommand = `global ${yarnRemoveCommand}`; } return yarnRemoveCommand; }, rebuild(command) { return command.replace("rebuild", "add --force"); }, exec(command) { return command.replace(/^exec\s?([^\s]+)?(\s--\s--)?(.*)$/, (_, data, dash, rest) => { let result = ""; if (data && !unchangedCLICommands3.includes(data) && !yarnCLICommands.includes(data)) { result += data; } else { result += `run ${data || ""}`; } if (dash) { result += dash.replace(/^\s--/, ""); } if (rest) { result += rest; } return result; }); }, run(command) { return command.replace(/^run\s?([^\s]+)?(\s--\s--)?(.*)$/, (_, data, dash, rest) => { let result = ""; if (data && !unchangedCLICommands3.includes(data) && !yarnCLICommands.includes(data)) { result += data; } else { result += `run ${data || ""}`; } if (dash) { result += dash.replace(/^\s--/, ""); } if (rest) { result += rest; } return result; }); }, ls(command) { return command.replace(/^(ls|list)(.*)$/, (_1, _2, args) => { let result = "list"; if (args) { let ended = false; let packages = []; const items = args.split(" ").filter(Boolean); for (const item of items) { if (ended) { result += ` ${item}`; } else if (item.startsWith("-")) { result += ` --pattern "${packages.join("|")}"`; packages = []; ended = true; result += ` ${item}`; } else { packages.push(item); } } if (packages.length > 0) { result += ` --pattern "${packages.join("|")}"`; } return result; } return "list"; }); }, list(command) { return npmToYarnTable.ls(command); }, init(command) { if (/^init (?!-).*$/.test(command)) { return command.replace("init", "create"); } return command.replace(" --scope", ""); } }; function convert3(_, command) { command = (command || "").trim(); const firstCommand = (/\w+/.exec(command) || [""])[0]; if (unchangedCLICommands3.includes(firstCommand)) { return `yarn ${command}`; } if (firstCommand in npmToYarnTable) { const converter = npmToYarnTable[firstCommand]; if (typeof converter === "function") { return `yarn ${converter(command)}`; } return `yarn ${command.replace(firstCommand, converter)}`; } return `yarn ${command} # couldn't auto-convert command`; } __name(convert3, "convert"); function npmToYarn(str) { return str.replace(/npm(?: +([^&\n\r]*))?/gm, convert3); } __name(npmToYarn, "npmToYarn"); // src/npm2yarn2pnpm.ts function createAttribute(attributeName, attributeValue) { return { type: "mdxJsxAttribute", name: attributeName, value: attributeValue }; } __name(createAttribute, "createAttribute"); function createTabItem({ code, node, value }) { const [, highlight] = (node.meta ?? "").split("|"); return { type: "mdxJsxFlowElement", name: "TabItem", attributes: [createAttribute("value", value), createAttribute("label", value)], children: [ { type: node.type, lang: node.lang, value: code, meta: `${highlight} showLineNumbers` } ] }; } __name(createTabItem, "createTabItem"); var transformNode = /* @__PURE__ */ __name((node, options) => { const groupIdProp = { type: "mdxJsxAttribute", name: "groupId", value: "npm2yarn2pnpm" }; const npmCode = node.value; const yarnCode = npmToYarn(node.value); const pnpmCode = npmToPnpm(node.value); const bunCode = npmToBun(node.value); return [ { type: "mdxJsxFlowElement", name: "Tabs", ...options.sync && { attributes: [groupIdProp] }, children: [ createTabItem({ code: npmCode, node, value: "npm" }), createTabItem({ code: yarnCode, node, value: "yarn" }), createTabItem({ code: pnpmCode, node, value: "pnpm" }), ...options.convertToBun ? [ createTabItem({ code: bunCode, node, value: "bun" }) ] : [] ] } ]; }, "transformNode"); var isMdxEsmLiteral = /* @__PURE__ */ __name((node) => node.type === "mdxjsEsm", "isMdxEsmLiteral"); var isTabsImport = /* @__PURE__ */ __name((node) => isMdxEsmLiteral(node) && node.value.includes("@theme/Tabs"), "isTabsImport"); var isParent = /* @__PURE__ */ __name((node) => Array.isArray(node.children), "isParent"); var matchNode = /* @__PURE__ */ __name((node) => node.type === "code" && typeof node.meta === "string" && (node.meta ?? "").startsWith("npm2yarn2pnpm"), "matchNode"); function createImportNode() { return { type: "mdxjsEsm", value: "import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'", data: { estree: { type: "Program", body: [ { type: "ImportDeclaration", specifiers: [ { type: "ImportDefaultSpecifier", local: { type: "Identifier", name: "Tabs" } } ], source: { type: "Literal", value: "@theme/Tabs", raw: "'@theme/Tabs'" } }, { type: "ImportDeclaration", specifiers: [ { type: "ImportDefaultSpecifier", local: { type: "Identifier", name: "TabItem" } } ], source: { type: "Literal", value: "@theme/TabItem", raw: "'@theme/TabItem'" } } ], sourceType: "module" } } }; } __name(createImportNode, "createImportNode"); var convertNpmToPackageManagers = /* @__PURE__ */ __name(({ sync = true, convertToBun = false } = { sync: true, convertToBun: false }) => async (root) => { const { visit } = await import('unist-util-visit'); let transformed = false; let alreadyImported = false; visit(root, (node) => { if (isTabsImport(node)) { alreadyImported = true; } if (isParent(node)) { let index = 0; while (index < node.children.length) { const child = node.children[index]; if (matchNode(child)) { const result = transformNode(child, { sync, convertToBun }); node.children.splice(index, 1, ...result); index += result.length; transformed = true; } else { index += 1; } } } }); if (transformed && !alreadyImported) { root.children.unshift(createImportNode()); } }, "convertNpmToPackageManagers"); export { convertNpmToPackageManagers, npmToBun, npmToPnpm, npmToYarn }; //# sourceMappingURL=out.js.map //# sourceMappingURL=index.mjs.map