@zuke-build/cli 2.13.1 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuke-build/cli",
3
- "version": "2.13.1",
3
+ "version": "2.14.0",
4
4
  "description": "The zuke command-line tool — scaffold a Zuke project with `zuke setup`, including a starter build, launchers, and tasks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/import.js CHANGED
@@ -138,7 +138,7 @@ function stripRecipePrefix(recipe) {
138
138
  * appears only where it changes something: the wrapper's own resolution is not
139
139
  * repeated, and a command with no arguments takes no `.args()`.
140
140
  */
141
- function runCall(argv, resolution) {
141
+ function runCall(argv, resolution, stdout) {
142
142
  const match = wrapperFor(argv);
143
143
  const [bin, ...args] = argv;
144
144
  const [callee, own, rest, head] = match === undefined
@@ -155,6 +155,10 @@ function runCall(argv, resolution) {
155
155
  }
156
156
  if (rest.length > 0)
157
157
  settings.push(`args(${rest.map(literal).join(", ")})`);
158
+ if (stdout !== undefined) {
159
+ const append = stdout.append ? ", (w) => w.append()" : "";
160
+ settings.push(`stdoutToFile(${literal(stdout.path)}${append})`);
161
+ }
158
162
  const params = settings.length === 0
159
163
  ? head
160
164
  : [...head, `(s) => s.${settings.join(".")}`];
@@ -175,13 +179,19 @@ function runCall(argv, resolution) {
175
179
  * comment preserving the raw text when it is too shell-specific to translate.
176
180
  */
177
181
  export function translateCommand(command, resolution) {
178
- return segments(command).map((segment) => "raw" in segment
179
- ? {
182
+ return segments(command).map((segment) => {
183
+ if ("argv" in segment)
184
+ return runCall(segment.argv, resolution);
185
+ if ("redirect" in segment) {
186
+ const { argv, path, append } = segment.redirect;
187
+ return runCall(argv, resolution, { path, append });
188
+ }
189
+ return {
180
190
  code: `// TODO: translate this shell command: ${oneLine(segment.raw)}`,
181
191
  runnable: false,
182
192
  uses: [],
183
- }
184
- : runCall(segment.argv, resolution));
193
+ };
194
+ });
185
195
  }
186
196
  // --- identifiers ----------------------------------------------------------
187
197
  /**
@@ -145,11 +145,12 @@ function bodyLines(steps) {
145
145
  run(step.argv);
146
146
  else {
147
147
  for (const segment of segments(step.command)) {
148
- if ("raw" in segment) {
149
- todo(`translate this shell command: ${segment.raw}`);
150
- }
151
- else
148
+ if ("argv" in segment)
152
149
  run(segment.argv);
150
+ else {
151
+ const raw = "raw" in segment ? segment.raw : segment.redirect.raw;
152
+ todo(`translate this shell command: ${raw}`);
153
+ }
153
154
  }
154
155
  }
155
156
  }
@@ -1,9 +1,25 @@
1
- /** One `&&`-separated segment of a command: argv to run, or text to keep verbatim. */
1
+ /**
2
+ * One `&&`-separated segment of a command: argv to run, argv whose stdout the
3
+ * shell sends to a file, or text to keep verbatim.
4
+ */
2
5
  export type Segment = {
3
6
  readonly argv: readonly string[];
7
+ } | {
8
+ readonly redirect: Redirect;
4
9
  } | {
5
10
  readonly raw: string;
6
11
  };
12
+ /** A segment of the form `cmd args > file`, or `>> file` to append. */
13
+ export interface Redirect {
14
+ /** The command, as argv. */
15
+ readonly argv: readonly string[];
16
+ /** The file its stdout goes to, as written. */
17
+ readonly path: string;
18
+ /** Whether stdout is added to the file (`>>`) rather than replacing it. */
19
+ readonly append: boolean;
20
+ /** The segment as written, for a generator that cannot express it. */
21
+ readonly raw: string;
22
+ }
7
23
  /** Whether `segment` uses a shell feature a single argv cannot faithfully express. */
8
24
  export declare function needsShell(segment: string): boolean;
9
25
  /**
package/src/translate.js CHANGED
@@ -74,11 +74,50 @@ export function segments(command) {
74
74
  const segment = raw.trim();
75
75
  if (segment === "")
76
76
  continue;
77
- const argv = needsShell(segment) ? undefined : tokenize(segment);
78
- out.push(argv === undefined ? { raw: segment } : { argv });
77
+ out.push(parseSegment(segment));
79
78
  }
80
79
  return out;
81
80
  }
81
+ /** One non-blank segment, as {@link segments} classifies it. */
82
+ function parseSegment(segment) {
83
+ if (!needsShell(segment)) {
84
+ const argv = tokenize(segment);
85
+ return argv === undefined ? { raw: segment } : { argv };
86
+ }
87
+ const redirect = redirected(segment);
88
+ return redirect === undefined ? { raw: segment } : { redirect };
89
+ }
90
+ /**
91
+ * `segment` as a command whose stdout goes to a file, or `undefined` unless
92
+ * that is all the shell does with it: exactly one unquoted `>` or `>>`, after
93
+ * whitespace (so neither `2>` nor `&>`), followed by a single plain word that
94
+ * is the last thing on the line, with a command before it that needs no shell
95
+ * itself. A target the shell would expand (`$`, `~`, a glob), an fd target
96
+ * (`>&2`), a clobber (`>|`) or a parenthesis anywhere (a subshell to `sh`)
97
+ * leaves the segment verbatim.
98
+ */
99
+ function redirected(segment) {
100
+ const at = unquotedIndexes(segment, ">");
101
+ const append = at.length === 2 && at[1] === at[0] + 1;
102
+ if (at.length !== 1 && !append)
103
+ return undefined;
104
+ const start = at[0];
105
+ if (start === 0 || !/\s/.test(segment[start - 1]))
106
+ return undefined;
107
+ const command = segment.slice(0, start);
108
+ const target = segment.slice(at[at.length - 1] + 1);
109
+ if (needsShell(command) || needsShell(target))
110
+ return undefined;
111
+ if (/[~*?[{&]/.test(target) || /[()]/.test(segment))
112
+ return undefined;
113
+ const argv = tokenize(command);
114
+ const path = tokenize(target);
115
+ if (argv === undefined)
116
+ return undefined;
117
+ if (path === undefined || path.length !== 1)
118
+ return undefined;
119
+ return { argv, path: path[0], append, raw: segment };
120
+ }
82
121
  /** Split a command on top-level `&&`, ignoring `&&` inside quotes. */
83
122
  export function splitChain(command) {
84
123
  const parts = [];
package/src/version.js CHANGED
@@ -10,4 +10,4 @@
10
10
  * reads it — the annotation is what keeps that from mattering if it is ever
11
11
  * exported.
12
12
  */
13
- export const VERSION = "2.13.1"; // x-release-please-version
13
+ export const VERSION = "2.14.0"; // x-release-please-version