Skip to content

Commit

Permalink
♻️ Use eval directory to allow require
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 24, 2022
1 parent f9599c8 commit 40d71b1
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,19 @@ const parseItemFile = async (
};

if (caption) {
const captionData = await callAsyncFunction(
{ ...result, require, __original_require__: require },
caption
);
// I know, I know...
const captionData = eval(`(function (data) {
const { slug, path, source, title, date, excerpt, attributes } = data;
${caption}
})(${JSON.stringify(result)})`);
if (captionData) result.caption = captionData;
}

return result;
};

const token = getInput("token") || process.env.GH_PAT || process.env.GITHUB_TOKEN;

const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
type AsyncFunctionArguments = Item & {
require: NodeRequire;
__original_require__: NodeRequire;
};

/**
* Call as async function with arguments
* @param args
* @param source
* @link https://github.com/actions/github-script/blob/main/src/async-function.ts
* @returns
*/
export function callAsyncFunction(args: AsyncFunctionArguments, source: string): Promise<string> {
const fn = new AsyncFunction(...Object.keys(args), source);
return fn(...Object.values(args));
}

export const run = async () => {
const token = getInput("token") || process.env.GH_PAT || process.env.GITHUB_TOKEN;
if (!token) throw new Error("GitHub token not found");
const directory = getInput("directory");
const caption = getInput("caption");
Expand Down

0 comments on commit 40d71b1

Please sign in to comment.