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 // export function run() {
uuid: string, // createServer((req, res) => {
hash: string // if (req.url === '/ping') {
status: string // console.log('PING -> PONG!')
} // return res.end("Pong.");
// }
export function run() { //
createServer((req, res) => { // if (req.method !== 'POST') {
if (req.url === '/ping') { // res.statusCode = 400;
console.log('PING -> PONG!') // return res.end("Unsupported method.");
return res.end("Pong."); // }
} //
// let data = "";
if (req.method !== 'POST') { // req.on("data", chunk => {
res.statusCode = 400; // data += chunk;
return res.end("Unsupported method."); // });
} //
// req.on("end", () => {
let data = ""; // try {
req.on("data", chunk => { // const signal = JSON.parse(data) as IHMRStatusData;
data += chunk; // // if (!statuses[signal.hash] || statuses[signal.hash].seq < signal.seq) {
}); // // statuses[signal.hash] = signal
// // }
req.on("end", () => { // if (process.send) {
try { // process.send({
const signal = JSON.parse(data) as IHMRStatusData; // type: 'hmr-status',
// if (!statuses[signal.hash] || statuses[signal.hash].seq < signal.seq) { // version: 1,
// statuses[signal.hash] = signal // hash: signal.hash,
// } // data: signal
if (process.send) { // }, (error) => {
process.send({ // if (error) {
type: 'hmr-status', // console.error(`Process Send Error: `, error);
version: 1, // }
hash: signal.hash, //
data: signal // return null;
}, (error) => { // });
if (error) { // }
console.error(`Process Send Error: `, error); //
} // res.end('ok.');
// } catch (e) {
return null; // res.statusCode = 400;
}); // res.end("Invalid JSON.");
} // }
// });
res.end('ok.'); // }).listen(8238)
} 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
// });
// });
}

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()
} }