Skip to content

Commit

Permalink
further replace require with import, remove index.js hardcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kreddlear committed Feb 3, 2025
1 parent 1d58d09 commit 55ecb90
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
6 changes: 1 addition & 5 deletions packages/cli/src/oclif/commands/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { DateTime, IANAZone } = require('luxon');

const BaseCommand = require('../ZapierBaseCommand');
const { buildFlags } = require('../buildFlags');
const { localAppCommand, getLocalAppHandler } = require('../../utils/local');
const { localAppCommand } = require('../../utils/local');
const { startSpinner, endSpinner } = require('../../utils/display');

const ACTION_TYPE_PLURALS = {
Expand Down Expand Up @@ -407,10 +407,6 @@ class InvokeCommand extends BaseCommand {
}

if (!_.isEmpty(env)) {
// process.env changed, so we need to reload the modules that have loaded
// the old values of process.env
getLocalAppHandler({ reload: true });

// Save envs so the user won't have to re-enter them if the command fails
await appendEnv(env);
console.warn('CLIENT_ID and CLIENT_SECRET saved to .env file.');
Expand Down
18 changes: 3 additions & 15 deletions packages/cli/src/utils/local.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
const path = require('path');

const { findCorePackageDir } = require('./misc');

const getLocalAppHandler = ({ reload = false, baseEvent = {} } = {}) => {
const entryPath = `${process.cwd()}/index`;
const rootPath = path.dirname(require.resolve(entryPath));
const getLocalAppHandler = async () => {
const corePackageDir = findCorePackageDir();

if (reload) {
Object.keys(require.cache).forEach((cachePath) => {
if (cachePath.startsWith(rootPath)) {
delete require.cache[cachePath];
}
});
}
let appRaw, zapier;

try {
appRaw = require(entryPath);
appRaw = await import(process.cwd());
zapier = require(corePackageDir);
} catch (err) {
// this err.stack doesn't give a nice traceback at all :-(
Expand Down Expand Up @@ -53,6 +42,5 @@ const localAppCommand = async (event) => {
};

module.exports = {
getLocalAppHandler,
localAppCommand,
};
3 changes: 1 addition & 2 deletions packages/core/include/zapierwrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// not intended to be loaded via require() - copied during build step
import path from 'node:path';
import zapier from 'zapier-platform-core';

const appPath = path.resolve(process.cwd(), 'index.js');
const appPath = process.cwd();

export const handler = zapier.createAppHandler(appPath);
40 changes: 21 additions & 19 deletions packages/core/src/tools/create-lambda-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,30 @@ const getAppRawOverride = (rpc, appRawOverride) => {

// Sometimes tests want to pass in an app object defined directly in the test,
// so allow for that, and an event.appRawOverride for "buildless" apps.
const loadApp = (event, rpc, appRawOrPath) => {
return new ZapierPromise((resolve, reject) => {
const appRaw = _.isString(appRawOrPath)
? require(appRawOrPath)
: appRawOrPath;

if (event && event.appRawOverride) {
if (
Array.isArray(event.appRawOverride) &&
event.appRawOverride.length > 1 &&
!event.appRawOverride[0]
) {
event.appRawOverride[0] = appRaw;
}
const loadApp = async (event, rpc, appRawOrPath) => {
let appRaw;
if (typeof appRawOrPath === 'string') {
appRaw = await import(appRawOrPath);
if (appRaw.default) {
appRaw = appRaw.default;
}
} else {
appRaw = appRawOrPath;
}

return getAppRawOverride(rpc, event.appRawOverride)
.then((appRawOverride) => resolve(appRawOverride))
.catch((err) => reject(err));
if (event && event.appRawOverride) {
if (
Array.isArray(event.appRawOverride) &&
event.appRawOverride.length > 1 &&
!event.appRawOverride[0]
) {
event.appRawOverride[0] = appRaw;
}

return resolve(appRaw);
});
return getAppRawOverride(rpc, event.appRawOverride);
}

return appRaw;
};

const createLambdaHandler = (appRawOrPath) => {
Expand Down

0 comments on commit 55ecb90

Please sign in to comment.