Skip to content

Commit 71b9f41

Browse files
committed
Add to API core.getPIOCommandOutput(args, options)
1 parent 4a38ed3 commit 71b9f41

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

src/core.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,30 @@ export async function runPIOCommand(args, callback, options = {}) {
119119
if (process.env.PLATFORMIO_CALLER) {
120120
baseArgs.push('-c', process.env.PLATFORMIO_CALLER);
121121
}
122+
if (options.projectDir) {
123+
options.spawnOptions = options.spawnOptions || {};
124+
options.spawnOptions.cwd = options.projectDir;
125+
delete options.projectDir;
126+
}
122127
proc.runCommand(await getCorePythonExe(), [...baseArgs, ...args], callback, options);
123128
}
129+
130+
export async function getPIOCommandOutput(args, options = {}) {
131+
return new Promise((resolve, reject) => {
132+
runPIOCommand(
133+
args,
134+
(code, stdout, stderr) => {
135+
if (code === 0) {
136+
return resolve(stdout);
137+
} else {
138+
const errMessage = stdout ? `${stderr} -> ${stdout}` : stderr;
139+
const err = new Error(errMessage);
140+
err.stderr = stderr;
141+
err.stdout = stdout;
142+
return reject(err);
143+
}
144+
},
145+
options
146+
);
147+
});
148+
}

src/installer/stages/platformio-core.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,11 @@ export default class PlatformIOCoreStage extends BaseStage {
202202
return true;
203203
}
204204

205-
installPIOHome() {
206-
return new Promise((resolve) => {
207-
core.runPIOCommand(
208-
['home', '--host', '__do_not_start__'],
209-
(code, stdout, stderr) => {
210-
if (code !== 0) {
211-
console.warn(stdout, stderr);
212-
}
213-
return resolve(true);
214-
}
215-
);
216-
});
205+
async installPIOHome() {
206+
try {
207+
await core.getPIOCommandOutput(['home', '--host', '__do_not_start__']);
208+
} catch (err) {
209+
console.warn(err);
210+
}
217211
}
218212
}

src/project/indexer.js

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

9+
import { getPIOCommandOutput } from '../core';
910
import path from 'path';
10-
import { runPIOCommand } from '../core';
1111
import { terminateCmdsInQueue } from '../proc';
1212

1313
export default class ProjectIndexer {
@@ -98,38 +98,24 @@ export default class ProjectIndexer {
9898
};
9999

100100
try {
101-
await new Promise((resolve, reject) => {
102-
const args = ['project', 'init', '--ide', this.options.ide];
103-
if (this.observer.getActiveEnvName()) {
104-
args.push('--environment', this.observer.getActiveEnvName());
105-
}
106-
runPIOCommand(
107-
args,
108-
(code, stdout, stderr) => {
109-
if (code === 0) {
110-
resolve();
111-
} else {
112-
reject(new Error(stderr));
113-
}
114-
},
115-
{
116-
spawnOptions: {
117-
cwd: this.projectDir,
118-
},
119-
runInQueue: true,
120-
onProcCreated: (subprocess) => {
121-
if (token) {
122-
token.onCancellationRequested(() => {
123-
logMessage('Configuration process has been terminated!', true);
124-
terminateCmdsInQueue();
125-
subprocess.kill();
126-
});
127-
}
128-
},
129-
onProcStdout: (data) => logMessage(data),
130-
onProcStderr: (data) => logMessage(data, true),
101+
const args = ['project', 'init', '--ide', this.options.ide];
102+
if (this.observer.getActiveEnvName()) {
103+
args.push('--environment', this.observer.getActiveEnvName());
104+
}
105+
await getPIOCommandOutput(args, {
106+
projectDir: this.projectDir,
107+
runInQueue: true,
108+
onProcCreated: (subprocess) => {
109+
if (token) {
110+
token.onCancellationRequested(() => {
111+
logMessage('Configuration process has been terminated!', true);
112+
terminateCmdsInQueue();
113+
subprocess.kill();
114+
});
131115
}
132-
);
116+
},
117+
onProcStdout: (data) => logMessage(data),
118+
onProcStderr: (data) => logMessage(data, true),
133119
});
134120
} catch (err) {
135121
console.warn(err);

0 commit comments

Comments
 (0)