Skip to content

Commit 781ed66

Browse files
committed
[ci] merge branch 'main' into main
2 parents 04c29de + d5b63bb commit 781ed66

File tree

6 files changed

+167
-39
lines changed

6 files changed

+167
-39
lines changed

cli/tools/task.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,15 @@ pub async fn execute_script(
241241
description: None,
242242
},
243243
kill_signal,
244+
cli_options.argv(),
244245
)
245246
.await;
246247
}
247248

248249
for task_config in &packages_task_configs {
249-
let exit_code = task_runner.run_tasks(task_config, &kill_signal).await?;
250+
let exit_code = task_runner
251+
.run_tasks(task_config, &kill_signal, cli_options.argv())
252+
.await?;
250253
if exit_code > 0 {
251254
return Ok(exit_code);
252255
}
@@ -263,6 +266,7 @@ struct RunSingleOptions<'a> {
263266
cwd: &'a Path,
264267
custom_commands: HashMap<String, Rc<dyn ShellCommand>>,
265268
kill_signal: KillSignal,
269+
argv: &'a [String],
266270
}
267271

268272
struct TaskRunner<'a> {
@@ -279,9 +283,10 @@ impl<'a> TaskRunner<'a> {
279283
&self,
280284
pkg_tasks_config: &PackageTaskInfo,
281285
kill_signal: &KillSignal,
286+
argv: &[String],
282287
) -> Result<i32, deno_core::anyhow::Error> {
283288
match sort_tasks_topo(pkg_tasks_config) {
284-
Ok(sorted) => self.run_tasks_in_parallel(sorted, kill_signal).await,
289+
Ok(sorted) => self.run_tasks_in_parallel(sorted, kill_signal, argv).await,
285290
Err(err) => match err {
286291
TaskError::NotFound(name) => {
287292
if self.task_flags.is_run {
@@ -317,6 +322,7 @@ impl<'a> TaskRunner<'a> {
317322
&self,
318323
tasks: Vec<ResolvedTask<'a>>,
319324
kill_signal: &KillSignal,
325+
args: &[String],
320326
) -> Result<i32, deno_core::anyhow::Error> {
321327
struct PendingTasksContext<'a> {
322328
completed: HashSet<usize>,
@@ -338,13 +344,21 @@ impl<'a> TaskRunner<'a> {
338344
&mut self,
339345
runner: &'b TaskRunner<'b>,
340346
kill_signal: &KillSignal,
347+
argv: &'a [String],
341348
) -> Option<
342349
LocalBoxFuture<'b, Result<(i32, &'a ResolvedTask<'a>), AnyError>>,
343350
>
344351
where
345352
'a: 'b,
346353
{
347-
for task in self.tasks.iter() {
354+
let mut tasks_iter = self.tasks.iter().peekable();
355+
while let Some(task) = tasks_iter.next() {
356+
let args = if tasks_iter.peek().is_none() {
357+
argv
358+
} else {
359+
&[]
360+
};
361+
348362
if self.completed.contains(&task.id)
349363
|| self.running.contains(&task.id)
350364
{
@@ -366,7 +380,13 @@ impl<'a> TaskRunner<'a> {
366380
match task.task_or_script {
367381
TaskOrScript::Task(_, def) => {
368382
runner
369-
.run_deno_task(task.folder_url, task.name, def, kill_signal)
383+
.run_deno_task(
384+
task.folder_url,
385+
task.name,
386+
def,
387+
kill_signal,
388+
args,
389+
)
370390
.await
371391
}
372392
TaskOrScript::Script(scripts, _) => {
@@ -376,6 +396,7 @@ impl<'a> TaskRunner<'a> {
376396
task.name,
377397
scripts,
378398
kill_signal,
399+
args,
379400
)
380401
.await
381402
}
@@ -399,7 +420,7 @@ impl<'a> TaskRunner<'a> {
399420

400421
while context.has_remaining_tasks() {
401422
while queue.len() < self.concurrency {
402-
if let Some(task) = context.get_next_task(self, kill_signal) {
423+
if let Some(task) = context.get_next_task(self, kill_signal, args) {
403424
queue.push(task);
404425
} else {
405426
break;
@@ -429,6 +450,7 @@ impl<'a> TaskRunner<'a> {
429450
task_name: &str,
430451
definition: &TaskDefinition,
431452
kill_signal: KillSignal,
453+
argv: &'a [String],
432454
) -> Result<i32, deno_core::anyhow::Error> {
433455
let cwd = match &self.task_flags.cwd {
434456
Some(path) => canonicalize_path(&PathBuf::from(path))
@@ -447,6 +469,7 @@ impl<'a> TaskRunner<'a> {
447469
cwd: &cwd,
448470
custom_commands,
449471
kill_signal,
472+
argv,
450473
})
451474
.await
452475
}
@@ -457,6 +480,7 @@ impl<'a> TaskRunner<'a> {
457480
task_name: &str,
458481
scripts: &IndexMap<String, String>,
459482
kill_signal: KillSignal,
483+
argv: &[String],
460484
) -> Result<i32, deno_core::anyhow::Error> {
461485
// ensure the npm packages are installed if using a managed resolver
462486
if let Some(npm_resolver) = self.npm_resolver.as_managed() {
@@ -489,6 +513,7 @@ impl<'a> TaskRunner<'a> {
489513
cwd: &cwd,
490514
custom_commands: custom_commands.clone(),
491515
kill_signal: kill_signal.clone(),
516+
argv,
492517
})
493518
.await?;
494519
if exit_code > 0 {
@@ -510,11 +535,12 @@ impl<'a> TaskRunner<'a> {
510535
cwd,
511536
custom_commands,
512537
kill_signal,
538+
argv,
513539
} = opts;
514540

515541
output_task(
516542
opts.task_name,
517-
&task_runner::get_script_with_args(script, self.cli_options.argv()),
543+
&task_runner::get_script_with_args(script, argv),
518544
);
519545

520546
Ok(
@@ -525,7 +551,7 @@ impl<'a> TaskRunner<'a> {
525551
env_vars: self.env_vars.clone(),
526552
custom_commands,
527553
init_cwd: self.cli_options.initial_cwd(),
528-
argv: self.cli_options.argv(),
554+
argv,
529555
root_node_modules_dir: self.npm_resolver.root_node_modules_path(),
530556
stdio: None,
531557
kill_signal,

ext/node/polyfills/_fs/_fs_readdir.ts

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
// deno-lint-ignore-file prefer-primordials
55

66
import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js";
7-
import { asyncIterableToCallback } from "ext:deno_node/_fs/_fs_watch.ts";
87
import Dirent from "ext:deno_node/_fs/_fs_dirent.ts";
98
import { denoErrorToNodeError } from "ext:deno_node/internal/errors.ts";
109
import { getValidatedPath } from "ext:deno_node/internal/fs/utils.mjs";
1110
import { Buffer } from "node:buffer";
1211
import { promisify } from "ext:deno_node/internal/util.mjs";
12+
import { op_fs_read_dir_async, op_fs_read_dir_sync } from "ext:core/ops";
13+
import { join, relative } from "node:path";
1314

1415
function toDirent(val: Deno.DirEntry & { parentPath: string }): Dirent {
1516
return new Dirent(val);
@@ -18,6 +19,7 @@ function toDirent(val: Deno.DirEntry & { parentPath: string }): Dirent {
1819
type readDirOptions = {
1920
encoding?: string;
2021
withFileTypes?: boolean;
22+
recursive?: boolean;
2123
};
2224

2325
type readDirCallback = (err: Error | null, files: string[]) => void;
@@ -30,12 +32,12 @@ type readDirBoth = (
3032

3133
export function readdir(
3234
path: string | Buffer | URL,
33-
options: { withFileTypes?: false; encoding?: string },
35+
options: readDirOptions,
3436
callback: readDirCallback,
3537
): void;
3638
export function readdir(
3739
path: string | Buffer | URL,
38-
options: { withFileTypes: true; encoding?: string },
40+
options: readDirOptions,
3941
callback: readDirCallbackDirent,
4042
): void;
4143
export function readdir(path: string | URL, callback: readDirCallback): void;
@@ -51,8 +53,7 @@ export function readdir(
5153
const options = typeof optionsOrCallback === "object"
5254
? optionsOrCallback
5355
: null;
54-
const result: Array<string | Dirent> = [];
55-
path = getValidatedPath(path);
56+
path = getValidatedPath(path).toString();
5657

5758
if (!callback) throw new Error("No callback function supplied");
5859

@@ -66,24 +67,44 @@ export function readdir(
6667
}
6768
}
6869

69-
try {
70-
path = path.toString();
71-
asyncIterableToCallback(Deno.readDir(path), (val, done) => {
72-
if (typeof path !== "string") return;
73-
if (done) {
74-
callback(null, result);
70+
const result: Array<string | Dirent> = [];
71+
const dirs = [path];
72+
let current: string | undefined;
73+
(async () => {
74+
while ((current = dirs.shift()) !== undefined) {
75+
try {
76+
const entries = await op_fs_read_dir_async(current);
77+
78+
for (let i = 0; i < entries.length; i++) {
79+
const entry = entries[i];
80+
if (options?.recursive && entry.isDirectory) {
81+
dirs.push(join(current, entry.name));
82+
}
83+
84+
if (options?.withFileTypes) {
85+
entry.parentPath = current;
86+
result.push(toDirent(entry));
87+
} else {
88+
let name = decode(entry.name, options?.encoding);
89+
if (options?.recursive) {
90+
name = relative(path, join(current, name));
91+
}
92+
result.push(name);
93+
}
94+
}
95+
} catch (err) {
96+
callback(
97+
denoErrorToNodeError(err as Error, {
98+
syscall: "readdir",
99+
path: current,
100+
}),
101+
);
75102
return;
76103
}
77-
if (options?.withFileTypes) {
78-
val.parentPath = path;
79-
result.push(toDirent(val));
80-
} else result.push(decode(val.name));
81-
}, (e) => {
82-
callback(denoErrorToNodeError(e as Error, { syscall: "readdir" }));
83-
});
84-
} catch (e) {
85-
callback(denoErrorToNodeError(e as Error, { syscall: "readdir" }));
86-
}
104+
}
105+
106+
callback(null, result);
107+
})();
87108
}
88109

89110
function decode(str: string, encoding?: string): string {
@@ -118,8 +139,7 @@ export function readdirSync(
118139
path: string | Buffer | URL,
119140
options?: readDirOptions,
120141
): Array<string | Dirent> {
121-
const result = [];
122-
path = getValidatedPath(path);
142+
path = getValidatedPath(path).toString();
123143

124144
if (options?.encoding) {
125145
try {
@@ -131,16 +151,37 @@ export function readdirSync(
131151
}
132152
}
133153

134-
try {
135-
path = path.toString();
136-
for (const file of Deno.readDirSync(path)) {
137-
if (options?.withFileTypes) {
138-
file.parentPath = path;
139-
result.push(toDirent(file));
140-
} else result.push(decode(file.name));
154+
const result: Array<string | Dirent> = [];
155+
const dirs = [path];
156+
let current: string | undefined;
157+
while ((current = dirs.shift()) !== undefined) {
158+
try {
159+
const entries = op_fs_read_dir_sync(current);
160+
161+
for (let i = 0; i < entries.length; i++) {
162+
const entry = entries[i];
163+
if (options?.recursive && entry.isDirectory) {
164+
dirs.push(join(current, entry.name));
165+
}
166+
167+
if (options?.withFileTypes) {
168+
entry.parentPath = current;
169+
result.push(toDirent(entry));
170+
} else {
171+
let name = decode(entry.name, options?.encoding);
172+
if (options?.recursive) {
173+
name = relative(path, join(current, name));
174+
}
175+
result.push(name);
176+
}
177+
}
178+
} catch (e) {
179+
throw denoErrorToNodeError(e as Error, {
180+
syscall: "readdir",
181+
path: current,
182+
});
141183
}
142-
} catch (e) {
143-
throw denoErrorToNodeError(e as Error, { syscall: "readdir" });
144184
}
185+
145186
return result;
146187
}

tests/specs/task/dependencies/__test__.jsonc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
"args": "task a",
5757
"output": "./cycle_2.out",
5858
"exitCode": 1
59+
},
60+
"arg_task_with_deps": {
61+
"cwd": "arg_task_with_deps",
62+
"args": "task a a",
63+
"output": "./arg_task_with_deps.out"
5964
}
6065
}
6166
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Task b echo 'b'
2+
b
3+
Task a echo "a"
4+
a
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"tasks": {
3+
"a": {
4+
"command": "echo",
5+
"dependencies": ["b"]
6+
},
7+
"b": "echo 'b'"
8+
}
9+
}

0 commit comments

Comments
 (0)