Skip to content

Commit

Permalink
Fix TypeScript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Aug 23, 2024
1 parent 160688d commit 407615c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/addon-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class AddonManager extends EventEmitter {

private updateTimeout: NodeJS.Timeout | null = null;

private updateInterval: number | null = null;
private updateInterval: NodeJS.Timeout | null = null;

private pairingTimeout: NodeJS.Timeout | null = null;

Expand Down
99 changes: 61 additions & 38 deletions src/platforms/linux-raspbian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class LinuxRaspbianPlatform extends BasePlatform {
mgmt = '';
const options: Record<string, unknown> = {};

let proc = child_process.spawnSync('systemctl', ['is-active', 'hostapd.service']);
const proc = child_process.spawnSync('systemctl', ['is-active', 'hostapd.service']);
if (proc.status === 0) {
mode = 'ap';
enabled = true;
Expand Down Expand Up @@ -212,13 +212,15 @@ class LinuxRaspbianPlatform extends BasePlatform {
}
} else {
mode = 'sta';
proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'status'], { encoding: 'utf8' });
const startProc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'status'], {
encoding: 'utf8',
});

if (proc.status !== 0) {
return { enabled, mode, options };
}

for (const line of proc.stdout.split('\n')) {
for (const line of startProc.stdout.split('\n')) {
const [key, value] = line.split('=', 2);
switch (key) {
case 'wpa_state':
Expand Down Expand Up @@ -248,15 +250,15 @@ class LinuxRaspbianPlatform extends BasePlatform {
}
}

proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'list_networks'], {
const listProc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'list_networks'], {
encoding: 'utf8',
});
if (proc.status !== 0) {
return { enabled, mode, options };
}

options.networks = [];
for (const line of proc.stdout.trim().split('\n')) {
for (const line of listProc.stdout.trim().split('\n')) {
if (line.startsWith('network')) {
continue;
}
Expand Down Expand Up @@ -299,74 +301,83 @@ class LinuxRaspbianPlatform extends BasePlatform {
}

// First, remove existing networks
let proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'list_networks'], {
const listProc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'list_networks'], {
encoding: 'utf8',
});
if (proc.status === 0) {
const networks = proc.stdout
if (listProc.status === 0) {
const networks = listProc.stdout
.split('\n')
.filter((l) => !l.startsWith('network'))
.map((l) => l.split(' ')[0])
.reverse();

for (const id of networks) {
proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'remove_network', id]);
if (proc.status !== 0) {
const removeNetworkProc = child_process.spawnSync('wpa_cli', [
'-i',
'wlan0',
'remove_network',
id,
]);
if (removeNetworkProc.status !== 0) {
console.log('Failed to remove network with id:', id);
}
}
}

