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

Fixed issue #38 (added more sophisticated code optimization) #39

Merged
merged 4 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ $ nodemcu-tool mkfs --port=/dev/ttyUSB0
**Hint** include the native [encoder Module](http://nodemcu.readthedocs.io/en/master/en/modules/encoder/) into your firmware to speed-up the uploading by factor 4..10!

```shell
$ nodemcu-tool upload --port=/dev/ttyUSB0 --optimize helloworld.lua
$ nodemcu-tool upload --port=/dev/ttyUSB0 --minify helloworld.lua
[NodeMCU-Tool] Connected
[NodeMCU] Version: 0.9.5 | ChipID: 0xd1aa | FlashID: 0x1640e0
[NodeMCU-Tool] Uploading "main.lua" ...
Expand Down Expand Up @@ -230,14 +230,14 @@ This will create a JSON based configuration file named `.nodemcutool` in your **

### Example Configuration ###

In this Example, the baudrate is changed to 19.2k and COM3 is selected as default port. Additionally the `--optimize` and `--compile` flags are set permanently.
In this Example, the baudrate is changed to 19.2k and COM3 is selected as default port. Additionally the `--minify` and `--compile` flags are set permanently.

```json
{
"baudrate": "19200",
"port": "COM3",
"compile": true,
"optimize": true,
"minify": true,
"keeppath": true
}
```
Expand All @@ -249,7 +249,8 @@ All configuration options are **optional**
* **baudrate** (int) - the default baudrate in bits per second
* **port** (string) - the comport to use
* **compile** (boolean) - compile lua files after upload
* **optimize** (boolean) - optimize files before uploading
* **minify** (boolean) - minifies files before uploading
* **optimize** (boolean) - optimize files before uploading (Deprecated! Use minify instead.)
* **keeppath** (boolean) - keep the relative file path in the destination filename (i.e: static/test.html will be named static/test.html)


Expand Down Expand Up @@ -325,7 +326,7 @@ Of course, check the [Examples](docs/Examples.md) file (tool usage) as well as t
#### The serial file transfer is pretty slow ####
By default, the serial connection uses a 9600 baud with 8N1 - this means maximal 960 bytes/s raw data rate.
Due to the limitations of a line-wise file upload, these maximal transfer rate cannot be reached, because every line has to be processed by the lua interpreter and NodeMCU-Tool is waiting for it's response.
It's recommended to use the `--optimize` flag to strip whitespaces before uploading. Additionally, newer firmware versions `1.x.x` using an auto-baudrate detection algorithm - this means you can increase the baudrate to e.g. 115200 `--baud 115200` to speed up the transfer
It's recommended to use the `--minify` flag to minify the code before uploading. Additionally, newer firmware versions `1.x.x` using an auto-baudrate detection algorithm - this means you can increase the baudrate to e.g. 115200 `--baud 115200` to speed up the transfer

Additionally include the native [encoder Module](http://nodemcu.readthedocs.io/en/master/en/modules/encoder/) into your firmware to speed-up the uploading by factor 4..10!

Expand Down
6 changes: 6 additions & 0 deletions bin/nodemcu-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function cliPrepare(options){

// command specific flags
optimize: options.optimize || false,
minify: options.minify || false,
compile: options.compile || false,
keeppath: options.keeppath || false,
remotename: options.remotename || null,
Expand All @@ -91,6 +92,7 @@ function cliPrepare(options){
// extract values
defaultConfig.baudrate = d.baudrate || defaultConfig.baudrate;
defaultConfig.port = d.port || defaultConfig.port;
defaultConfig.minify = (d.minify && d.minify === true);
defaultConfig.optimize = (d.optimize && d.optimize === true);
defaultConfig.compile = (d.compile && d.compile === true);
defaultConfig.keeppath = (d.keeppath && d.keeppath === true);
Expand Down Expand Up @@ -169,6 +171,9 @@ _cli
.command('upload [files...]')
.description('Upload Files to NodeMCU (ESP8266) target')

// file minification
.option('-m, --minify', 'Minifies the file before uploading', false)

// file cleanup
.option('-o, --optimize', 'Removes comments and empty lines from file before uploading', false)

Expand Down Expand Up @@ -346,6 +351,7 @@ _cli
_logger.error(err);
}else{
// set defaults
data.minify = false;
data.optimize = false;
data.compile = false;
data.keeppath = false;
Expand Down
7 changes: 4 additions & 3 deletions docs/CommandReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,18 @@ The most important task of this tool: upload local files to the module.

**Options**

* `--optimize` | Remove Comments, Whitespaces and empty lines from the file before upload
* `--optimize` | Deprecated! - Remove Comments, Whitespaces and empty lines from the file before upload
* `--minify` | Minifies the code before upload
* `--compile` | Compiles the uploaded .lua file into executable bytecode and removes the source .lua file (performance)
* `--keeppath` | Keeps the relative file path in the destination filename (i.e: static/test.html will be named static/test.html)
* `--remotename` | Set the destination file name

**Example 1**

Upload and optimize the file "test.lua"
Upload and minify the file "test.lua"

```shell
$ nodemcu-tool --port=/dev/ttyUSB1 --optimize --compile upload test.lua
$ nodemcu-tool --port=/dev/ttyUSB1 --minify --compile upload test.lua
[NodeMCU-Tool] Connected
[NodeMCU] Version: 0.9.5 | ChipID: 0xd1aa | FlashID: 0x1640e0
[NodeMCU-Tool] Uploading "toolkit/test.lua" ...
Expand Down
13 changes: 13 additions & 0 deletions lib/LuaMinifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var luamin = require('luamin');
// strip comments and whitespaces from lua content
function minifyLuaContent(rawContent){
// apply minification
var t = luamin.minify((rawContent.toString('utf-8')));

// re-convert to buffer
return new Buffer(t, 'utf-8');
};

module.exports = {
minify: minifyLuaContent
};
6 changes: 4 additions & 2 deletions lib/LuaOptimizer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

var _logger = require('logging-facility').getLogger('NodeMCU-Connector');
// strip comments and whitespaces from lua content
function optimizeLuaContent(rawContent){
// Deprication warning
_logger.warn('--optimize is deprecated. Please use --minify instead.')
// apply optimizations
var t = rawContent.toString('utf-8')
.replace(/--.*$/gim, '')
Expand All @@ -14,4 +16,4 @@ function optimizeLuaContent(rawContent){

module.exports = {
optimize: optimizeLuaContent
};
};
9 changes: 6 additions & 3 deletions lib/NodeMcuConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var _serialport = require('serialport');
var _fs = require('fs');
var _path = require('path');
var _luaOptimizer = require('./LuaOptimizer');
var _luaMinifier = require('./LuaMinifier');
var _luaCommandBuilder = require('./LuaCommandBuilder');

// NodeMCU-Connector Context Logger
Expand Down Expand Up @@ -205,9 +206,11 @@ NodeMcuConnector.prototype.upload = function(localName, remoteName, options, com

// get file content
var rawContent = _fs.readFileSync(localName);

// remove lua comments and empty lines ?
if (options.optimize && _path.extname(localName).toLowerCase() == '.lua'){
if (options.minify && _path.extname(localName).toLowerCase() == '.lua'){
// minify
rawContent = _luaMinifier.minify(rawContent);
} else if (options.optimize && _path.extname(localName).toLowerCase() == '.lua'){
// remove lua comments and empty lines ?
// apply optimizations
rawContent = _luaOptimizer.optimize(rawContent);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"colors": "^1.1.2",
"commander": "^2.9.0",
"logging-facility": "^1.1.0",
"luamin": "^1.0.4",
"prompt": "^0.2.14",
"serialport": "^4.0.0"
}
Expand Down