Coder Social home page Coder Social logo

baapan's Introduction

Baapan

Baapan brings the all of the NPM goodness right into your REPL. On-the-fly!!.

Using Node REPL for quick local testing is a common practice among JavaScript/Node developers. But the problem with the REPL is, you don't have the luxury to take advantage of the entire NPM ecosystem out-of-the-box.

An alternative is, using something like RunKit. But fiddling with sensitive data in an online tool is not the best choice for many developers.

While using Node REPL, if you feel you need an NPM module to quickly test something (e.g, lodash to do some quick object/array manipulation, uuid to quickly generate some uuid), you'll have to manually install it via NPM and load it onto the REPL. Here's how baapan makes it easy!!!

Baapan - the life saver

Baapan intercepts require() calls and automatically installs the module if the module is not locally available. You can require() whatever you want, and Baapan will require() it for you? Don't you think it's cool??

Getting Started

Step 1: Install baapan globally

Simply run:

npm install -g baapan

This will install baapan CLI command.

Step 2: Load Baapan

You can launch Baapan by just running baapan command on terminal after installation. baapan will launch the NodeJS REPL for you.

$ baapan

That's it. You can now require() everything you want!!

This time I need to generate a random IP address. I can require chance to do that.

Switching to workspace /Users/deepal/.baapan/workspace_13531_1583696915861
Workspace loaded!
> const chance = require('chance').Chance()
undefined
> chance.ip()
'213.15.210.129'

Baapan accepts any Node command line argument

We want baapan to be identical to the good old Node REPL as much as possible. Therefore, baapan supports ANY Node JS command line argument which the Node CLI supports. Even --help.

You can type baapan --help to see all the Node command line arguments supported by baapan. The output is nothing but a node --help.

Usage: node [options] [ script.js ] [arguments]
       node inspect [options] [ script.js | host:port ] [arguments]

Options:
  -                                   script read from stdin (default if no file name is
                                      provided, interactive mode if a tty)
  --                                  indicate the end of node options
  --abort-on-uncaught-exception       aborting instead of exiting causes a core file to
                                      be generated for analysis
  -c, --check                         syntax check script without executing
  --completion-bash                   print source-able bash completion script
  --cpu-prof                          Start the V8 CPU profiler on start up, and write
                                      the CPU profile to disk before exit. If
                                      --cpu-prof-dir is not specified, write the profile
                                      to the current working directory.
  --cpu-prof-dir=...                  Directory where the V8 profiles generated by
                                      --cpu-prof will be placed. Does not affect --prof.
...

One good part of this is, you'll have access to the features such as --experimental-repl-await command line argument

e.g,

$ baapan --experimental-repl-await
Switching to workspace /Users/deepal/.baapan/workspace_13531_1583696915861
Workspace loaded!
> await Promise.resolve('It works')
'It works'
>    

Baapan Workspace

Every instance of baapan REPL server has its own workspace independent of each other. All module installations are done within the boundary of the workspace. This helps you open multiple baapan REPLs at once, install different modules without any conflicts. Current workspace for the REPL is automatically cleaned-up when you gracefully exit the REPL shell (e.g, pressing ctrl+c twice or .exit command).

Note! If the REPL process was killed forcefully, the workspace directory will not be cleaned up automatically. You have to clean up these stale workspace directories manually.

Workspaces are by-default created in $HOME/.baapan/ directory. You can see the workspace directory for your REPL session by reading the BAAPAN_WS_PATH environment variable.

e.g,

> process.env.BAAPAN_WS_PATH
'/Users/djayasekara/.baapan/workspace_44023_1562678000424'

Persist Workspace And Modules

Baapan will not create a fresh workspace upon startup if the user has explicitly provided one using the BAAPAN_WS_PATH environment variable.

If it was provided explicitly, baapan will not clean up the workspace when the session is closed and you can persist the workspace and modules you install.

You can explicitly provide BAAPAN_WS_PATH as follows:

e.g.

Windows

