Skip to content

Commit

Permalink
store examples path, fixes #74, #76
Browse files Browse the repository at this point in the history
  • Loading branch information
akaJes committed Oct 5, 2018
1 parent e38cd16 commit acd5f10
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
17 changes: 10 additions & 7 deletions app/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const seek4File = (file, paths, rel) =>
)

const copyFile = (from, to) =>
new Promise((resolve, reject) =>
fs.createReadStream(from)
new Promise((resolve, reject, stream) => (stream =
fs.createReadStream(from))
.on('error', reject)
.pipe(
fs.createWriteStream(to)
.on('finish', () => resolve(to))
.on('error', reject)
.on('open', () =>
stream.pipe(
fs.createWriteStream(to)
.on('finish', () => resolve(to))
.on('error', reject)
)
)
);

Expand All @@ -31,7 +33,7 @@ const uploadFiles = files =>
try {
return mctool
.makeCfg(file.path)
.then(mctool.makeHfile(root, file.name, store.vars.baseCfg))
.then(mctool.makeHfile(root, file.name, store.state.baseCfg))
.then(a => file.name)
} catch(e) {
console.error(e);
Expand Down Expand Up @@ -67,6 +69,7 @@ module.exports = {
uploadCopyFiles,
uploadFiles,
configFilesList,
configFilesListUpload,
configFiles,
getBoards,
getThermistors,
Expand Down
26 changes: 17 additions & 9 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ var http = require('http');
var https = require('https');
var ua = require('universal-analytics');
const {promisify, atob, walk, unique} = require('./helpers');
const {seek4File, copyFile, uploadCopyFiles, configFilesList, configFiles, getBoards, getThermistors} = require('./common');
const {seek4File, uploadCopyFiles, configFilesList, configFilesListUpload, getBoards, getThermistors} = require('./common');
var qr = require('qr-image');
var machineId = require('node-machine-id').machineId;

const store = require('./store');
store.mods.editor && (store.mods.editor.root = () => git.root())
const st = require('./store-tool');

var server = http.Server(app);
var visitor = ua('UA-99239389-1');
Expand Down Expand Up @@ -58,10 +59,10 @@ var get_cfg=()=>{
.map(i => Object.assign(defs.defs[i], {select: a.select, type: "select"}))
return defs;
})
var list=['Configuration.h','Configuration_adv.h']
var list = configFilesListUpload
.map(f => base
.then(p=>
git.Show(p[1], path.join(store.vars.baseCfg, f)).catch(e => git.Show(p[1], path.join('Marlin', f)))
git.Show(p[1], path.join(store.state.baseCfg, f)).catch(e => git.Show(p[1], path.join('Marlin', f)))
.then(file=>mctool.getJson(p[0],file,p[1])(path.join(p[0],'Marlin',f)))
)
.then(o=>(o.names.filter(n=>hints.d2i(n.name),1).map(n=>o.defs[n.name].hint=!0),o))
Expand All @@ -80,19 +81,21 @@ app.get('/examples', function (req, res) {
return ex_dir()
.then(dir => (ex = dir))
.then(walk)
.then(a=>a.filter(i=>/Configuration(_adv)?\.h/.test(i)))
.then(files => files.filter(file => configFilesList.indexOf(path.basename(file)) >= 0))
.then(a=>a.map(i=>path.parse(path.relative(ex,i)).dir))
.then(unique)
.then(a => a.sort(sortNCS))
.catch(e => [])
.then(a=>(a.unshift('Marlin'),a))
.then(a => res.send({current: store.vars.baseCfg, list: a}))
.then(a => res.send({current: store.state.baseCfg, list: a}))
});

app.get('/set-base/:path', function (req, res) {
return Promise.resolve(atob(decodeURI(req.params.path)).toString())
.then(base => base == 'Marlin' && base || ex_dir(1).then(ex => path.join(ex, base)) )
.then(base => res.send(store.vars.baseCfg = base))
.then(base => store.state.baseCfg = base)
.then(st.write)
.then(base => res.send(store.state.baseCfg))
});

/* VERSION */
Expand Down Expand Up @@ -129,9 +132,14 @@ app.get('/version/:screen', function (req, res) {
ul:req.headers['accept-language'].split(',')[0],
}).send()
)
Promise.all([pio.isPIO().catch(() => false), git.root(), pioRoot().then(pioEnv).catch(e => [])])
.then(pp => {
var cfg = {pio: pp[0], version: pjson.version, root: pp[1], base: store.vars.baseCfg, env: pp[2]};
Promise.all([
pio.isPIO().catch(() => false),
git.root(),
pioRoot().then(pioEnv).catch(e => []),
st.read(),
])
.then(([pio, root, env]) => {
var cfg = {pio, version: pjson.version, root, base: store.state.baseCfg, env};
res.set('Content-Type', 'application/javascript').send("var config = " + JSON.stringify(cfg));
})
});
Expand Down
4 changes: 2 additions & 2 deletions app/services/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ router.get('/checkout-force', function (req, res) {
.then(root => Promise.all(
configFilesList
.map(f =>
copyFile(path.join(root, store.vars.baseCfg, f), path.join(root, 'Marlin', f))
copyFile(path.join(root, store.state.baseCfg, f), path.join(root, 'Marlin', f))
.catch(e => 'not found')
)
))
Expand All @@ -53,7 +53,7 @@ router.get('/checkout-force', function (req, res) {

git.Checkout('--force')
.then(rm)
.then(a => store.vars.baseCfg == 'Marlin' ? a : cp())
.then(a => store.state.baseCfg == 'Marlin' ? a : cp())
.then(a => res.send(a))
.catch(e => res.status(403).send(e))
});
Expand Down
18 changes: 18 additions & 0 deletions app/store-tool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const path = require('path');

const { promisify } = require('./helpers');
const git = require('./git-tool');
const store = require('./store');

exports.write = () =>
git.root()
.then(root => promisify(fs.writeFile)(path.join(root, store.config.store, 'config.json'), JSON.stringify({state: store.state}, 0, 2)))

exports.read = () =>
git.root()
.then(root => promisify(fs.readFile)(path.join(root, store.config.store, 'config.json'), 'utf8'))
.then(data => JSON.parse(data))
.then(a=>(console.log('read', a),a))
.then(data => Object.assign(store, data))
// .catch(e => e)
4 changes: 3 additions & 1 deletion app/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
exports.vars = {
httpPort: 3000,
httpsPort: 3002,
baseCfg: 'Marlin',
};

exports.state = {
baseCfg: 'Marlin',
}
exports.config = {
store: '.mct.bak',
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marlin-conf",
"version": "2.10.5",
"version": "2.10.6",
"description": "configuration tool for Marlin project",
"main": "./index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ $(function(){
// consoles menu
var wins = {};
function openWin(url, target) {
wins[target] && wins[target].closed && (wins[target] = 0);
(wins[target] || ( wins[target] = window.open(url, target))).focus();
}
$('.mct-consoles').on('click',function(){ openWin('consoles.html', 'consoles') });
Expand Down

0 comments on commit acd5f10

Please sign in to comment.