Skip to content

Commit

Permalink
initial updates for init
Browse files Browse the repository at this point in the history
  • Loading branch information
kreddlear committed Feb 26, 2025
1 parent b13bd0c commit 1ffbde3
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/cli/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,13 @@ This doesn't register or deploy the integration with Zapier - try the `zapier re

**Flags**
* `-t, --template` | The template to start your integration with. One of `[basic-auth | callback | custom-auth | digest-auth | dynamic-dropdown | files | minimal | oauth1-trello | oauth2 | search-or-create | session-auth | typescript]`.
* `-m, --module` | Choose module type: CommonJS or ES Modules. Only enabled for Typescript and Minimal templates. One of `[commonjs | esm]`. Defaults to `commonjs`.
* `-d, --debug` | Show extra debugging output.

**Examples**
* `zapier init myapp`
* `zapier init ./path/myapp --template oauth2`
* `zapier init ./path/myapp --template minimal --module esm`


## integrations
Expand Down
83 changes: 78 additions & 5 deletions packages/cli/src/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,28 @@ const writeGitignore = (gen) => {
};

const writeGenericPackageJson = (gen, packageJsonExtension) => {
const moduleExtension =
gen.options.module === 'esm'
? {
exports: {
import: './index.js',
require: './index.js',
},
type: 'module',
}
: {
main: 'index.js',
};

const fullExtension = merge(moduleExtension, packageJsonExtension);

gen.fs.writeJSON(
gen.destinationPath('package.json'),
merge(
{
name: gen.options.packageName,
version: '1.0.0',
description: '',
main: 'index.js',
scripts: {
test: 'jest --testTimeout 10000',
},
Expand All @@ -51,20 +65,34 @@ const writeGenericPackageJson = (gen, packageJsonExtension) => {
},
private: true,
},
packageJsonExtension,
fullExtension,
),
);
};

const writeTypeScriptPackageJson = (gen, packageJsonExtension) => {
const moduleExtension =
gen.options.module === 'esm'
? {
exports: {
import: './src/index.js',
require: './src/index.js',
},
type: 'module',
}
: {
main: 'src/index.js',
};

const fullExtension = merge(moduleExtension, packageJsonExtension);

gen.fs.writeJSON(
gen.destinationPath('package.json'),
merge(
{
name: gen.options.packageName,
version: '1.0.0',
description: '',
main: 'src/index.js',
scripts: {
test: 'vitest',
},
Expand All @@ -76,14 +104,18 @@ const writeTypeScriptPackageJson = (gen, packageJsonExtension) => {
},
private: true,
},
packageJsonExtension,
fullExtension,
),
);
};

const writeGenericIndex = (gen, hasAuth) => {
const templatePath =
gen.options.module === 'esm'
? 'index-esm.template.js'
: 'index.template.js';
gen.fs.copyTpl(
gen.templatePath('index.template.js'),
gen.templatePath(templatePath),
gen.destinationPath('index.js'),
{ corePackageName: PLATFORM_PACKAGE, hasAuth },
);
Expand Down Expand Up @@ -185,6 +217,19 @@ const writeForStandaloneTypeScriptTemplate = (gen) => {

writeTypeScriptPackageJson(gen, packageJsonExtension);

if (gen.options.module === 'esm') {
gen.fs.write(
gen.destinationPath('index.js'),
"module.exports = require('./dist').default;",
);
} else {
// TODO test that this works
gen.fs.write(
gen.destinationPath('index.js'),
'export { default } from "./dist";',
);
}

gen.fs.copy(
gen.templatePath(gen.options.template, '**', '*.{js,json,ts}'),
gen.destinationPath(),
Expand All @@ -206,6 +251,8 @@ const TEMPLATE_ROUTES = {
typescript: writeForStandaloneTypeScriptTemplate,
};

const ESM_SUPPORTED_TEMPLATES = ['minimal', 'typescript'];

const TEMPLATE_CHOICES = Object.keys(TEMPLATE_ROUTES);

class ProjectGenerator extends Generator {
Expand Down Expand Up @@ -234,6 +281,32 @@ class ProjectGenerator extends Generator {
]);
this.options.template = this.answers.template;
}

// TODO make sure the default doesn't make this irrelevant
if (
ESM_SUPPORTED_TEMPLATES.includes(this.options.template) &&
!this.options.module
) {
this.answers = await this.prompt([
{
type: 'list',
name: 'module',
choices: ['commonjs', 'esm'],
message: 'Choose module type:',
default: 'commonjs',
},
]);
this.options.module = this.answers.module;
}

if (
!ESM_SUPPORTED_TEMPLATES.includes(this.options.template) &&
this.options.module === 'esm'
) {
throw new Error(
'ESM is not supported for this template, please use a different template or set the module to commonjs',
);
}
}

writing() {
Expand Down
36 changes: 36 additions & 0 deletions packages/cli/src/generators/templates/index-esm.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<% if (hasAuth) { %>
import {
config as authentication,
befores = [],
afters = [],
} from './authentication';
<% } %>

import { version as packageVersion } from './package.json';
import { version as platformVersion } from '<%= corePackageName %>';

export default {
// This is just shorthand to reference the installed dependencies you have.
// Zapier will need to know these before we can upload.
version: packageVersion,
platformVersion: platformVersion,

<% if (hasAuth) { %>
authentication,

beforeRequest: [...befores],

afterResponse: [...afters],
<% } %>

// If you want your trigger to show up, you better include it here!
triggers: {},

// If you want your searches to show up, you better include it here!
searches: {},

// If you want your creates to show up, you better include it here!
creates: {},

resources: {},
};
8 changes: 8 additions & 0 deletions packages/cli/src/oclif/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ InitCommand.flags = buildFlags({
description: 'The template to start your integration with.',
options: TEMPLATE_CHOICES,
}),
module: Flags.string({
char: 'm',
description:
'Choose module type: CommonJS or ES Modules. Only enabled for Typescript and Minimal templates.',
options: ['commonjs', 'esm'],
default: 'commonjs',
}),
},
});
InitCommand.args = {
Expand All @@ -42,6 +49,7 @@ InitCommand.args = {
InitCommand.examples = [
'zapier init myapp',
'zapier init ./path/myapp --template oauth2',
'zapier init ./path/myapp --template minimal --module esm',
];
InitCommand.description = `Initialize a new Zapier integration with a project template.
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/smoke-tests/smoke-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ describe('smoke tests - setup will take some time', function () {
fs.existsSync(appPackageJson).should.be.true();
});

it('zapier init -t minimal -m esm', () => {
runCommand(
context.cliBin,
['init', 'awesome-esm-app', '-t', 'minimal', '-m', 'esm'],
{
cwd: context.workdir,
},
);

const newAppDir = path.join(context.workdir, 'awesome-esm-app');
fs.existsSync(newAppDir).should.be.true();

const appIndexJs = path.join(newAppDir, 'index.js');
const appPackageJson = path.join(newAppDir, 'package.json');
// TODO add a check for the module type
fs.existsSync(appIndexJs).should.be.true();
fs.existsSync(appPackageJson).should.be.true();
});

it('zapier scaffold trigger neat (JS)', () => {
runCommand(context.cliBin, ['init', 'scaffold-town', '-t', 'minimal'], {
cwd: context.workdir,
Expand Down

0 comments on commit 1ffbde3

Please sign in to comment.