Skip to content

Commit

Permalink
fix(devtools): use random port on localhost in dev action (#2119)
Browse files Browse the repository at this point in the history
* fix(devtools): use random port on localhost in dev action

* Update dev.action.mjs

* Update client/src/cordova/dev.action.mjs

Co-authored-by: Vinicius Fortuna <[email protected]>

* Update dev.action.mjs

---------

Co-authored-by: Vinicius Fortuna <[email protected]>
  • Loading branch information
daniellacosse and fortuna authored Aug 15, 2024
1 parent ba59dd4 commit ae3e3e6
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions client/src/cordova/dev.action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {makeReplacements} from '../../build/make_replacements.mjs';
const OUTPUT_PATH = 'output/build/client/macos';
const OUTLINE_APP_PATH = 'Debug/Outline.app';
const OUTLINE_APP_WWW_PATH = 'Contents/Resources/www';
const RELOAD_SERVER_PORT = 35729;

const getUIHash = async () => {
const hashResult = await hashElement(
Expand Down Expand Up @@ -78,46 +77,8 @@ export async function main(...givenParameters) {
await runAction('client/go/build', ...parameters);
await runAction('client/src/cordova/setup', ...parameters);

await makeReplacements([
{
files: path.join(
getRootDir(),
'client/platforms/osx/**/index_cordova.html'
),
from: '<app-root></app-root>',
to: `<app-root></app-root>
<script>
try {
const reloadSocket = new WebSocket("ws://localhost:35729");
reloadSocket.onopen = () => console.log("LiveReload connected~");
reloadSocket.onmessage = ({ data }) => data === "reload" && location.reload();
} catch (e) {
// nevermind
}
</script>`,
},
]);

await spawnStream(
'xcodebuild',
'-scheme',
'Outline',
'-workspace',
path.join(getRootDir(), 'client/src/cordova/apple/macos.xcworkspace'),
`SYMROOT=${path.join(getRootDir(), OUTPUT_PATH)}`
);

await spawnStream(
'open',
path.join(getRootDir(), OUTPUT_PATH, OUTLINE_APP_PATH)
);

let previousUIHashResult = await getUIHash();

console.log(`Starting reload server @ port ${RELOAD_SERVER_PORT}...`);
createReloadServer(async () => {
const server = createReloadServer(async () => {
const currentUIHashResult = await getUIHash();

if (previousUIHashResult === currentUIHashResult) {
Expand All @@ -134,7 +95,48 @@ export async function main(...givenParameters) {
);

return true;
}).listen(RELOAD_SERVER_PORT);
});

server.listen(0, 'localhost', async () => {
const serverAddress = `ws://[${server.address().address}]:${server.address().port}`;
console.log(`LiveReload server running at ${serverAddress}`);

await makeReplacements([
{
files: path.join(
getRootDir(),
'client/platforms/osx/**/index_cordova.html'
),
from: '<app-root></app-root>',
to: `<app-root></app-root>
<script>
try {
const reloadSocket = new WebSocket("${serverAddress}");
reloadSocket.onopen = () => console.log("LiveReload connected~");
reloadSocket.onmessage = ({ data }) => data === "reload" && location.reload();
} catch (e) {
// nevermind
}
</script>`,
},
]);

await spawnStream(
'xcodebuild',
'-scheme',
'Outline',
'-workspace',
path.join(getRootDir(), 'client/src/cordova/apple/macos.xcworkspace'),
`SYMROOT=${path.join(getRootDir(), OUTPUT_PATH)}`
);

await spawnStream(
'open',
path.join(getRootDir(), OUTPUT_PATH, OUTLINE_APP_PATH)
);
});
}

if (import.meta.url === url.pathToFileURL(process.argv[1]).href) {
Expand Down

0 comments on commit ae3e3e6

Please sign in to comment.