1/************************************************************************************************** 2 * EJDB2 Node.js native API binding. 3 * 4 * MIT License 5 * 6 * Copyright (c) 2012-2021 Softmotions Ltd <info@softmotions.com> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in all 16 * copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 * SOFTWARE. 25 *************************************************************************************************/ 26 27const { promisify } = require('util'); 28const fs = require('fs'); 29const os = require('os'); 30const path = require('path'); 31const rimraf = promisify(require('rimraf')); 32const extract = promisify(require('extract-zip')); 33const readdir = promisify(fs.readdir); 34 35const utils = require('./utils'); 36const REVISION = require('./package.json')['revision']; 37 38function hasRevision() { 39 return REVISION && REVISION.length && REVISION != '@GIT_REVISION@'; 40} 41 42async function install() { 43 44 let out = await utils.runProcessAndGetOutput('cmake', ['--version']).catch(() => { 45 console.error('Unable to find executable'); 46 process.exit(1); 47 }); 48 console.log(out); 49 50 out = await utils.runProcessAndGetOutput('make', ['--version']).catch(() => { 51 console.error('Unable to find executable'); 52 process.exit(1); 53 }); 54 console.log(out); 55 56 console.log('Building EJDB2 native binding...'); 57 const wdir = await promisify(fs.mkdtemp)(path.join(os.tmpdir(), 'ejdb2-node')); 58 console.log(`Git revision: ${REVISION}`); 59 console.log(`Build temp dir: ${wdir}`); 60 61 let dist = path.join(wdir, 'dist.zip'); 62 await utils.download(`https://github.com/Softmotions/ejdb/archive/${REVISION}.zip`, dist); 63 await extract(dist, { dir: wdir }); 64 65 dist = (await readdir(wdir)).find(fn => fn.startsWith(`ejdb-${REVISION}`)); 66 if (dist == null) throw Error(`Invalid distrib dir ${wdir}`); 67 dist = path.join(wdir, dist); 68 69 const buildDir = path.join(dist, 'build'); 70 fs.mkdirSync(buildDir); 71 72 await utils.runProcess( 73 'cmake', 74 ['..', '-DCMAKE_BUILD_TYPE=Release', '-DBUILD_NODEJS_BINDING=ON', `-DNODE_BIN_ROOT=${__dirname}`], 75 buildDir); 76 await utils.runProcess('make', [], buildDir); 77 await rimraf(wdir); 78} 79 80if (process.platform.toLowerCase().indexOf('win') == 0) { // Windows system 81 console.error('Building for windows is currently not supported'); 82 process.exit(1); 83} 84if (hasRevision() && !fs.existsSync(path.join(utils.binariesDir), 'ejdb2_node.node')) { 85 install().catch((err) => { 86 console.error(err); 87 process.exit(1); 88 }); 89} 90