Skip to content

Commit 66b9421

Browse files
committed
feat: use improved extension templates
1 parent e769a72 commit 66b9421

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.3",
2+
"version": "1.0.4",
33
"name": "@elfsquad/cli",
44
"main": "dist/index.js",
55
"scripts": {

src/extension.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import chalk from 'chalk'
33
import fs from 'fs'
44
import { getAccessToken } from './auth.js'
55
import JSZip from 'jszip'
6+
import { exec } from 'child_process'
7+
68

79

810
export const register = (cli: Argv) => {
@@ -18,7 +20,7 @@ export const register = (cli: Argv) => {
1820
describe: 'Template to use',
1921
alias: 't',
2022
type: 'string',
21-
choices: ['dialog', 'action'],
23+
choices: ['dialog', 'action', 'instant'],
2224
required: true
2325
})
2426
}, init)
@@ -29,12 +31,17 @@ export const register = (cli: Argv) => {
2931
const init = async (argv: Arguments<{ name: string, template: string }>) => {
3032
const templates: Record<string, Record<string, string>> = {
3133
dialog: DIALOG_SAMPLE_FILES,
32-
action: ACTION_SAMPLE_FILES
34+
action: ACTION_SAMPLE_FILES,
35+
instant: INSTANT_SAMPLE_FILES
3336
}
3437

3538
const path = (suffix: string) => `${argv.name}/${suffix}`;
3639

3740
// 1. Create a new directory with the name of the extension
41+
if (fs.existsSync(argv.name)) {
42+
console.error(chalk.red(`Directory ${argv.name} already exists. Please choose a different name.`));
43+
return;
44+
}
3845
await fs.promises.mkdir(argv.name)
3946
const srcDir = path('src')
4047
await fs.promises.mkdir(srcDir)
@@ -46,6 +53,22 @@ const init = async (argv: Arguments<{ name: string, template: string }>) => {
4653
await fs.promises.writeFile(fileName, content.replace('{{ EXTENSION_NAME }}', argv.name))
4754
console.log(chalk.green(`Created ${fileName}`))
4855
}
56+
57+
// 3. Run npm install and wait for it to finish
58+
console.log(chalk.green('Running npm install...'))
59+
await new Promise<void>((resolve, reject) => {
60+
exec('npm install', { cwd: argv.name }, (error, stdout, stderr) => {
61+
if (error) {
62+
console.error(chalk.red(`Error running npm install: ${error.message}`));
63+
reject(error);
64+
} else {
65+
console.log(stdout);
66+
resolve();
67+
}
68+
});
69+
})
70+
71+
console.log(chalk.green(`Extension ${argv.name} initialized successfully!`))
4972
}
5073

5174
const publish = async () => {
@@ -73,7 +96,7 @@ const publish = async () => {
7396

7497
// 4. Upload the zip file to the server
7598
const accessToken = await getAccessToken()
76-
const url = 'https://api.elfsquad.io/api/2/extensions'
99+
const url = 'http://localhost:5101/api/2/extensions'
77100
const zipBlob = new Blob([zipContent], { type: 'application/zip' });
78101

79102
const formData = new FormData()
@@ -110,7 +133,7 @@ const defaultPackageJson = `{
110133
}`;
111134

112135
const DIALOG_SAMPLE_FILES = {
113-
'package.json': defaultPackageJson.replace('{{ BUILD }}', 'tsc src/*.ts -noEmit && esbuild src/index.ts --bundle --platform=node --outfile=dist/index.js && cp src/index.html dist/index.html'),
136+
'package.json': defaultPackageJson.replace('{{ BUILD }}', 'esbuild src/index.ts --bundle --platform=browser --target=es2015 --outfile=dist/index.js && cp src/index.html dist/index.html'),
114137
'elfsquadrc.yml': `identifier: "{{ EXTENSION_NAME }}"
115138
116139
page_extensions:
@@ -120,11 +143,14 @@ page_extensions:
120143
color: primary
121144
122145
names:
123-
en: Execute
146+
en: Open dialog
124147
125148
executable:
126149
type: "dialog"
127150
entrypoint: "index.html"
151+
parameters:
152+
width: 50vw
153+
height: 70vh
128154
`,
129155
'src/index.ts': `import { ui, dialog, api } from '@elfsquad/custom-scripting';
130156
@@ -170,13 +196,28 @@ page_extensions:
170196
color: primary
171197
172198
names:
173-
en: Execute
199+
en: Execute action
174200
175201
executable:
176202
type: "action"
177203
entrypoint: "index.js"
178204
`,
179205
'src/index.ts': `import { api } from '@elfsquad/custom-scripting';
180206
181-
alert('Hello from my Elfsquad extension!');`
207+
console.log('Hello from my Elfsquad extension!');`
182208
}
209+
210+
const INSTANT_SAMPLE_FILES = {
211+
'package.json': defaultPackageJson.replace('{{ BUILD }}', 'esbuild src/index.ts --bundle --platform=browser --target=es2015 --outfile=dist/index.js'),
212+
'elfsquadrc.yml': `identifier: "{{ EXTENSION_NAME }}"
213+
214+
page_extensions:
215+
quotation:
216+
actions:
217+
- executable:
218+
type: "instant"
219+
entrypoint: "index.js"
220+
`,
221+
'src/index.ts': `alert('Hello from my Elfsquad instant extension!');`
222+
}
223+

0 commit comments

Comments
 (0)