$ set process.env.BAAPAN_WS_PATH=D:\nodejs\baapan-modules-repo
$ baapan
Switching to workspace D:\nodejs\baapan-modules-repo
Workspace loaded!
> process.env.BAAPAN_WS_PATH
'D:\\nodejs\\baapan-modules-repo'

Unix/Linux

$ BAAPAN_WS_PATH=/Users/johndoe/baapan-modules-repo baapan
Switching to workspace /Users/johndoe/baapan-modules-repo
Workspace loaded!
> process.env.BAAPAN_WS_PATH
'/Users/johndoe/baapan-modules-repo'

Feel free to drop any issues/feature requests/PRs at any time!!


baapan i.e, "බාපං" in Sinhala language is the translation for fetch/download 🇱🇰

Cool Text: Logo and Graphics Generator

baapan's People

Contributors

deepal avatar shahidcodes avatar vishmimoney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

baapan's Issues

Setup CI pipeline

  • Automate linting
  • Automate unit tests
  • Automate build
  • Publish to NPM automatically

Replace `baapan` function with `require` which makes more sense

Currently, modules should be installed/require'ed using baapan command as follows.

const lodash = await baapan('lodash');

Investigate the ability to replace this with a sane version as follows:

const lodash = require('lodash')

Baapan should intercept require calls and if the module is not available, install it immediately and require.

Concerns:

  • This should make require an async function which returns a promise.

Support for 3rd party NPM registries

We need a way to support 3rd party/Private NPM registries where the user should be able to set the NPM registry via an environment variable.

Can use NPM flag --registry in order to pass the registry on the fly when installing an NPM module.

The fallback registry should be https://registry.npmjs.org

User friendly error message when the module installation fails

The following error is thrown during installing a module when the module is not found in NPM. This error message is vague and does not indicate the specific error. This needs to be improved.

Creating workspace...
Initializing workspace...
Workspace loaded!
> require('ERTGB23456789sdf_')
Fetching and installing module 'ERTGB23456789sdf_' from npm...
Thrown:
{ Error: Command failed: npm install --silent ERTGB23456789sdf_
    at require (internal/modules/cjs/helpers.js:25:18)
    at Module.installModule [as require] (/Users/djayasekara/MyProjects/baapan/index.js:203:7)
    at installModule (/Users/djayasekara/MyProjects/baapan/index.js:73:3)
    at execSync (child_process.js:666:13)
    at checkExecSyncError (child_process.js:629:11)
  status: 1,
  signal: null,
  output: [ null, <Buffer >, <Buffer > ],
  pid: 7825,
  stdout: <Buffer >,
  stderr: <Buffer > }
>  

Add table of contents to readme

Readme is getting longer with new features, and becoming boring to read. Probably, it's time to add a ToC at the top of Readme.

Node >= v12.16.0 Cannot install a module without an index.js at root after require() has been called once

Steps to reproduce:

  • Install chance first
  • Install underscore second

underscore does not have an index.js at the module root. Instead, the entry point is configured via main attribute in package.json

> require('chance')
Fetching and installing module 'chance' from npm...
Done!
[Function: Chance] { Chance: [Circular] }
> require('underscore')
Fetching and installing module 'underscore' from npm...
Done!
Uncaught:
Error: Cannot find module '/Users/deepal/.baapan/workspace_90531_1584193257750/node_modules/underscore'
Require stack:
- <repl>
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at Module._module.Module.require (/Users/deepal/Projects/baapan/src/baapan.js:131:34)
    at require (internal/modules/cjs/helpers.js:77:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<repl>' ]
}
> 

This happens if the require() has been called once to install a module dynamically. Installation of the second module is successful, but it cannot be required and throws MODULE_NOT_FOUND error.

This issue is specific to Node v12.16.0 and above.

This issue is due to nodejs/node#29492

Node >= v12.3.0 Cannot require module with relative path

Resolving local modules with relative paths has broken in Node v12.3.0 probably due to nodejs/node#27598

