libre-blocks
v0.1.0
Published
Share logic between apps to reduce development time
Maintainers
Readme
App: Libre Blocks
Share logic between apps. The goal of share logic is to reduce development time when coding similar apps. Developers can create a libre blocks and use it in different apps. Logic is served via ws and cached in memory during runtime.
CLI
The CLI is used to create a libre blocks. It can be used to create a libre blocks from scratch
- The CLI is fully interactive
- A run mode is selected when running the CLI for the first time
- CLI is exposed as bin "lb" or "libre-blocks"
Run mode
- Choose run mode "self-hosted" vs "cloud"
- setting is saved locally at ~/.libre-blocks/config.json
- do not ask for run mode if config.json exists and has a valid run mode
Self-hosted mode
- No login required
Cloud mode
- Not implemented (Show message) and exit
Interactive mode
Create a block
- Select "Create block" from the menu, specify a name and a content
- Save block to pwd/data/default/.json (create intermediate folders if needed)
- Show message "Block created successfully"
Note: If the block already exists, ask for confirmation to edit it
Configure command aliases
We can configure aliases for common commands to link them to a block. This is useful to create a development command for projects using libre blocks.
- Example 1: "dev" alias can be link to a block named "npm-run-dev" that will call "npm run dev" on the shell
Note: Block "npm-run-dev" should be a shell block using the "//!shell" prefix
- Alias can be CRUD
Non-interactive mode
Create a block
- sl create
Note: Find a way to support multiline content
Serve blocks
- sl serve
Execute alias
- sl run
Note: The alias links to a block and executes via ws like loading a block, so the ws server must be running Note: The ws url is configurable via SL_WS_URL or SL_URL || URL || (wsUrl field from config.js in ~/.libre-blocks/config.json) || ws://localhost:8080
CLI Note: Exposes ws server at ws://localhost:8080 (configurable via SL_WS_PORT or SL_PORT || PORT || 8080)
Block types
- Normal block: Run wrapped as a JS function
- Shell block: Run as a shell command: Prefix the block content with "//!shell" to indicate it should be run as a shell command
- Hybrid blocks: Run shell and js logic in the same block by prefixing the block content with "//!shell" to indicate it should be run as a shell command or "//!js" to indicate it should be run as a JS function.
Note: Hybrid blocks shell/js execution order can be configured via "//!executionOrder shell,js" or "//!executionOrder js,shell" to indicate the order of execution (Default to shell,js)
NodeJS API
Install
Libre blocks is accessible using an npm package "libre-blocks"
npm install libre-blocks (or npm link if you are in development mode)Usage
const libreBlocks = require('libre-blocks');Initialize
libreBlocks.setup({
wsUrl: 'ws://localhost:8080', //Optional, default is ws://localhost:8080
timeout: 5000 //Optional, default is 5000ms
});Note 1: If the server is not running, the setup will timeout with an error after the timeout specified
Note 2: Logging is available using the "debug" npm package with DEBUG=sl:* (or DEBUG=sl:setup for setup only)
Load a block
Logic blocks can be loaded using the block method
sl.block('logic-block-name');Example if the block content is "console.log('hello world');", the stdout will be "hello world"
Data Persistance
Local
Self-hosted mode
Blocks: Saved at pwd/data/default/.json (create intermediate folders if needed)
Note on loading blocks: The block can be js,shell,hybrid. If the block is js or hybrid, the js logic can be overridden creating a js file at pwd/js/.js. If the block is shell or hybrid, the shell logic can be overridden creating a shell file at pwd/shell/.sh. The extension specific files will override the block content if they exist. I the block is hybrid, the extension file will only override the logic of the specified extension.
Cloud mode
- Not implemented (Show message) and exit
Database
Not implemented
