Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: restructure monorepo #231

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,5 @@
"node": "22.11.0",
"pnpm": "9.14.2"
},
"pnpm": {
"patchedDependencies": {
"fixturify-project": "patches/fixturify-project.patch"
},
"overrides": {
"@sveltejs/vite-plugin-svelte": "^4.0.0"
}
},
"packageManager": "[email protected]+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
}
1 change: 1 addition & 0 deletions packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const handler = (({ max = 1 }: { max?: number } = { max: 1 }) => {
if (running_controllers.size >= max) {
const first = running_controllers.values().next();
first.value?.abort();
running_controllers.delete(first.value);
running_controllers.delete(first.value!);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I don't understand why you needed to add this 🤔 the line above you're dealing with value possibly being null/undefined but here you're asserting that it's not? it seems like a real type change in a PR that is otherwise just moving things around

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the switcharoo that we did with the tsconfig caused the surface of this problem...but it's really a non problem because you can try to delete null or undefined and it will just not delete anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but that should probably be reflected in the types no? what's the signature for delete?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but that should probably be reflected in the types no? what's the signature for delete?

Nope, i think this is one of the rare instances when TS is more strict than it needs to be

interface Set<T> {
    /**
     * Appends a new element with a specified value to the end of the Set.
     */
    add(value: T): this;

    clear(): void;
    /**
     * Removes a specified value from the Set.
     * @returns Returns true if an element in the Set existed and has been removed, or false if the element does not exist.
     */
    delete(value: T): boolean;
    /**
     * Executes a provided function once per each value in the Set object, in insertion order.
     */
    forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void;
    /**
     * @returns a boolean indicating whether an element with the specified value exists in the Set or not.
     */
    has(value: T): boolean;
    /**
     * @returns the number of (unique) elements in Set.
     */
    readonly size: number;
}

we could make the Set be Set<AbortController | undefined | null> but tbf i'm perfectly fine with just asserting the existance.

}
running_controllers.add(utils.abort_controller);
try {
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@sheepdog/core",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "git+https://github.com/mainmatter/sheepdog.git"
},
"main": "./dist/index.js",
"type": "module",
"scripts": {
"build": "tsc --outDir dist --declaration --sourceMap false && publint",
"prepack": "pnpm build",
"prepare": "pnpm build"
},
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js",
"import": "./dist/index.js"
},
"./utils": {
"types": "./dist/utils.d.ts",
"svelte": "./dist/utils.js",
"import": "./dist/utils.js"
},
"./vite": {
"types": "./dist/vite.d.ts",
"svelte": "./dist/vite.js",
"import": "./dist/vite.js"
}
},
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js",
"dependencies": {
"acorn": "^8.11.3",
"esm-env": "^1.1.4",
"esrap": "^1.2.2",
"zimmerframe": "^1.1.2"
},
"volta": {
"extends": "../../package.json"
},
"devDependencies": {
"publint": "^0.2.7",
"vite": "^5.2.11"
}
}
6 changes: 6 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true
}
}
33 changes: 33 additions & 0 deletions packages/core/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DEV } from 'esm-env';
import { CancelationError } from './index';
import type { TaskFunction } from './index';

/**
* Utility method to know if a `perform` thrown because it was canceled
*/
export const didCancel = (e: Error | CancelationError) => {
return e instanceof CancelationError;
};

/**
* A Promise that will resolve after {ms} milliseconds
*/
export async function timeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* A function to mark your function to be transformable by the
* async transform vite plugin. It will error out in dev mode
* if you didn't add the plugin.
*/
export function transform<TArgs, TReturn>(
fn: TaskFunction<TArgs, TReturn>,
): TaskFunction<TArgs, TReturn> {
if (DEV) {
throw new Error(
'You are using the transform function without the vite plugin. Please add the `asyncTransform` plugin to your `vite.config.ts`',
);
}
return fn;
}
144 changes: 144 additions & 0 deletions packages/core/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import type {
ArrowFunctionExpression,
AwaitExpression,
BlockStatement,
CallExpression,
FunctionExpression,
ImportDeclaration,
} from 'acorn';
import type { Plugin } from 'vite';

type Nodes =
| ImportDeclaration
| FunctionExpression
| ArrowFunctionExpression
| CallExpression
| BlockStatement
| AwaitExpression;