// Then, stop hostapd. It will either need to be off or reconfigured, so
// this is valid in both modes.
proc = child_process.spawnSync('sudo', ['systemctl', 'stop', 'hostapd.service']);
if (proc.status !== 0) {
const stopProc = child_process.spawnSync('sudo', ['systemctl', 'stop', 'hostapd.service']);
if (stopProc.status !== 0) {
return false;
}

if (!enabled) {
proc = child_process.spawnSync('sudo', ['systemctl', 'disable', 'hostapd.service']);
return proc.status === 0;
const disableProc = child_process.spawnSync('sudo', [
'systemctl',
'disable',
'hostapd.service',
]);
return disableProc.status === 0;
}

// Make sure Wi-Fi isn't blocked by rfkill
child_process.spawnSync('sudo', ['rfkill', 'unblock', 'wifi']);

// Now, set the IP address back to a sane state
proc = child_process.spawnSync('sudo', ['ifconfig', 'wlan0', '0.0.0.0']);
if (proc.status !== 0) {
const configProc = child_process.spawnSync('sudo', ['ifconfig', 'wlan0', '0.0.0.0']);
if (configProc.status !== 0) {
return false;
}

if (mode === 'sta') {
proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'add_network'], {
const addProc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'add_network'], {
encoding: 'utf8',
});
if (proc.status !== 0) {
if (addProc.status !== 0) {
return false;
}

const id = proc.stdout.trim();
const id = addProc.stdout.trim();

options.ssid = (<string>options.ssid).replace('"', '\\"');
proc = child_process.spawnSync(
let setProc = child_process.spawnSync(
'wpa_cli',
// the ssid argument MUST be quoted
['-i', 'wlan0', 'set_network', id, 'ssid', `"${options.ssid}"`]
);
if (proc.status !== 0) {
if (setProc.status !== 0) {
return false;
}

if (options.key) {
options.key = (<string>options.key).replace('"', '\\"');
proc = child_process.spawnSync(
setProc = child_process.spawnSync(
'wpa_cli',
// the psk argument MUST be quoted
['-i', 'wlan0', 'set_network', id, 'psk', `"${options.key}"`]
);
} else {
proc = child_process.spawnSync('wpa_cli', [
setProc = child_process.spawnSync('wpa_cli', [
'-i',
'wlan0',
'set_network',
Expand All @@ -376,22 +387,26 @@ class LinuxRaspbianPlatform extends BasePlatform {
]);
}

if (proc.status !== 0) {
if (setProc.status !== 0) {
return false;
}

proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'enable_network', id]);
if (proc.status !== 0) {
const enableProc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'enable_network', id]);
if (enableProc.status !== 0) {
return false;
}

proc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'save_config']);
if (proc.status !== 0) {
const saveProc = child_process.spawnSync('wpa_cli', ['-i', 'wlan0', 'save_config']);
if (saveProc.status !== 0) {
return false;
}

proc = child_process.spawnSync('sudo', ['systemctl', 'disable', 'hostapd.service']);
if (proc.status !== 0) {
const disableProc = child_process.spawnSync('sudo', [
'systemctl',
'disable',
'hostapd.service',
]);
if (disableProc.status !== 0) {
return false;
}
} else {
Expand All @@ -409,30 +424,38 @@ class LinuxRaspbianPlatform extends BasePlatform {

fs.writeFileSync('/tmp/hostapd.conf', config);

proc = child_process.spawnSync('sudo', [
const mvProc = child_process.spawnSync('sudo', [
'mv',
'/tmp/hostapd.conf',
'/etc/hostapd/hostapd.conf',
]);

if (proc.status !== 0) {
if (mvProc.status !== 0) {
return false;
}

proc = child_process.spawnSync('sudo', ['systemctl', 'start', 'hostapd.service']);
if (proc.status !== 0) {
const startProc = child_process.spawnSync('sudo', ['systemctl', 'start', 'hostapd.service']);
if (startProc.status !== 0) {
return false;
}

proc = child_process.spawnSync('sudo', ['systemctl', 'enable', 'hostapd.service']);
if (proc.status !== 0) {
const enableProc = child_process.spawnSync('sudo', [
'systemctl',
'enable',
'hostapd.service',
]);
if (enableProc.status !== 0) {
return false;
}
}

if (options.ipaddr) {
proc = child_process.spawnSync('sudo', ['ifconfig', 'wlan0', <string>options.ipaddr]);
if (proc.status !== 0) {
const configProc = child_process.spawnSync('sudo', [
'ifconfig',
'wlan0',
<string>options.ipaddr,
]);
if (configProc.status !== 0) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default class Plugin {

asDict(): Record<string, unknown> {
let pid: string | number = 'not running';
if (this.process.p) {
if (this.process.p && typeof this.process.p.pid != 'undefined') {
pid = this.process.p.pid;
}
return {
Expand Down
6 changes: 3 additions & 3 deletions src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface TestGlobals {
}

export const chai = _chai;
(<NodeJS.Global & typeof globalThis & TestGlobals>global).chai = chai;
(<typeof global & typeof globalThis & TestGlobals>global).chai = chai;

expect.extend({
assert(value, message = 'expected condition to be truthy') {
Expand All @@ -41,7 +41,7 @@ expect.extend({
});

import { servers, serverStartup } from '../app';
(<NodeJS.Global & typeof globalThis & TestGlobals>global).server = servers.https!;
(<typeof global & typeof globalThis & TestGlobals>global).server = servers.https!;

import addonManager from '../addon-manager';

Expand All @@ -50,7 +50,7 @@ export function mockAdapter(): MockAdapter {
expect(adapter).not.toBeUndefined();
return <MockAdapter>(<unknown>adapter!);
}
(<NodeJS.Global & typeof globalThis & TestGlobals>global).mockAdapter = mockAdapter;
(<typeof global & typeof globalThis & TestGlobals>global).mockAdapter = mockAdapter;

function removeTestManifest(): void {
const testManifestJsonFilename = path.join(
Expand Down
4 changes: 2 additions & 2 deletions src/test/jsdom-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ interface TestGlobals {
}

beforeEach(() => {
(<NodeJS.Global & typeof globalThis & TestGlobals>global).sandbox = sinon.createSandbox();
(<typeof global & typeof globalThis & TestGlobals>global).sandbox = sinon.createSandbox();
});

afterEach(() => {
(<NodeJS.Global & typeof globalThis & TestGlobals>global).sandbox.restore();
(<typeof global & typeof globalThis & TestGlobals>global).sandbox.restore();
// Clean up any nodes added to the DOM
document.body.innerHTML = '';
});
2 changes: 1 addition & 1 deletion src/test/schema-form/form-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('Form', () => {
foo: { type: 'string' },
},
};
const onSubmit = (<NodeJS.Global & typeof globalThis & TestGlobals>global).sandbox.stub();
const onSubmit = (<typeof global & typeof globalThis & TestGlobals>global).sandbox.stub();

const { node } = createSchemaForm({
schema,
Expand Down

0 comments on commit 407615c

Please sign in to comment.