Git Product home page Git Product logo

compile-run's Introduction

compile-run

  • This library works as a wrapper over the compilers installed in your system.
  • It provides APIs to execute programs by spawning child processes.
  • It has built in supports for types.
  • It can work with async/await and promises.

Supported Languages

  • C
  • Cpp
  • Java
  • JavaScript(Node.js env)
  • Python

Prerequisites

The following should be installed on your machine and be added to the path.

Language Software
C gcc
C++ gcc
Java jdk or jre
Python python
JavaScript(in node.js environment) node.js

Installation

You can install it by using npm like below.

npm install compile-run --save

Usage

It have 5 modules each for a language containing namely.

const {c, cpp, node, python, java} = require('compile-run');

Each module have 2 functions :-

1. runFile

This enables you to run a file and takes filepath as an argument with options and callback as optional arguments.

2. runSource

This enables you to directly execute a source code in a stored in a string. It takes source code as an argument with options and callback as optional arguments.

Examples:-

- Running a cpp source code file.

let resultPromise = cpp.runFile('E:\\abcd.cpp', { stdin:'3\n2 '});
resultPromise
    .then(result => {
        console.log(result);//result object
    })
    .catch(err => {
        console.log(err);
    });

- Running a python source code string.

const sourcecode = `print("Hell0 W0rld!")`;
let resultPromise = python.runSource(sourcecode);
resultPromise
    .then(result => {
        console.log(result);
    })
    .catch(err => {
        console.log(err);
    });

- Working with callback.

You can also use callback by passing it like -

  • with options
cpp.runFile('E:\\abcd.cpp', { stdin:'3\n2 '}, (err, result) => {
    if(err){
        console.log(err);
    }
    else{
        console.log(result);
    }
});
  • without options
cpp.runFile('E:\\abcd.cpp', (err, result) => {
    if(err){
        console.log(err);
    }
    else{
        console.log(result);
    }
});

Result

Result is an object with the following keys:-

  1. stdout <string> - stdout of the program execution. For empty stdout an empty string is returned.
  2. stderr <string> - stderr of the program execution, compilation or if public class name is not found in provided source string(In java). For empty stderr empty string is returned.
  3. exitCode <number> - exit code of the program.
  4. errorType <string|undefined> - It is set to the below values if there is some stderr or in case of a non-zero exitCode.
    1. 'pre-compile-time' - Only in case of java. Can be arised due to invalid public class name if using runSource for java.
    2. 'compile-time' - If some error has occured at the compile time.
    3. 'run-time' - If the error has occured at the run time.

Options

API's offer an optional options object which has following keys:-

  1. stdin <string> - Input/stdin you want to pass to the program.
  2. timeout <number> - timeout for program execution in milliseconds. Default is 3000 milliseconds.
  3. compileTimeout - timeout during compilation for c, cpp, java in milliseconds. Default is 3000 milliseconds. Would be ignored if passed for node or python

Compile-run 1.x.x

The versions < 2.x.x have been deprecated due to inconsistencies so its recommended to use version > 2.0.0.

You can find compile run version 1.x.x at https://github.com/vibhor1997a/compile-run.

compile-run's People

Contributors

vibhor1997a avatar sohailrajdev97 avatar

Watchers

James Cloos 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.