Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed1f862

Browse files
committed
fix: provide alias for codeup for code portability
1 parent 3df165b commit ed1f862

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/action.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fsp from "node:fs/promises";
22
import consola from "consola";
33
import createJiti from "jiti";
4-
import { dirname, extname, resolve } from "pathe";
4+
import { dirname, extname, join, resolve } from "pathe";
55
import { filename } from "pathe/utils";
66
import { resolveSourceDir } from "./_giget";
77
import { createContext, runWithContext } from "./context";
88
import type { Action } from "./types";
9+
import { fileURLToPath } from "node:url";
910

1011
/**
1112
* Apply an action within context and working directory.
@@ -24,13 +25,13 @@ export async function applyAction(action: Action, cwd: string) {
2425
consola.success(
2526
`Action \`${getActionName(action)}\` applied in ${(
2627
performance.now() - start
27-
).toFixed(2)}ms`,
28+
).toFixed(2)}ms`
2829
);
2930
});
3031
} catch (error) {
3132
consola.error(
3233
`Failed to apply action \`${getActionName(action)}\`:\n`,
33-
error,
34+
error
3435
);
3536
}
3637
}
@@ -43,7 +44,7 @@ export async function applyAction(action: Action, cwd: string) {
4344
export async function applyActions(
4445
actions: Action[],
4546
cwd: string,
46-
opts?: { sort: boolean },
47+
opts?: { sort: boolean }
4748
) {
4849
const _actions = opts?.sort ? sortActions(actions) : actions;
4950
const _cwd = resolve(cwd || ".");
@@ -60,7 +61,7 @@ export async function applyActions(
6061
].filter(Boolean) as string[];
6162
return ` - ${parts.join(" ")}`;
6263
})
63-
.join("\n")}\n`,
64+
.join("\n")}\n`
6465
);
6566
for (const action of _actions) {
6667
await applyAction(action, _cwd);
@@ -102,11 +103,18 @@ export async function applyActionFromFile(path: string, workingDir: string) {
102103
export async function loadActionFromFile(path: string) {
103104
const _path = resolve(path);
104105
const actionDir = dirname(_path);
105-
const jiti = createJiti(actionDir, { interopDefault: true });
106+
const _pkgDir = fileURLToPath(new URL("../..", import.meta.url));
107+
const jiti = createJiti(actionDir, {
108+
interopDefault: true,
109+
alias: {
110+
codeup: join(_pkgDir, "dist/index.mjs"),
111+
"codeup/utils": join(_pkgDir, "dist/utils/index.mjs"),
112+
},
113+
});
106114
const action = jiti(_path) as Action;
107115
if (!action || typeof action.apply !== "function") {
108116
throw new Error(
109-
`File \`${_path}\` does not export a valid object with \`apply\` method!`,
117+
`File \`${_path}\` does not export a valid object with \`apply\` method!`
110118
);
111119
}
112120
action._path = _path;
@@ -120,12 +128,12 @@ const supportedExtensions = new Set([".js", ".ts", ".mjs", ".cjs"]);
120128
*/
121129
export async function loadActionsFromDir(actionsDir: string) {
122130
const actionFiles = (await fsp.readdir(actionsDir)).filter((path) =>
123-
supportedExtensions.has(extname(path)),
131+
supportedExtensions.has(extname(path))
124132
);
125133
const actions = await Promise.all(
126134
actionFiles.map(async (actionFile) =>
127-
loadActionFromFile(resolve(actionsDir, actionFile)),
128-
),
135+
loadActionFromFile(resolve(actionsDir, actionFile))
136+
)
129137
);
130138
return actions;
131139
}

0 commit comments

Comments
 (0)