Skip to content

Commit

Permalink
improve task name inference
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Jul 7, 2024
1 parent 9441c84 commit 12faa47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-frogs-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ryanatkn/gro": patch
---

improve task name inference
22 changes: 7 additions & 15 deletions src/lib/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {resolve} from 'node:path';
import {is_task_path, to_task_name, validate_task_module, find_tasks, load_tasks} from './task.js';
import * as actual_test_task_module from './test.task.js';
import {create_empty_config} from './config.js';
import {GRO_DIST_DIR} from './paths.js';

test('is_task_path basic behavior', () => {
assert.ok(is_task_path('foo.task.ts'));
Expand All @@ -26,31 +27,22 @@ test('to_task_name basic behavior', () => {
assert.is(to_task_name('/a/b/d/foo.task.js', '/a/b/d', '/a/b/d/foo', '/a/c'), '../b/d/foo');
assert.is(
to_task_name(
'/a/node_modules/b/c/foo.task.js',
'/a/node_modules/b/c',
'/a/node_modules/b/c/foo',
GRO_DIST_DIR + 'foo.task.js',
GRO_DIST_DIR.slice(0, -1),
GRO_DIST_DIR + 'foo',
'/a',
),
'foo',
);
assert.is(
to_task_name(
'/a/node_modules/b/c/foo.task.js',
'/a/node_modules/b/c/', // compared to the above, adds a trailing slash here
'/a/node_modules/b/c/foo',
GRO_DIST_DIR + 'foo.task.js',
GRO_DIST_DIR, // same as above but adds a trailing slash here
GRO_DIST_DIR + 'foo',
'/a',
),
'foo',
);
assert.is(
to_task_name(
'/a/node_modules/b/c/foo.task.js',
'/a/node_modules/b/c',
'/a/node_modules/b/c/foo',
'/a/', // compared to the above, adds a trailing slash here
),
'foo',
);
assert.is(
to_task_name(resolve('a/b'), resolve('b'), '', ''),
resolve('a/b'),
Expand Down
17 changes: 9 additions & 8 deletions src/lib/task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Logger} from '@ryanatkn/belt/log.js';
import {strip_end, strip_start} from '@ryanatkn/belt/string.js';
import {ensure_end, strip_end, strip_start} from '@ryanatkn/belt/string.js';
import type {z} from 'zod';
import type {Timings} from '@ryanatkn/belt/timings.js';
import {red} from '@ryanatkn/belt/styletext.js';
Expand All @@ -17,7 +17,7 @@ import {
type Resolved_Input_File,
type Resolved_Input_Path,
} from './input_path.js';
import {print_path} from './paths.js';
import {GRO_DIST_DIR, print_path} from './paths.js';
import {search_fs} from './search_fs.js';
import {load_modules, type Load_Modules_Failure, type Module_Meta} from './modules.js';

Expand Down Expand Up @@ -61,13 +61,14 @@ export const to_task_name = (
for (const suffix of TASK_FILE_SUFFIXES) {
task_name = strip_end(task_name, suffix);
}
if (ensure_end(task_root_dir, '/') === GRO_DIST_DIR) {
// TODO ideally it would display this in some contexts like the task progress logs,
// but not all, like printing the task list, UNLESS there's a local override
// return 'gro/' + task_name;
return task_name;
}
if (isAbsolute(input_path)) {
// is a bit hacky, but does what we want
const relative_to_root = relative(root_path, join(task_root_dir, task_name));
if (relative_to_root.startsWith('node_modules/')) {
return task_name;
}
return relative_to_root;
return relative(root_path, join(task_root_dir, task_name));
}
return task_name;
};
Expand Down

0 comments on commit 12faa47

Please sign in to comment.