Skip to content

Commit acd5f10

Browse files
committed
store examples path, fixes #74, #76
1 parent e38cd16 commit acd5f10

File tree

7 files changed

+52
-20
lines changed

7 files changed

+52
-20
lines changed

app/common.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ const seek4File = (file, paths, rel) =>
1414
)
1515

1616
const copyFile = (from, to) =>
17-
new Promise((resolve, reject) =>
18-
fs.createReadStream(from)
17+
new Promise((resolve, reject, stream) => (stream =
18+
fs.createReadStream(from))
1919
.on('error', reject)
20-
.pipe(
21-
fs.createWriteStream(to)
22-
.on('finish', () => resolve(to))
23-
.on('error', reject)
20+
.on('open', () =>
21+
stream.pipe(
22+
fs.createWriteStream(to)
23+
.on('finish', () => resolve(to))
24+
.on('error', reject)
25+
)
2426
)
2527
);
2628

@@ -31,7 +33,7 @@ const uploadFiles = files =>
3133
try {
3234
return mctool
3335
.makeCfg(file.path)
34-
.then(mctool.makeHfile(root, file.name, store.vars.baseCfg))
36+
.then(mctool.makeHfile(root, file.name, store.state.baseCfg))
3537
.then(a => file.name)
3638
} catch(e) {
3739
console.error(e);
@@ -67,6 +69,7 @@ module.exports = {
6769
uploadCopyFiles,
6870
uploadFiles,
6971
configFilesList,
72+
configFilesListUpload,
7073
configFiles,
7174
getBoards,
7275
getThermistors,

app/server.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ var http = require('http');
1515
var https = require('https');
1616
var ua = require('universal-analytics');
1717
const {promisify, atob, walk, unique} = require('./helpers');
18-
const {seek4File, copyFile, uploadCopyFiles, configFilesList, configFiles, getBoards, getThermistors} = require('./common');
18+
const {seek4File, uploadCopyFiles, configFilesList, configFilesListUpload, getBoards, getThermistors} = require('./common');
1919
var qr = require('qr-image');
2020
var machineId = require('node-machine-id').machineId;
2121

2222
const store = require('./store');
2323
store.mods.editor && (store.mods.editor.root = () => git.root())
24+
const st = require('./store-tool');
2425

2526
var server = http.Server(app);
2627
var visitor = ua('UA-99239389-1');
@@ -58,10 +59,10 @@ var get_cfg=()=>{
5859
.map(i => Object.assign(defs.defs[i], {select: a.select, type: "select"}))
5960
return defs;
6061
})
61-
var list=['Configuration.h','Configuration_adv.h']
62+
var list = configFilesListUpload
6263
.map(f => base
6364
.then(p=>
64-
git.Show(p[1], path.join(store.vars.baseCfg, f)).catch(e => git.Show(p[1], path.join('Marlin', f)))
65+
git.Show(p[1], path.join(store.state.baseCfg, f)).catch(e => git.Show(p[1], path.join('Marlin', f)))
6566
.then(file=>mctool.getJson(p[0],file,p[1])(path.join(p[0],'Marlin',f)))
6667
)
6768
.then(o=>(o.names.filter(n=>hints.d2i(n.name),1).map(n=>o.defs[n.name].hint=!0),o))
@@ -80,19 +81,21 @@ app.get('/examples', function (req, res) {
8081
return ex_dir()
8182
.then(dir => (ex = dir))
8283
.then(walk)
83-
.then(a=>a.filter(i=>/Configuration(_adv)?\.h/.test(i)))
84+
.then(files => files.filter(file => configFilesList.indexOf(path.basename(file)) >= 0))
8485
.then(a=>a.map(i=>path.parse(path.relative(ex,i)).dir))
8586
.then(unique)
8687
.then(a => a.sort(sortNCS))
8788
.catch(e => [])
8889
.then(a=>(a.unshift('Marlin'),a))
89-
.then(a => res.send({current: store.vars.baseCfg, list: a}))
90+
.then(a => res.send({current: store.state.baseCfg, list: a}))
9091
});
9192

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

98101
/* VERSION */
@@ -129,9 +132,14 @@ app.get('/version/:screen', function (req, res) {
129132
ul:req.headers['accept-language'].split(',')[0],
130133
}).send()
131134
)
132-
Promise.all([pio.isPIO().catch(() => false), git.root(), pioRoot().then(pioEnv).catch(e => [])])
133-
.then(pp => {
134-
var cfg = {pio: pp[0], version: pjson.version, root: pp[1], base: store.vars.baseCfg, env: pp[2]};
135+
Promise.all([
136+
pio.isPIO().catch(() => false),
137+
git.root(),
138+
pioRoot().then(pioEnv).catch(e => []),
139+
st.read(),
140+
])
141+
.then(([pio, root, env]) => {
142+
var cfg = {pio, version: pjson.version, root, base: store.state.baseCfg, env};
135143
res.set('Content-Type', 'application/javascript').send("var config = " + JSON.stringify(cfg));
136144
})
137145
});

app/services/git.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ router.get('/checkout-force', function (req, res) {
3737
.then(root => Promise.all(
3838
configFilesList
3939
.map(f =>
40-
copyFile(path.join(root, store.vars.baseCfg, f), path.join(root, 'Marlin', f))
40+
copyFile(path.join(root, store.state.baseCfg, f), path.join(root, 'Marlin', f))
4141
.catch(e => 'not found')
4242
)
4343
))
@@ -53,7 +53,7 @@ router.get('/checkout-force', function (req, res) {
5353

5454
git.Checkout('--force')
5555
.then(rm)
56-
.then(a => store.vars.baseCfg == 'Marlin' ? a : cp())
56+
.then(a => store.state.baseCfg == 'Marlin' ? a : cp())
5757
.then(a => res.send(a))
5858
.catch(e => res.status(403).send(e))
5959
});

app/store-tool.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const { promisify } = require('./helpers');
5+
const git = require('./git-tool');
6+
const store = require('./store');
7+
8+
exports.write = () =>
9+
git.root()
10+
.then(root => promisify(fs.writeFile)(path.join(root, store.config.store, 'config.json'), JSON.stringify({state: store.state}, 0, 2)))
11+
12+
exports.read = () =>
13+
git.root()
14+
.then(root => promisify(fs.readFile)(path.join(root, store.config.store, 'config.json'), 'utf8'))
15+
.then(data => JSON.parse(data))
16+
.then(a=>(console.log('read', a),a))
17+
.then(data => Object.assign(store, data))
18+
// .catch(e => e)

app/store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
exports.vars = {
22
httpPort: 3000,
33
httpsPort: 3002,
4-
baseCfg: 'Marlin',
54
};
65

6+
exports.state = {
7+
baseCfg: 'Marlin',
8+
}
79
exports.config = {
810
store: '.mct.bak',
911
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "marlin-conf",
3-
"version": "2.10.5",
3+
"version": "2.10.6",
44
"description": "configuration tool for Marlin project",
55
"main": "./index.js",
66
"scripts": {

static/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ $(function(){
694694
// consoles menu
695695
var wins = {};
696696
function openWin(url, target) {
697+
wins[target] && wins[target].closed && (wins[target] = 0);
697698
(wins[target] || ( wins[target] = window.open(url, target))).focus();
698699
}
699700
$('.mct-consoles').on('click',function(){ openWin('consoles.html', 'consoles') });

0 commit comments

Comments
 (0)