Skip to content

Commit

Permalink
update zapierwrapper to mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
kreddlear committed Feb 22, 2025
1 parent da76631 commit ee0b284
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
20 changes: 12 additions & 8 deletions packages/cli/src/tests/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ describe('build (runs slowly)', function () {
const tmpIndexPath = path.join(tmpProjectDir, 'index.js');

fs.outputFileSync(
path.join(tmpProjectDir, 'zapierwrapper.js'),
"console.log('hello!')",
path.join(tmpProjectDir, 'zapierwrapper.mjs'),
`console.log('hello!');
export const appPath = '${tmpProjectDir}/index.js';`,
);
fs.outputFileSync(tmpIndexPath, "console.log('hello!')");
fs.chmodSync(tmpIndexPath, 0o700);
Expand Down Expand Up @@ -153,8 +154,9 @@ describe('build (runs slowly)', function () {
const tmpIndexPath = path.join(tmpProjectDir, 'index.js');

fs.outputFileSync(
path.join(tmpProjectDir, 'zapierwrapper.js'),
"console.log('hello!')",
path.join(tmpProjectDir, 'zapierwrapper.mjs'),
`console.log('hello!')
export const appPath = '${tmpProjectDir}/index.js';`,
);
fs.outputFileSync(tmpIndexPath, "console.log('hello!')");
fs.chmodSync(tmpIndexPath, 0o700);
Expand Down Expand Up @@ -196,8 +198,9 @@ describe('build (runs slowly)', function () {
const tmpZapierAppPath = path.join(tmpProjectDir, '.zapierapprc');

fs.outputFileSync(
path.join(tmpProjectDir, 'zapierwrapper.js'),
"console.log('hello!')",
path.join(tmpProjectDir, 'zapierwrapper.mjs'),
`console.log('hello!')
export const appPath = '${tmpProjectDir}/index.js';`,
);
fs.outputFileSync(tmpIndexPath, "console.log('hello!')");
fs.outputFileSync(tmpReadmePath, 'README');
Expand Down Expand Up @@ -251,8 +254,9 @@ describe('build (runs slowly)', function () {
const tmpEnvironmentPath = path.join(tmpProjectDir, '.environment');

fs.outputFileSync(
path.join(tmpProjectDir, 'zapierwrapper.js'),
"console.log('hello!')",
path.join(tmpProjectDir, 'zapierwrapper.mjs'),
`console.log('hello!')
export const appPath = '${tmpProjectDir}/index.js';`,
);
fs.outputFileSync(tmpIndexPath, "console.log('hello!')");
fs.chmodSync(tmpIndexPath, 0o700);
Expand Down
24 changes: 8 additions & 16 deletions packages/cli/src/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const requiredFiles = async ({ cwd, entryPoints }) => {
metafile: true,
logLevel: 'warning',
external: ['../test/userapp'],
format: 'esm',
});

return Object.keys(result.metafile.inputs).map((path) =>
Expand Down Expand Up @@ -160,11 +161,8 @@ const writeZipFromPaths = (dir, zipPath, paths) => {
};

const makeZip = async (dir, zipPath, disableDependencyDetection) => {
const entryPoints = [
path.resolve(dir, 'zapierwrapper.js'),
// TODO do not hardcode index.js here!
path.resolve(dir, 'index.js'),
];
const { appPath } = await import(`${dir}/zapierwrapper.mjs`);
const entryPoints = [path.resolve(dir, 'zapierwrapper.mjs'), appPath];

let paths;

Expand Down Expand Up @@ -202,9 +200,9 @@ const makeSourceZip = async (dir, zipPath) => {
};

// Similar to utils.appCommand, but given a ready to go app
// with a different location and ready to go zapierwrapper.js.
// with a different location and ready to go zapierwrapper.mjs.
const _appCommandZapierWrapper = async (dir, event) => {
const app = await import(`${dir}/zapierwrapper.js`);
const app = await import(`${dir}/zapierwrapper.mjs`);
event = { ...event, calledFromCli: true };
return new Promise((resolve, reject) => {
app.handler(event, {}, (err, resp) => {
Expand Down Expand Up @@ -382,7 +380,7 @@ const _buildFunc = async ({

if (printProgress) {
endSpinner();
startSpinner('Applying entry point file');
startSpinner('Applying entry point files');
}

// TODO: should this routine for include exist elsewhere?
Expand All @@ -392,20 +390,14 @@ const _buildFunc = async ({
'node_modules',
constants.PLATFORM_PACKAGE,
'include',
'zapierwrapper.js',
'zapierwrapper.mjs',
),
);
await writeFile(
path.join(tmpDir, 'zapierwrapper.js'),
path.join(tmpDir, 'zapierwrapper.mjs'),
zapierWrapperBuf.toString(),
);

// copy root directory to node_modules, excluding root directory itself
// this allows the import in _appCommandZapierWrapper to succeed
await copyDir(wdir, path.join(tmpDir, 'node_modules', path.basename(wdir)), {
filter: (src) => src !== wdir,
});

if (printProgress) {
endSpinner();
startSpinner('Building app definition.json');
Expand Down
7 changes: 0 additions & 7 deletions packages/core/include/zapierwrapper.js

This file was deleted.

22 changes: 22 additions & 0 deletions packages/core/include/zapierwrapper.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// not intended to be loaded via require() - copied during build step
import { createRequire } from 'module';
import path from 'node:path';
import zapier from 'zapier-platform-core';

const require = createRequire(import.meta.url);
let appPath;

try {
appPath = require.resolve(process.cwd());
} catch (e) {
// if it failed, try to resolve the app path with self-referencing
// in case index.js is not in the root directory
try {
appPath = require.resolve(path.basename(process.cwd()));
} catch (e2) {
throw new Error("Failed to resolve app path - make sure you have a package.json in the root directory");
}
}

export { appPath };
export const handler = zapier.createAppHandler(appPath);

0 comments on commit ee0b284

Please sign in to comment.