chore: cleanup

This commit is contained in:
Igor Randjelovic
2021-03-26 22:58:49 +01:00
parent 75e6009b8a
commit b7da9d573f
3 changed files with 71 additions and 169 deletions

View File

@ -1,71 +1,56 @@
import { createServer } from 'http' // import { createServer } from 'http'
// import { spawn } from 'child_process' //
// yarn --cwd /Users/rigor789/Code/echo-server start // export interface IHMRStatusData {
// seq: number
// let statuses: { // uuid: string,
// [hash: string]: IHMRStatusData // hash: string
// } = {} // status: string
export interface IHMRStatusData {
seq: number
uuid: string,
hash: string
status: string
}
export function run() {
createServer((req, res) => {
if (req.url === '/ping') {
console.log('PING -> PONG!')
return res.end("Pong.");
}
if (req.method !== 'POST') {
res.statusCode = 400;
return res.end("Unsupported method.");
}
let data = "";
req.on("data", chunk => {
data += chunk;
});
req.on("end", () => {
try {
const signal = JSON.parse(data) as IHMRStatusData;
// if (!statuses[signal.hash] || statuses[signal.hash].seq < signal.seq) {
// statuses[signal.hash] = signal
// } // }
if (process.send) { //
process.send({ // export function run() {
type: 'hmr-status', // createServer((req, res) => {
version: 1, // if (req.url === '/ping') {
hash: signal.hash, // console.log('PING -> PONG!')
data: signal // return res.end("Pong.");
}, (error) => { // }
if (error) { //
console.error(`Process Send Error: `, error); // if (req.method !== 'POST') {
} // res.statusCode = 400;
// return res.end("Unsupported method.");
return null; // }
}); //
} // let data = "";
// req.on("data", chunk => {
res.end('ok.'); // data += chunk;
} catch (e) {
res.statusCode = 400;
res.end("Invalid JSON.");
}
});
}).listen(8238)
// spawn('/Users/rigor789/Code/echo-server/node_modules/.bin/ts-node', ['/Users/rigor789/Code/echo-server/index.ts'], {
// cwd: '/Users/rigor789/Code/echo-server/',
// stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
// }).on('message', function (data) {
// console.log({
// messageFromEchoServer: data
// }); // });
//
// req.on("end", () => {
// try {
// const signal = JSON.parse(data) as IHMRStatusData;
// // if (!statuses[signal.hash] || statuses[signal.hash].seq < signal.seq) {
// // statuses[signal.hash] = signal
// // }
// if (process.send) {
// process.send({
// type: 'hmr-status',
// version: 1,
// hash: signal.hash,
// data: signal
// }, (error) => {
// if (error) {
// console.error(`Process Send Error: `, error);
// }
//
// return null;
// }); // });
} // }
//
// res.end('ok.');
// } catch (e) {
// res.statusCode = 400;
// res.end("Invalid JSON.");
// }
// });
// }).listen(8238)
// }
//

View File

@ -8,7 +8,6 @@ import path from 'path';
import fs from 'fs'; import fs from 'fs';
import { parseEnvFlags } from '../cli/parseEnvFlags'; import { parseEnvFlags } from '../cli/parseEnvFlags';
import { run } from "./devServer";
const defaultConfig = path.resolve( const defaultConfig = path.resolve(
__dirname, __dirname,
@ -108,9 +107,6 @@ program
} }
if (options.watch) { if (options.watch) {
// run dev server
run();
console.log('webpack is watching the files...') console.log('webpack is watching the files...')
compiler.watch( compiler.watch(
configuration.watchOptions ?? {}, configuration.watchOptions ?? {},

View File

@ -1,125 +1,46 @@
// @ts-nocheck // @ts-nocheck
// This is a runtime module - included by nativescript-hot-loader // This is a runtime module - included by nativescript-hot-loader
// todo: log correct message format for CLI to pick up // this file should not include external dependencies
// todo: build CLI service to listen for state changes
// --- // ---
import type { IHMRStatusData } from "../../bin/devServer";
import { Http, Device } from '@nativescript/core'
const uuid = Device.uuid;
console.log(`[HMR] uuid = ${uuid}`)
let __NS_DEV_HOST_URL__;
Promise.race(__NS_DEV_HOST_IPS__
.map(ip => `http://${ip}:8238/`)
.map(async url => {
await Http.request({
method: 'get',
url: url + 'ping'
})
return url;
})).then(winner => {
__NS_DEV_HOST_URL__ = winner
})
let __SEQ = 0;
if (module.hot) { if (module.hot) {
module.hot.dispose(() => { const setStatus = (hash: string, status: 'success' | 'failure') => {
console.log('Disposing entry file?!') // format is important - CLI expects this exact format
// require('@nativescript/core').Application.resetRootView() console.log(`[HMR][${hash}] ${status}`)
})
const send = (content: object) => {
if (__NS_DEV_HOST_URL__) {
Http.request({
method: 'post',
url: __NS_DEV_HOST_URL__,
content: JSON.stringify(content)
}).catch(err => {
console.log(err)
})
} }
} const originalOnLiveSync = global.__onLiveSync
const sendStatus = (status, hash) => {
send({
seq: __SEQ++,
uuid,
hash,
status
} as IHMRStatusData)
}
const orig = global.__onLiveSync
const log = (type, info) => {
// console.log(`[nds] HMR ${type}:`, info)
}
log('init')
module.hot.addStatusHandler(status => {
log('status', status)
// sendStatus(status)
})
global.__onLiveSync = async function () { global.__onLiveSync = async function () {
// handle hot updates on LiveSync
console.log('~~~ livesync ~~~')
log('checking')
const hash = __webpack_require__.h(); const hash = __webpack_require__.h();
await module.hot.check().catch(err => { await module.hot.check().catch(err => {
log('checking-failed', err) setStatus(hash, 'failure')
sendStatus('failure', hash)
}); });
log('checked')
log('applying')
await module.hot.apply({ await module.hot.apply({
ignoreUnaccepted: false, ignoreUnaccepted: false,
ignoreDeclined: false, ignoreDeclined: false,
ignoreErrored: false, ignoreErrored: false,
onDeclined(info) { onDeclined(info) {
log('declined', info) setStatus(hash, 'failure')
sendStatus('failure', hash);
}, },
onUnaccepted(info) { onUnaccepted(info) {
log('unaccepted', info) setStatus(hash, 'failure')
sendStatus('failure', hash);
}, },
onAccepted(info) { onAccepted(info) {
log('accepted', info) // console.log('accepted', info)
}, },
onDisposed(info) { onDisposed(info) {
log('disposed', info) // console.log('disposed', info)
}, },
onErrored(info) { onErrored(info) {
log('errored', info) setStatus(hash, 'failure')
sendStatus('failure', hash);
} }
}).then(() => { }).then(() => {
sendStatus('success', hash) setStatus(hash, 'success')
}).catch((err) => { }).catch((err) => {
sendStatus('failure', hash) setStatus(hash, 'failure')
log('applying-failed', err)
}) })
// log('applying')
// await module.hot.apply()
log('applying-done')
// await module.hot.apply()
setTimeout(() => { setTimeout(() => {
orig(); originalOnLiveSync();
}); })
}; };
// global.__onLiveSync()
} }