Git Product home page Git Product logo

cross-cmd's Introduction

cross-cmd ✨

Cross-platform CLI commands

The problem

I was trying to create a CLI package using my linux system, and I wanted to trigger a command that reads a file. Being linux, I used cat and it worked perfectly. But, for people using my package on a different operating system like windows, they'd never be able to trigger a command like cat.

This Solution

This CLI package has many commands with different purposes mapped to different operating system commands. You give it a command and the required options, and it runs the appropriate command for the platform is used with the options provided.

Usage

This library comes in handy when creating a CLI package and you want to target various platforms.

Note that this is a javascript library.

Installation

npm i cross-cmd

In your project

Let's say you use spawnSync from the child_process module in NodeJS (it could be any function, as long as it executes terminal commands) and you're developing your package on a linux system:

const {spawnSync} = require('child_process');
spawnSync('node', ['./node_modules/cross-cmd', 'cat', 'path-to-file'], {
    stdio: "inherit"
})

What happens here is that cross-cmd checks its list of commands, gets the one with cat and appropriately apply the command for the platform it is being run on. For example, your package with this command executed on linux and windows respectively would be:

# linux
cat path-to-file
# windows
type path-to-file

This is because 'type' is a win32 representative associated with linux's 'cat' as saved in the list of commands.

cross-cmd is not installed globally, that's why it is accessed with node ./node_modules... instead of cross-cmd.... Just as I wouldn't want the users of this package to have it installed globally against their will, the users wouldn't also want the users of their packages to do the same.

If the command does not exist, it throws an error. When this happens, you can do me and the community a favor by creating a feature request issue of the command you want to be added.

For commands with spaces

e.g type nul > which creates an empty file in windows as touch does in linux and is saved in the command list, a windows user of this package would have to do the following:

const {spawnSync} = require('child_process');
spawnSync('node', ['./node_modules/cross-cmd', 'type nul >', 'path-to-file'], {
    stdio: "inherit"
})

cross-cmd changes the command 'type nul >' depending on the os.

All commands

It is important to confirm that a command exists before using it in your package. You can run the following to confirm that a command exists:

node ./node_modules cross_cmd <command-name> --help

Or find all commands available here.

Issues and Contributions

Your contribution to this project would be highly appreciated. Could be a documentation issue, pull request, feature request, they are all welcome.

LICENSE

MIT

cross-cmd's People

Contributors

dillionmegida avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.