Skip to content

Commit

Permalink
Converted to ts and unified hotreload scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed Sep 25, 2023
1 parent fcdb613 commit 9d0f0b0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-ants-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'capkit': patch
---

Converted hotreload to one script and uses now typescript
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"capkit": "dist/index.js"
},
"scripts": {
"dev": "tsup src/index.ts --format esm --dts --watch",
"build": "tsup src/index.ts --format esm --dts",
"dev": "tsup src/index.ts src/scripts/hotreload.ts --format esm --dts --watch",
"build": "tsup src/index.ts src/scripts/hotreload.ts --format esm --dts",
"lint": "prettier --check . && eslint .",
"lint:write": "prettier --write . && eslint . --fix",
"format": "prettier --write .",
Expand Down
8 changes: 0 additions & 8 deletions scripts/hotreload-cleanup.js

This file was deleted.

38 changes: 0 additions & 38 deletions scripts/hotreload.js

This file was deleted.

12 changes: 3 additions & 9 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ export async function initializeProject({
stop: `Successfully configured: "${kleur.cyan('package.json')}"`,
task: async () => {
const packageJson = JSON.parse(String(await fs.readFile('package.json')));
packageJson.scripts['dev:cap'] =
'node ./scripts/hotreload.js && npx cap sync && node ./scripts/hotreload-cleanup.js && npm run build';
packageJson.scripts['dev:cap'] = 'node scripts/hotreload.js';
packageJson.scripts['build:cap'] = 'vite build && npx cap sync';
return await fs.writeFile('package.json', JSON.stringify(packageJson, null, 2));
}
Expand Down Expand Up @@ -249,14 +248,9 @@ export async function initializeProject({
await fs.mkdir(`${consumerDir}/scripts`);
}

await fs.copyFile(
`${packageDir}/../scripts/hotreload.js`,
`${consumerDir}/scripts/hotreload.js`
);

return fs.copyFile(
`${packageDir}/../scripts/hotreload-cleanup.js`,
`${consumerDir}/scripts/hotreload-cleanup.js`
`${packageDir}/scripts/hotreload.js`,
`${consumerDir}/scripts/hotreload.js`
);
}
});
Expand Down
73 changes: 73 additions & 0 deletions src/scripts/hotreload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { exec } from 'child_process';
import { promises as fs } from 'fs';
import os from 'os';

async function hotreload() {
// Read and cache config
const capacitorConfigRaw = await fs.readFile('./capacitor.config.json');
await fs.copyFile('./capacitor.config.json', `./capacitor.config.json.timestamp-${Date.now()}`);

// Edit config
const config = JSON.parse(String(capacitorConfigRaw));
if (!config.server) config.server = {};
config.server.url = `http://${getIp()}:${await getPort()}/`;
config.server.cleartext = true;
await fs.writeFile('./capacitor.config.json', JSON.stringify(config));

// Sync config with Capacitor
await new Promise((resolve, reject) => {
const child = exec('npx cap sync');
child.addListener('error', reject);
child.addListener('exit', resolve);
});

// Restore config
cleanup();

// Run dev server
exec('vite dev');
}

try {
hotreload();
} catch(e) {
cleanup().then(() => {
if (e instanceof Error) console.error(e.message);
else console.error(e);
process.exit(-1);
});
}


function getIp() {
const ifaces = os.networkInterfaces();
let ip = 'localhost';
Object.keys(ifaces).forEach((ifname) => {
let alias = 0;
const iface = ifaces[ifname];
if (!iface) return;
iface.forEach((iface) => {
if ('IPv4' !== iface.family || iface.internal !== false) return;
if (alias >= 1) ip = iface.address;
else ip = iface.address;
++alias;
});
});
return ip;
}

async function getPort() {
const file = await fs.readFile('./vite.config.ts');
const match = String(file).match(/port:\s*(\d+)/);
return match && match[1] ? match[1] : 5173;
}

async function cleanup() {
const files = await fs.readdir('./');
for (const file of files) {
if (file.match(/capacitor\.config\.json\.timestamp-\d+/g)) {
await fs.copyFile(`./${file}`, './capacitor.config.json');
await fs.unlink(file);
}
}
}

0 comments on commit 9d0f0b0

Please sign in to comment.