$ ls src/baapan.js                                                                                                                  
src/baapan.js
$ /Users/deepal/.nvm/versions/node/v12.3.0/bin/node --require @babel/register src/entrypoint.js
Switching to workspace /Users/deepal/.baapan/workspace_47039_1584287356022
Workspace loaded!
> require('./src/baapan.js')
Thrown:
Error: Cannot find module './src/baapan.js'
Require stack:
- /Users/deepal/Projects/baapan/src/baapan.js
- /Users/deepal/Projects/baapan/src/index.js
- /Users/deepal/Projects/baapan/src/entrypoint.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
    at Function.resolve (internal/modules/cjs/helpers.js:21:19)
    at Module._module.Module.require (/Users/deepal/Projects/baapan/src/baapan.js:130:19)
    at require (internal/modules/cjs/helpers.js:16:16) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/deepal/Projects/baapan/src/baapan.js',
    '/Users/deepal/Projects/baapan/src/index.js',
    '/Users/deepal/Projects/baapan/src/entrypoint.js'
  ]
}
> 

Requiring with absolute path works fine

> require('/Users/deepal/Projects/baapan/src/baapan.js')
{ default: [Function: BaapanREPLServer] }
> 

Change log for Node v12.3.0 https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#2019-05-21-version-1230-current-bridgear

Ability to run scripts using baapan command

Proposed Feature

e.g. mytest.js

const lodash = require('lodash')
lodash.get({a: {b: 10}}, 'a.b')

Introduce the ability to run baapan command similar to node command in order to run JavaScript.

e.g,

baapan mytest.js

baapan should automatically download any required dependencies and run the script.

Alternative suggestion

This is currently possible by .load repl command as follows:

$ baapan
Creating workspace...
Initializing workspace...
Workspace loaded!
> .load mytest.js
const lodash = require('lodash')
lodash.get({a: {b: 10}}, 'a.b')

Fetching and installing module 'lodash' from npm...
Done!
10
>

rm not found on windows

getting error with
'rm' is not recognized as an internal or external command, operable program or batch file.

reusing the same workspace

I am trying to set BAAPAN_WS_PATH env variable to a previously created workspace of baapan but in env of baapan I see new workspace path everytime I run baapan. How can I persist the workspace?

Package installation is not isolated

baapan requires to be run from the user's home directory. If the user starts baapan on an npm project directory, all modules installed through baapan is installed in the project directory.
To fix this, the command should run on a dedicated directory (e.g, $HOME/.baapan). This will make the module installable via npm i -g or yarn global add

Support top level REPL await

Top level await is not supported since v2.x.x as Baapan is now implemented using repl module instead of a dynamically required module similar to node --require baapan.

Due to this, using --experimental-repl-await is not possible anymore in order to facilitate top level REPL await.

Sandbox Mode to instruct baapan not to 'require' modules outside workspace directory

The standard baapan REPL require resolves a required module path starting from the current directory's node_modules directory all the way up to the parent directories (and some additional directories) until the module is located. This behaviour is identical to the original Node REPL.

If baapan Sandbox mode is enabled, require will not try to resolve a 3rd party module's path outside the working directory. If the module is not installed in the workspace, it should try to install it to the workspace. However, this behaviour should not affect local modules (relative/absolute paths) or native modules

Cannot install winston on REPL due to a breaking change in readable-stream dependency

Cannot install winston on REPL and it throws the following error.

Creating workspace...
Initializing workspace...
Workspace loaded!
> const winston = require('winston')
Thrown:
{ Error: Cannot find module '/Users/djayasekara/.baapan/workspace/node_modules/readable-stream/writable'
    at require (internal/modules/cjs/helpers.js:25:18)
    at Module._module.Module.require (/Users/djayasekara/MyProjects/baapan/index.js:128:12)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) code: 'MODULE_NOT_FOUND' }
> 

It appears that, when winston is installed on REPL, it installs the latest version of readable-stream 3.4.0 (as of this writing). But winston fails trying to require readable-stream/writable module which is available in v2.4.1 of readable-stream.

Baapan VSCode Extension

VSCode extension should:

  • Automatically install modules as you type require()
  • (optional) Automatically uninstall the corresponding module when require() is removed?
  • The user should be able to enable/disable Baapan on the editor easily using the command palate command or a hotkey.
  • The status bar should display whether Baapan is enabled or not.

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.