export async function asyncTransform() {
const { parse } = await import('acorn');
const { print } = await import('esrap');
const { walk } = await import('zimmerframe');

return {
name: 'sheepdog-async-transform',
async transform(code, id) {
try {
const ast = parse(code, {
ecmaVersion: 'latest',
locations: true,
sourceType: 'module',
});
let task_fn_name: string;
const transform_fn_names = new Set<string>();
// let's walk once to find the name (we were using a promise before but that's just messy)
walk(
ast as unknown as ImportDeclaration,
{},
{
ImportDeclaration(node) {
if (
node.source.value === '@sheepdog/svelte' ||
node.source.value === '@sheepdog/svelte/task' ||
node.source.value === '@sheepdog/svelte/utils'
) {
const task_fn = node.specifiers.find((specifier) => {
return (
specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name === 'task'
);
});
const transform_fn = node.specifiers.find((specifier) => {
return (
specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name === 'transform'
);
});
if (transform_fn && transform_fn.type === 'ImportSpecifier') {
transform_fn_names.add(transform_fn.local.name);
}
if (task_fn && task_fn.type === 'ImportSpecifier') {
task_fn_name = task_fn.local.name;
}
}
},
},
);
let changed = false;
const returned = walk(
ast as unknown as Nodes,
{
transform: false,
},
{
AwaitExpression(node, { next, state }) {
if (state.transform) {
next();
node.type = 'YieldExpression' as never;
}
},
CallExpression(node, { state, next }) {
let local_changed = false;
let task_arg: (typeof node)['arguments'][number] | undefined;
if (
(node.callee.type === 'Identifier' &&
(node.callee.name === task_fn_name ||
transform_fn_names.has(node.callee.name))) ||
(node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === task_fn_name)
) {
task_arg = node.arguments[0];
if (task_arg && task_arg.type === 'ArrowFunctionExpression' && task_arg.async) {
const to_change = task_arg as unknown as FunctionExpression;
to_change.type = 'FunctionExpression';
to_change.generator = true;
next({ ...state, transform: true });
changed = true;
local_changed = true;
} else if (
task_arg &&
task_arg.type === 'FunctionExpression' &&
!task_arg.generator &&
task_arg.async
) {
const to_change = task_arg as unknown as FunctionExpression;
to_change.generator = true;
next({ ...state, transform: true });
changed = true;
local_changed = true;
}
}
if (!local_changed) {
next();
}
if (
task_arg &&
node.callee.type === 'Identifier' &&
transform_fn_names.has(node.callee.name)
) {
return task_arg as never;
}
},
},
) as unknown as typeof ast;

if (changed) {
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...print(returned as any, {
sourceMapContent: code,
sourceMapSource: id,
}),
};
}
} catch (e) {
console.error(e);
/** in case parsing fails */
}
},
} satisfies Plugin;
}
2 changes: 1 addition & 1 deletion packages/svelte/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
engine-strict=true
engine-strict=true
7 changes: 2 additions & 5 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@
"test:unit:ui": "vitest --ui"
},
"dependencies": {
"acorn": "^8.11.3",
"esm-env": "^1.1.4",
"esrap": "^1.2.2",
"zimmerframe": "^1.1.2"
"@sheepdog/core": "workspace:*"
},
"devDependencies": {
"@playwright/test": "^1.44.0",
Expand All @@ -78,7 +75,7 @@
"eslint-plugin-playwright": "^2.0.0",
"eslint-plugin-svelte": "2.46.0",
"execa": "^9.4.0",
"fixturify-project": "^7.1.2",
"fixturify-project": "^7.1.3",
"globals": "^15.2.0",
"happy-dom": "^15.0.0",
"prettier": "^3.2.5",
Expand Down
10 changes: 8 additions & 2 deletions packages/svelte/src/lib/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import type { HandlerType, HandlersMap, SheepdogUtils, TaskOptions, TaskFunction } from './core';
import { CancelationError, createTask, handlers } from './core';
import type {
HandlerType,
HandlersMap,
SheepdogUtils,
TaskOptions,
TaskFunction,
} from '@sheepdog/core';
import { CancelationError, createTask, handlers } from '@sheepdog/core';
import type { ReadableWithGet, WritableWithGet } from './internal/helpers';
import { writable_with_get } from './internal/helpers';
export type { SheepdogUtils, TaskOptions };
Expand Down
34 changes: 2 additions & 32 deletions packages/svelte/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
import { DEV } from 'esm-env';
import { CancelationError } from './core';
import type { TaskFunction } from './core';
import { didCancel, timeout, transform } from '@sheepdog/core/utils';

/**
* Utility method to know if a `perform` thrown because it was canceled
*/
export const didCancel = (e: Error | CancelationError) => {
return e instanceof CancelationError;
};

/**
* A Promise that will resolve after {ms} milliseconds
*/
export async function timeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* A function to mark your function to be transformable by the
* async transform vite plugin. It will error out in dev mode
* if you didn't add the plugin.
*/
export function transform<TArgs, TReturn>(
fn: TaskFunction<TArgs, TReturn>,
): TaskFunction<TArgs, TReturn> {
if (DEV) {
throw new Error(
'You are using the transform function without the vite plugin. Please add the `asyncTransform` plugin to your `vite.config.ts`',
);
}
return fn;
}
export { didCancel, timeout, transform };
Loading