Skip to content

Commit b731f07

Browse files
committed
rework services 2
1 parent da8c697 commit b731f07

File tree

12 files changed

+503
-428
lines changed

12 files changed

+503
-428
lines changed

app/common.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const git = require('./git-tool');
5+
const mctool = require('./mc-tool');
6+
const store = require('./store');
7+
const {getFirstFile} = require('./helpers');
8+
9+
const seek4File = (file, paths, rel) =>
10+
git.root()
11+
.then(root =>
12+
getFirstFile(paths.map(i => path.join(root, i, file)))
13+
.then(res => rel && path.relative(root, res) || res)
14+
)
15+
16+
const copyFile = (from, to) =>
17+
new Promise((resolve, reject) =>
18+
fs.createReadStream(from)
19+
.on('error', reject)
20+
.pipe(
21+
fs.createWriteStream(to)
22+
.on('finish', () => resolve(to))
23+
.on('error', reject)
24+
)
25+
);
26+
27+
const uploadFiles = files =>
28+
Promise.all(files.map(file =>
29+
git.root()
30+
.then(root => {
31+
try {
32+
return mctool
33+
.makeCfg(file.path)
34+
.then(mctool.makeHfile(root, file.name, store.vars.baseCfg))
35+
.then(a => file.name)
36+
} catch(e) {
37+
console.error(e);
38+
throw e;
39+
}
40+
})
41+
))
42+
43+
const configFiles = p => Promise.all(
44+
['Configuration.h', 'Configuration_adv.h', '_Bootscreen.h']
45+
.map(file => seek4File(file, p ? [p.replace(/\\/g, path.sep)] : ['Marlin', path.join('Marlin', 'src', 'config')]).catch(() => null))
46+
).then(files => files.filter(a => a));
47+
48+
const getBoards = () =>
49+
seek4File('boards.h', [ 'Marlin', path.join('Marlin', 'src', 'core')])
50+
.then(mctool.getBoards);
51+
52+
const getThermistors = () =>
53+
seek4File('thermistornames.h', ['Marlin', path.join('Marlin', 'src', 'lcd')])
54+
.then(mctool.getThermistors);
55+
56+
module.exports = {
57+
seek4File,
58+
copyFile,
59+
uploadFiles,
60+
configFiles,
61+
getBoards,
62+
getThermistors,
63+
}

app/helpers.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Object.prototype.filter = function( predicate, obj ) {
1313
return result;
1414
};
1515

16+
function atob(b64) {
17+
return process.version < "v6.0.0" ? Buffer.from(b64, 'base64') : new Buffer(b64, 'base64');
18+
}
1619

1720
function promisify(func,that) {
1821
return function() {
@@ -53,7 +56,21 @@ var walk = function(dir){
5356
})
5457
};
5558

59+
function getFirstFile(paths) {
60+
if (!paths || paths.length == 0)
61+
return Promise.reject();
62+
var filePath = paths.shift();
63+
return promisify(fs.access)(filePath, fs.constants.R_OK)
64+
.then(a => filePath)
65+
.catch(e => getFirstFile(paths) );
66+
}
67+
68+
const unique = a => a.filter((elem, index, self) => index == self.indexOf(elem))
69+
5670
module.exports = {
5771
promisify,
5872
walk,
73+
atob,
74+
getFirstFile,
75+
unique,
5976
};

0 commit comments

Comments
 (0)