Skip to content

Commit

Permalink
standalone version
Browse files Browse the repository at this point in the history
  • Loading branch information
akaJes committed May 29, 2017
1 parent a1ed141 commit fa99f01
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ coverage

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
dist

# Dependency directories
node_modules
Expand Down
2 changes: 1 addition & 1 deletion app/git-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ exports.clone=name=>new Promise((done,fail)=>{
clearInterval(timer);
console.log();
if (code == 0)
done();
done(name);
else
fail();
});
Expand Down
12 changes: 12 additions & 0 deletions app/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
var fs = require('fs');
var path = require('path');

Object.prototype.filter = function( predicate, obj ) {
var result = { };
obj = obj || this
for (var key in obj) {
if( obj.hasOwnProperty(key) && predicate( obj[key], key, obj ) ) {
result[key] = obj[key];
}
}
return result;
};


function promisify(func) {
return function() {
return new Promise((resolve, reject) => {
Expand Down
12 changes: 1 addition & 11 deletions app/mc-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var fs = require('fs');
var path = require('path');
var mc = require('./mc');
var ht = require('./helpers');

//common
var inFile=name=>new Promise((done, fail) => fs.readFile( name, 'utf8', (err, data) => err ? fail( err ) : done( data ) ) )
Expand All @@ -11,17 +12,6 @@ var parseJson=a=>JSON.parse(a);
var text2array=text=>text.split(/\r\n?|\n/);
var array2text=(a,text)=>(text='',a.forEach(i=>text+=i+'\n'),text.slice(0,-1));

Object.prototype.filter = function( predicate, obj ) {
var result = { };
obj = obj || this
for (var key in obj) {
if( obj.hasOwnProperty(key) && predicate( obj[key], key, obj ) ) {
result[key] = obj[key];
}
}
return result;
};

//workers

var onlyChanged=a=>a.filter(i=>i.changed);
Expand Down
46 changes: 27 additions & 19 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function SSEsend(event,data){
});
}
function serial_init(){
serial.changes().then(monitor=>{
return serial.changes().then(monitor=>{
monitor.on("created", function (f, stat) {
SSEsend('created',f)
// serial.list().then(a=>SSEsend('list',a));
Expand Down Expand Up @@ -134,7 +134,7 @@ app.get('/version/:screen', function (req, res) {
.then(a=>{
//console.log(a)
var s=JSON.stringify(a);
res.write(`var config={pio:${s},version:"${pjson.version}"}`);
res.write(`var config={pio:${s},version:"${pjson.version}"};`);
res.end();
})
});
Expand All @@ -146,7 +146,7 @@ app.get('/pio', function (req, res) {
});
});
function atob(b64string){
if (typeof Buffer.from === "function")
if ( process.version<"v6.0.0" )
// Node 5.10+
return Buffer.from(b64string, 'base64');
else
Expand Down Expand Up @@ -257,23 +257,31 @@ app.post('/set/:file/:name/:prop/:value', function (req, res) {
.then(a=>res.send(req.params))
.catch(a=>res.status(403).send(a))
})
function main(){
serial_init();
hints.init(1);
git.root()
.then(root=>{
fs.stat(path.join(root,'Marlin'),(e,a)=>{
if(!a)
console.log('this git not look like Marlin repository');
else
getPort(3000).then(port => {
server.listen(port, function () {
console.log('Marlin config tool started on port http://localhost:'+port);
});
opn('http://localhost:'+port+'/');
});
})
function main(noOpn){
return Promise.resolve()
.then(serial_init)
.catch(a=>console.error('serial failed'))
.then(()=>hints.init(1))
.catch(a=>console.error('hints failed'))
.then(()=>git.root())
.then(root=>promisify(fs.stat)(path.join(root,'Marlin')))
.catch(a=>{
var e=('this git not look like Marlin repository');
console.log(e);
throw e;
})
.then(()=>getPort(3000))
.then(port =>new Promise((done,fail)=>{
server.on('error',function(e){
fail(e)
})
server.listen(port, function () {
var url='http://localhost:'+port+'/';
console.log('Marlin config tool started on '+url);
done(url);
});
}))
.then(url=>(!noOpn&&opn(url),url));
}
module.exports.main=main;
require.main===module && main();
68 changes: 68 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';
const {app, BrowserWindow, Menu, dialog} = require('electron');
console.log('Node',process.version);

var which=require('which');
var path=require('path');
var promisify = require('./app/helpers').promisify;
var git = require('./app/git-tool');
var server = require('./app/server');

var mainWindow = null;
var getFolder=()=>new Promise((done,fail)=>
dialog.showOpenDialog({
title:'select folder with Marlin Firmware or empty folder to clone it',
properties:['openDirectory'],
},function(folders){
console.log();
if (!folders)
return fail({type:'fatal',message:'no folder selected'});
done(folders[0])
})
)
app.on('ready', function() {
var folder;
promisify(which)('git')
.catch(function(){
dialog.showErrorBox('Dependecies','Application needs GIT to be installed!')
throw {type:'fatal',message:'no Git installed'};
})
.then(function(){
var is={'-G':0}
.filter((v,key,o,p,i)=>(p=process.argv,i=p.indexOf(key),!v&&i>=0&&i+1<p.length&&(o[key]=p[i+1]),i>=0));
if (!is['-G'])
return getFolder();
return is['-G'];
})
.then(f=>folder=f)
.then(git.root)
.catch(function(e){
if (e&&e.type=="fatal")
throw e;
console.log('no repository found in this folder');
return git.clone(path.join(folder,'Marlin'));
})
.catch(function(e){
if (e&&e.type=="fatal")
throw e;
return git.root(path.join(folder,'Marlin'));
})
.then(()=>server.main(1))
.then(function(url){
mainWindow = new BrowserWindow({
height: 600,
width: 800
});
mainWindow.loadURL(url);
return;
dialog.showMessageBox({
type:"info",
title:"Git repository",
message:folder
})
})
.catch(function(e){
console.error(e.message)
app.quit();
})
});
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "marlin-conf",
"version": "2.5.6",
"version": "2.6.0",
"description": "configuration tool for Marlin project",
"main": "./index.js",
"scripts": {
"start": "electron . -G /home/jes/TEST",
"build": "build --dir",
"distw": "build -w --ia32 --x64",
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall-": "bower install"
},
Expand Down Expand Up @@ -41,6 +45,8 @@
"which": "^1.2.14"
},
"devDependencies-": {
"electron": "^1.6.10",
"electron-builder": "^18.1.0",
"nodemon": "^1.11.0"
}
}
12 changes: 6 additions & 6 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,22 @@ function main(){
}
function startWeb(){
git.root()
.then(root=>{
.then(root=>new Promise((done,fail)=>{
console.log('type: mct help if you need more information');
if(root){
var rl = getConsole();
rl.question('press Enter to run web browser or ^C to exit', (answer) => {
rl.close();
server.main()
server.main().then(done).catch(fail)
});
}
})
})).catch((e)=>{ console.log(e); })
}
function tryRun(){
process.chdir('Marlin')
console.log(process.cwd())
git.root('Marlin').then(a=>{
server.main();
});
git.root('Marlin')
.then(server.main);
}
git.root()
.then(startWeb)
Expand Down Expand Up @@ -149,4 +148,5 @@ commands:
`);
}
module.exports.main=main;
console.log('Node',process.version);
require.main===module && main();
1 change: 1 addition & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<!-- Bootstrap -->
<link href="libs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<script src="libs/jquery/dist/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="libs/tether/dist/js/tether.min.js"></script>
Expand Down

0 comments on commit fa99f01

Please sign in to comment.