Skip to content

Commit

Permalink
[ci] merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanChanges committed Dec 3, 2024
2 parents 04c29de + d5b63bb commit 781ed66
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 39 deletions.
40 changes: 33 additions & 7 deletions cli/tools/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,15 @@ pub async fn execute_script(
description: None,
},
kill_signal,
cli_options.argv(),
)
.await;
}

for task_config in &packages_task_configs {
let exit_code = task_runner.run_tasks(task_config, &kill_signal).await?;
let exit_code = task_runner
.run_tasks(task_config, &kill_signal, cli_options.argv())
.await?;
if exit_code > 0 {
return Ok(exit_code);
}
Expand All @@ -263,6 +266,7 @@ struct RunSingleOptions<'a> {
cwd: &'a Path,
custom_commands: HashMap<String, Rc<dyn ShellCommand>>,
kill_signal: KillSignal,
argv: &'a [String],
}

struct TaskRunner<'a> {
Expand All @@ -279,9 +283,10 @@ impl<'a> TaskRunner<'a> {
&self,
pkg_tasks_config: &PackageTaskInfo,
kill_signal: &KillSignal,
argv: &[String],
) -> Result<i32, deno_core::anyhow::Error> {
match sort_tasks_topo(pkg_tasks_config) {
Ok(sorted) => self.run_tasks_in_parallel(sorted, kill_signal).await,
Ok(sorted) => self.run_tasks_in_parallel(sorted, kill_signal, argv).await,
Err(err) => match err {
TaskError::NotFound(name) => {
if self.task_flags.is_run {
Expand Down Expand Up @@ -317,6 +322,7 @@ impl<'a> TaskRunner<'a> {
&self,
tasks: Vec<ResolvedTask<'a>>,
kill_signal: &KillSignal,
args: &[String],
) -> Result<i32, deno_core::anyhow::Error> {
struct PendingTasksContext<'a> {
completed: HashSet<usize>,
Expand All @@ -338,13 +344,21 @@ impl<'a> TaskRunner<'a> {
&mut self,
runner: &'b TaskRunner<'b>,
kill_signal: &KillSignal,
argv: &'a [String],
) -> Option<
LocalBoxFuture<'b, Result<(i32, &'a ResolvedTask<'a>), AnyError>>,
>
where
'a: 'b,
{
for task in self.tasks.iter() {
let mut tasks_iter = self.tasks.iter().peekable();
while let Some(task) = tasks_iter.next() {
let args = if tasks_iter.peek().is_none() {
argv
} else {
&[]
};

if self.completed.contains(&task.id)
|| self.running.contains(&task.id)
{
Expand All @@ -366,7 +380,13 @@ impl<'a> TaskRunner<'a> {
match task.task_or_script {
TaskOrScript::Task(_, def) => {
runner
.run_deno_task(task.folder_url, task.name, def, kill_signal)
.run_deno_task(
task.folder_url,
task.name,
def,
kill_signal,
args,
)
.await
}
TaskOrScript::Script(scripts, _) => {
Expand All @@ -376,6 +396,7 @@ impl<'a> TaskRunner<'a> {
task.name,
scripts,
kill_signal,
args,
)
.await
}
Expand All @@ -399,7 +420,7 @@ impl<'a> TaskRunner<'a> {

while context.has_remaining_tasks() {
while queue.len() < self.concurrency {
if let Some(task) = context.get_next_task(self, kill_signal) {
if let Some(task) = context.get_next_task(self, kill_signal, args) {
queue.push(task);
} else {
break;
Expand Down Expand Up @@ -429,6 +450,7 @@ impl<'a> TaskRunner<'a> {
task_name: &str,
definition: &TaskDefinition,
kill_signal: KillSignal,
argv: &'a [String],
) -> Result<i32, deno_core::anyhow::Error> {
let cwd = match &self.task_flags.cwd {
Some(path) => canonicalize_path(&PathBuf::from(path))
Expand All @@ -447,6 +469,7 @@ impl<'a> TaskRunner<'a> {
cwd: &cwd,
custom_commands,
kill_signal,
argv,
})
.await
}
Expand All @@ -457,6 +480,7 @@ impl<'a> TaskRunner<'a> {
task_name: &str,
scripts: &IndexMap<String, String>,
kill_signal: KillSignal,
argv: &[String],
) -> Result<i32, deno_core::anyhow::Error> {
// ensure the npm packages are installed if using a managed resolver
if let Some(npm_resolver) = self.npm_resolver.as_managed() {
Expand Down Expand Up @@ -489,6 +513,7 @@ impl<'a> TaskRunner<'a> {
cwd: &cwd,
custom_commands: custom_commands.clone(),
kill_signal: kill_signal.clone(),
argv,
})
.await?;
if exit_code > 0 {
Expand All @@ -510,11 +535,12 @@ impl<'a> TaskRunner<'a> {
cwd,
custom_commands,
kill_signal,
argv,
} = opts;

output_task(
opts.task_name,
&task_runner::get_script_with_args(script, self.cli_options.argv()),
&task_runner::get_script_with_args(script, argv),
);

Ok(
Expand All @@ -525,7 +551,7 @@ impl<'a> TaskRunner<'a> {
env_vars: self.env_vars.clone(),
custom_commands,
init_cwd: self.cli_options.initial_cwd(),
argv: self.cli_options.argv(),
argv,
root_node_modules_dir: self.npm_resolver.root_node_modules_path(),
stdio: None,
kill_signal,
Expand Down
105 changes: 73 additions & 32 deletions ext/node/polyfills/_fs/_fs_readdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
// deno-lint-ignore-file prefer-primordials

import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js";
import { asyncIterableToCallback } from "ext:deno_node/_fs/_fs_watch.ts";
import Dirent from "ext:deno_node/_fs/_fs_dirent.ts";
import { denoErrorToNodeError } from "ext:deno_node/internal/errors.ts";
import { getValidatedPath } from "ext:deno_node/internal/fs/utils.mjs";
import { Buffer } from "node:buffer";
import { promisify } from "ext:deno_node/internal/util.mjs";
import { op_fs_read_dir_async, op_fs_read_dir_sync } from "ext:core/ops";
import { join, relative } from "node:path";

function toDirent(val: Deno.DirEntry & { parentPath: string }): Dirent {
return new Dirent(val);
Expand All @@ -18,6 +19,7 @@ function toDirent(val: Deno.DirEntry & { parentPath: string }): Dirent {
type readDirOptions = {
encoding?: string;
withFileTypes?: boolean;
recursive?: boolean;
};

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

export function readdir(
path: string | Buffer | URL,
options: { withFileTypes?: false; encoding?: string },
options: readDirOptions,
callback: readDirCallback,
): void;
export function readdir(
path: string | Buffer | URL,
options: { withFileTypes: true; encoding?: string },
options: readDirOptions,
callback: readDirCallbackDirent,
): void;
export function readdir(path: string | URL, callback: readDirCallback): void;
Expand All @@ -51,8 +53,7 @@ export function readdir(
const options = typeof optionsOrCallback === "object"
? optionsOrCallback
: null;
const result: Array<string | Dirent> = [];
path = getValidatedPath(path);
path = getValidatedPath(path).toString();

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

Expand All @@ -66,24 +67,44 @@ export function readdir(
}
}

try {
path = path.toString();
asyncIterableToCallback(Deno.readDir(path), (val, done) => {
if (typeof path !== "string") return;
if (done) {
callback(null, result);
const result: Array<string | Dirent> = [];
const dirs = [path];
let current: string | undefined;
(async () => {
while ((current = dirs.shift()) !== undefined) {
try {
const entries = await op_fs_read_dir_async(current);

for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (options?.recursive && entry.isDirectory) {
dirs.push(join(current, entry.name));
}

if (options?.withFileTypes) {
entry.parentPath = current;
result.push(toDirent(entry));
} else {
let name = decode(entry.name, options?.encoding);
if (options?.recursive) {
name = relative(path, join(current, name));
}
result.push(name);
}
}
} catch (err) {
callback(
denoErrorToNodeError(err as Error, {
syscall: "readdir",
path: current,
}),
);
return;
}
if (options?.withFileTypes) {
val.parentPath = path;
result.push(toDirent(val));
} else result.push(decode(val.name));
}, (e) => {
callback(denoErrorToNodeError(e as Error, { syscall: "readdir" }));
});
} catch (e) {
callback(denoErrorToNodeError(e as Error, { syscall: "readdir" }));
}
}

callback(null, result);
})();
}

function decode(str: string, encoding?: string): string {
Expand Down Expand Up @@ -118,8 +139,7 @@ export function readdirSync(
path: string | Buffer | URL,
options?: readDirOptions,
): Array<string | Dirent> {
const result = [];
path = getValidatedPath(path);
path = getValidatedPath(path).toString();

if (options?.encoding) {
try {
Expand All @@ -131,16 +151,37 @@ export function readdirSync(
}
}

try {
path = path.toString();
for (const file of Deno.readDirSync(path)) {
if (options?.withFileTypes) {
file.parentPath = path;
result.push(toDirent(file));
} else result.push(decode(file.name));
const result: Array<string | Dirent> = [];
const dirs = [path];
let current: string | undefined;
while ((current = dirs.shift()) !== undefined) {
try {
const entries = op_fs_read_dir_sync(current);

for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (options?.recursive && entry.isDirectory) {
dirs.push(join(current, entry.name));
}

if (options?.withFileTypes) {
entry.parentPath = current;
result.push(toDirent(entry));
} else {
let name = decode(entry.name, options?.encoding);
if (options?.recursive) {
name = relative(path, join(current, name));
}
result.push(name);
}
}
} catch (e) {
throw denoErrorToNodeError(e as Error, {
syscall: "readdir",
path: current,
});
}
} catch (e) {
throw denoErrorToNodeError(e as Error, { syscall: "readdir" });
}

return result;
}
5 changes: 5 additions & 0 deletions tests/specs/task/dependencies/__test__.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
"args": "task a",
"output": "./cycle_2.out",
"exitCode": 1
},
"arg_task_with_deps": {
"cwd": "arg_task_with_deps",
"args": "task a a",
"output": "./arg_task_with_deps.out"
}
}
}
4 changes: 4 additions & 0 deletions tests/specs/task/dependencies/arg_task_with_deps.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Task b echo 'b'
b
Task a echo "a"
a
9 changes: 9 additions & 0 deletions tests/specs/task/dependencies/arg_task_with_deps/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tasks": {
"a": {
"command": "echo",
"dependencies": ["b"]
},
"b": "echo 'b'"
}
}
Loading

0 comments on commit 781ed66

Please sign in to comment.