Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new conversions #59

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ function yarnToNPM(_m, command) {
return 'npm ' + command + "\n# couldn't auto-convert command";
}
}
function pnpmToNpm() {
return 'pnpm to npm';
}
function bunToNpm() {
return 'bun to npm';
}

function convertInstallArgs$1(args) {
if (args.includes('--global') || args.includes('-g')) {
Expand Down Expand Up @@ -352,6 +358,12 @@ function npmToYarn(_m, command) {
return 'npm ' + command + "\n# couldn't auto-convert command";
}
}
function pnpmToYarn() {
return 'pnpm to yarn';
}
function bunToYarn() {
return 'bun to yarn';
}

function convertPnpmInstallArgs(args) {
return args.map(function (item) {
Expand Down Expand Up @@ -488,6 +500,13 @@ function npmToPnpm(_m, command) {
return 'npm ' + command + "\n# couldn't auto-convert command";
}
}
function yarnToPnpm(_m, command) {
var npmCommand = yarnToNPM(_m, command);
return npmCommand.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm);
}
function bunToPnpm() {
return 'bun to pnpm';
}

function convertInstallArgs(args) {
// bun uses -g and --global flags
Expand Down Expand Up @@ -614,21 +633,95 @@ function npmToBun(_m, command) {
var filtered = args.filter(Boolean).filter(function (arg) { return arg !== '--'; });
return "".concat(cmd, " ").concat(filtered.join(' ')).concat(cmd === 'npm' ? "\n# couldn't auto-convert command" : '').replace('=', ' ');
}
function yarnToBun(_m, command) {
var npmCommand = yarnToNPM(_m, command);
return npmCommand.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun);
}
function pnpmToBun() {
return 'pnpm to bun';
}

/**
* Returns the current package manager
*/
function getManager(command) {
if (command.startsWith('yarn')) {
return 'yarn';
}
else if (command.startsWith('pnpm')) {
return 'pnpm';
}
else if (command.startsWith('bun')) {
return 'bun';
}
return 'npm';
}
/**
* Checks whether the command is other than npm command
*/
function isOtherManagerCommand(command) {
var currentManager = getManager(command);
return currentManager !== 'npm';
}

/**
* Converts between npm and yarn command
*/
function convert(str, to) {
if (to === 'npm') {
// pnpm | bun to npm
if (isOtherManagerCommand(str)) {
var manager = getManager(str);
if (manager === 'pnpm') {
return str.replace(/pnpm(?: +([^&\n\r]*))?/gm, pnpmToNpm);
}
else if (manager === 'bun') {
return str.replace(/bun(?: +([^&\n\r]*))?/gm, bunToNpm);
}
}
// yarn to npm
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM);
}
else if (to === 'pnpm') {
// yarn | bun to pnpm
if (isOtherManagerCommand(str)) {
var manager = getManager(str);
if (manager === 'yarn') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToPnpm);
}
else if (manager === 'bun') {
return str.replace(/bun(?: +([^&\n\r]*))?/gm, bunToPnpm);
}
}
// npm to pnpm
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm);
}
else if (to === 'bun') {
// yarn | pnpm to bun
if (isOtherManagerCommand(str)) {
var manager = getManager(str);
if (manager === 'yarn') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToBun);
}
else if (manager === 'pnpm') {
return str.replace(/pnpm(?: +([^&\n\r]*))?/gm, pnpmToBun);
}
}
// npm to bun
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun);
}
else {
// pnpm | bun to yarn
if (isOtherManagerCommand(str)) {
var manager = getManager(str);
if (manager === 'pnpm') {
return str.replace(/pnpm(?: +([^&\n\r]*))?/gm, pnpmToYarn);
}
else if (manager === 'bun') {
return str.replace(/bun(?: +([^&\n\r]*))?/gm, bunToYarn);
}
}
// npm to yarn
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

93 changes: 93 additions & 0 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.

8 changes: 8 additions & 0 deletions dist/types/lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Returns the current package manager
*/
export declare function getManager(command: string): 'npm' | 'yarn' | 'pnpm' | 'bun';
/**
* Checks whether the command is other than npm command
*/
export declare function isOtherManagerCommand(command: string): boolean;
4 changes: 4 additions & 0 deletions dist/types/toBun.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const bunCLICommands: readonly ["init", "run", "add", "pm", "help", "install", "remove", "upgrade", "version"];
export declare function npmToBun(_m: string, command: string): string;
export declare function yarnToBun(_m: string, command: string): string;
export declare function pnpmToBun(): string;
3 changes: 3 additions & 0 deletions dist/types/toNpm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare function yarnToNPM(_m: string, command: string): string;
export declare function pnpmToNpm(): string;
export declare function bunToNpm(): string;
3 changes: 3 additions & 0 deletions dist/types/toPnpm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare function npmToPnpm(_m: string, command: string): string;
export declare function yarnToPnpm(_m: string, command: string): string;
export declare function bunToPnpm(): string;
3 changes: 3 additions & 0 deletions dist/types/toYarn.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare function npmToYarn(_m: string, command: string): string;
export declare function pnpmToYarn(): string;
export declare function bunToYarn(): string;
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
testEnvironment: 'node',
testRegex: '(/test/.*|\\.(test|spec))\\.(ts|js)$',
moduleFileExtensions: ['ts', 'js'],
coveragePathIgnorePatterns: ['/node_modules/', '/test/'],
coveragePathIgnorePatterns: ['/node_modules/', '/test/', 'src/lib.ts'],
coverageThreshold: {
global: {
branches: 80,
Expand Down
58 changes: 54 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,69 @@
import { yarnToNPM } from './yarnToNpm'
import { npmToYarn } from './npmToYarn'
import { npmToPnpm } from './npmToPnpm'
import { npmToBun } from './npmToBun'
import { yarnToNPM, pnpmToNpm, bunToNpm } from './toNpm'
import { npmToYarn, pnpmToYarn, bunToYarn } from './toYarn'
import { npmToPnpm, yarnToPnpm, bunToPnpm } from './toPnpm'
import { npmToBun, yarnToBun, pnpmToBun } from './toBun'

import { getManager, isOtherManagerCommand } from './lib'

/**
* Converts between npm and yarn command
*/
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {
if (to === 'npm') {

// pnpm | bun to npm
if (isOtherManagerCommand(str)) {
const manager = getManager(str)
if (manager === 'pnpm') {
return str.replace(/pnpm(?: +([^&\n\r]*))?/gm, pnpmToNpm)
} else if (manager === 'bun') {
return str.replace(/bun(?: +([^&\n\r]*))?/gm, bunToNpm)
}
}

// yarn to npm
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM)
} else if (to === 'pnpm') {

// yarn | bun to pnpm
if (isOtherManagerCommand(str)) {
const manager = getManager(str)
if (manager === 'yarn') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToPnpm)
} else if (manager === 'bun') {
return str.replace(/bun(?: +([^&\n\r]*))?/gm, bunToPnpm)
}
}

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

// yarn | pnpm to bun
if (isOtherManagerCommand(str)) {
const manager = getManager(str)
if (manager === 'yarn') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToBun)
} else if (manager === 'pnpm') {
return str.replace(/pnpm(?: +([^&\n\r]*))?/gm, pnpmToBun)
}
}

// npm to bun
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun)
} else {

// pnpm | bun to yarn
if (isOtherManagerCommand(str)) {
const manager = getManager(str)
if (manager === 'pnpm') {
return str.replace(/pnpm(?: +([^&\n\r]*))?/gm, pnpmToYarn)
} else if (manager === 'bun') {
return str.replace(/bun(?: +([^&\n\r]*))?/gm, bunToYarn)
}
}

// npm to yarn
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn)
}
}
Loading
Loading