--- a/index.js +++ b/index.js @@ -1,17 +1,11 @@ const semver = require('semver'); const engines = require('./package.json').engines; -const fs = require('fs'); -const os = require('os'); const path = require('path'); -const {exec} = require('child_process'); -const {rimrafSync} = require('rimraf'); require('source-map-support').install(); let controller; let stopping = false; -const hashFile = path.join(__dirname, 'dist', '.hash'); - async function restart() { await stop(true); await start(); @@ -23,64 +17,7 @@ } } -async function currentHash() { - const git = require('git-last-commit'); - return new Promise((resolve) => { - git.getLastCommit((err, commit) => { - if (err) resolve('unknown'); - else resolve(commit.shortHash); - }); - }); -} - -async function writeHash() { - const hash = await currentHash(); - fs.writeFileSync(hashFile, hash); -} - -async function build(reason) { - return new Promise((resolve, reject) => { - process.stdout.write(`Building Zigbee2MQTT... (${reason})`); - rimrafSync('dist'); - const env = {...process.env}; - const _600mb = 629145600; - if (_600mb > os.totalmem() && !env.NODE_OPTIONS) { - // Prevent OOM on tsc compile for system with low memory - // https://github.com/Koenkk/zigbee2mqtt/issues/12034 - env.NODE_OPTIONS = '--max_old_space_size=256'; - } - - exec('npm run build', {env, cwd: __dirname}, async (err, stdout, stderr) => { - if (err) { - process.stdout.write(', failed\n'); - if (err.code === 134) { - process.stderr.write( - '\n\nBuild failed; ran out-of-memory, free some memory (RAM) and start again\n\n'); - } - reject(err); - } else { - process.stdout.write(', finished\n'); - resolve(); - } - }); - }); -} - -async function checkDist() { - if (!fs.existsSync(hashFile)) { - await build('initial build'); - } - - const distHash = fs.readFileSync(hashFile, 'utf-8'); - const hash = await currentHash(); - if (hash !== 'unknown' && distHash !== hash) { - await build('hash changed'); - } -} - async function start() { - await checkDist(); - const version = engines.node; if (!semver.satisfies(process.version, version)) { console.log(`\t\tZigbee2MQTT requires node version ${version}, you are running ${process.version}!\n`); // eslint-disable-line @@ -119,13 +56,9 @@ } if (require.main === module || require.main.filename.endsWith(path.sep + 'cli.js')) { - if (process.argv.length === 3 && process.argv[2] === 'writehash') { - writeHash(); - } else { - process.on('SIGINT', handleQuit); - process.on('SIGTERM', handleQuit); - start(); - } + process.on('SIGINT', handleQuit); + process.on('SIGTERM', handleQuit); + start(); } else { process.on('SIGINT', handleQuit); process.on('SIGTERM', handleQuit); --- a/package.json +++ b/package.json @@ -22,3 +22,3 @@ "scripts": { - "build": "tsc && node index.js writehash", + "build": "tsc", "build-watch": "tsc --watch",