Skip to content

Commit 4617075

Browse files
committed
Extend API with "home.showAtStartup"
1 parent ebe911e commit 4617075

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/core.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ export function getVersion() {
5757
}
5858

5959
export function runPIOCommand(args, callback, options = {}) {
60+
const baseArgs = ['-f'];
61+
if (process.env.PLATFORMIO_CALLER) {
62+
baseArgs.push('-c');
63+
baseArgs.push(process.env.PLATFORMIO_CALLER);
64+
}
6065
runCommand(
6166
'platformio',
62-
['-f', ...args],
67+
[...baseArgs, ...args],
6368
callback,
6469
options
6570
);

src/home.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
* the root directory of this source tree.
77
*/
88

9+
import { getHomeDir, runPIOCommand } from './core';
10+
911
import SockJS from 'sockjs-client';
12+
import fs from 'fs-plus';
1013
import jsonrpc from 'jsonrpc-lite';
14+
import path from 'path';
1115
import request from 'request';
12-
import spawn from 'cross-spawn';
1316
import tcpPortUsed from 'tcp-port-used';
1417

1518

@@ -108,9 +111,9 @@ export async function ensureServerStarted(options) {
108111
if (await isServerStarted()) {
109112
return params;
110113
}
111-
spawn('platformio', ['home', '--port', HTTP_PORT, '--no-open']);
114+
runPIOCommand(['home', '--port', HTTP_PORT, '--no-open']);
112115
await new Promise((resolve, reject) => {
113-
tcpPortUsed.waitUntilUsed(HTTP_PORT, 500, 10000)
116+
tcpPortUsed.waitUntilUsed(HTTP_PORT, 500, 15000)
114117
.then(() => {
115118
resolve(true);
116119
}, (err) => {
@@ -126,3 +129,17 @@ export async function ensureServerStarted(options) {
126129
export function shutdownServer() {
127130
request.get(`http://${HTTP_HOST}:${HTTP_PORT}?__shutdown__=1`);
128131
}
132+
133+
export function showAtStartup(caller) {
134+
const statePath = path.join(getHomeDir(), 'homestate.json');
135+
if (!fs.isFileSync(statePath)) {
136+
return true;
137+
}
138+
try {
139+
const state = JSON.parse(fs.readFileSync(statePath, 'utf8'));
140+
return !state || !state.storage || !state.storage.showOnStartup || !state.storage.showOnStartup.hasOwnProperty(caller) || state.storage.showOnStartup[caller];
141+
} catch (err) {
142+
console.error(err);
143+
return true;
144+
}
145+
}

src/misc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function patchOSEnviron({ caller, useBuiltinPIOCore=true, extraPath, extr
5353
console.warn(process.env);
5454
}
5555

56-
export function runCommand(cmd, args, callback, options = {}) {
56+
export function runCommand(cmd, args, callback=undefined, options = {}) {
5757
console.info('runCommand', cmd, args, options);
5858
let completed = false;
5959
const outputLines = [];
@@ -76,7 +76,7 @@ export function runCommand(cmd, args, callback, options = {}) {
7676
}
7777

7878
function onExit(code) {
79-
if (completed) {
79+
if (completed || !callback) {
8080
return;
8181
}
8282
completed = true;

0 commit comments

Comments
 (0)