Skip to content

Commit

Permalink
fix: some tasks fail on Windows due to missing shell commands (#781)
Browse files Browse the repository at this point in the history
This PR tries to address #452 by wrapping the commands 'rm', 'mkdir' and 'mv' with `shx`.
A whitelist of commands is maintained in the task runtime.

@eladb Is this what you had in mind when commenting on #540?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
bracki committed May 16, 2021
1 parent a12d696 commit f187119
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .projen/deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
"name": "semver",
"type": "bundled"
},
{
"name": "shx",
"type": "bundled"
},
{
"name": "xmlbuilder2",
"type": "bundled"
Expand Down
1 change: 1 addition & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const project = new JsiiProject({
'@iarna/toml',
'xmlbuilder2',
'ini',
'shx',
],

devDeps: [
Expand Down
2 changes: 2 additions & 0 deletions package.json

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

4 changes: 2 additions & 2 deletions src/__tests__/__snapshots__/new.test.ts.snap

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

28 changes: 24 additions & 4 deletions src/tasks/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SpawnOptions, spawnSync } from 'child_process';
import { existsSync, readFileSync, statSync } from 'fs';
import { platform } from 'os';
import { join, resolve } from 'path';
import { format } from 'util';
import * as chalk from 'chalk';
Expand Down Expand Up @@ -87,10 +88,29 @@ class RunTask {
}

if (step.exec) {
const command = step.exec;
let command = '';
let hasError = false;
const cmd = step.exec.split(' ')[0];
if (platform() == 'win32' && ['mkdir', 'mv', 'rm'].includes(cmd)) {
command = `shx ${step.exec}`;
} else {
command = step.exec;
}
const cwd = step.cwd;
const result = this.shell({ command, cwd });
if (result.status !== 0) {
try {
const result = this.shell({
command,
cwd,
});
hasError = result.status !== 0;
} catch (e) {
// This is the error 'shx' will throw
if (e?.message?.startsWith('non-zero exit code:')) {
hasError = true;
}
throw e;
}
if (hasError) {
throw new Error(`Task "${this.fullname}" failed when executing "${command}" (cwd: ${resolve(cwd ?? this.workdir)})`);
}
}
Expand Down Expand Up @@ -229,4 +249,4 @@ interface ShellOptions {
readonly spawnOptions?: SpawnOptions;
/** @default false */
readonly quiet?: boolean;
}
}
12 changes: 10 additions & 2 deletions yarn.lock

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

0 comments on commit f187119

Please sign in to comment.