|
| 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 | +} |
0 commit comments