Skip to content

Commit 0cc957c

Browse files
Update/cli template (#150)
* Update CLI template * Update init call to use project name in template
1 parent 71d2fe8 commit 0cc957c

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

packages/cli/src/init.ts

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,17 @@ export async function initializeProject(
111111
description: '',
112112
script: path.relative(dirPath, scriptPath),
113113
scopes: project.scopes,
114+
blocks: [
115+
{
116+
id: project.name,
117+
title: project.title,
118+
description: 'My GitBook Integration',
119+
},
120+
],
114121
secrets: {},
115122
});
116123

117-
await fs.promises.writeFile(scriptPath, generateScript());
124+
await fs.promises.writeFile(scriptPath, generateScript(project));
118125
await fs.promises.writeFile(path.join(dirPath, 'tsconfig.json'), generateTSConfig());
119126
await fs.promises.writeFile(path.join(dirPath, '.eslintrc.json'), generateESLint());
120127

@@ -177,25 +184,56 @@ export function installDependencies(dirPath: string): Promise<void> {
177184
/**
178185
* Generate the script code.
179186
*/
180-
export function generateScript(): string {
187+
export function generateScript(project: { name: string }): string {
181188
const src = detent(`
182-
import { createIntegration, FetchEventCallback, RuntimeContext } from '@gitbook/runtime';
183-
184-
type IntegrationContext = {} & RuntimeContext;
185-
186-
const handleFetchEvent: FetchEventCallback<IntegrationContext> = async (request, context) => {
187-
// Use the API to make requests to GitBook
188-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
189-
const { api } = context;
190-
191-
return new Response('Hello World');
192-
};
193-
194-
export default createIntegration({
195-
fetch: handleFetchEvent,
196-
components: [],
197-
events: {},
198-
});
189+
import {
190+
createIntegration,
191+
createComponent,
192+
FetchEventCallback,
193+
RuntimeContext,
194+
} from "@gitbook/runtime";
195+
196+
type IntegrationContext = {} & RuntimeContext;
197+
198+
const handleFetchEvent: FetchEventCallback<IntegrationContext> = async (
199+
request,
200+
context
201+
) => {
202+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
203+
const { api } = context;
204+
const user = api.user.getAuthenticatedUser();
205+
206+
return new Response(JSON.stringify(user));
207+
};
208+
209+
const exampleBlock = createComponent({
210+
componentId: ${project.name},
211+
initialState: (props) => {
212+
return {
213+
message: "Click Me",
214+
};
215+
},
216+
action: async (element, action, context) => {
217+
switch (action.action) {
218+
case "click":
219+
console.log("Button Clicked");
220+
return {};
221+
}
222+
},
223+
render: async (element, action, context) => {
224+
return (
225+
<block>
226+
<button label={element.state.message} onPress={{ action: "click" }} />
227+
</block>
228+
);
229+
},
230+
});
231+
232+
export default createIntegration({
233+
fetch: handleFetchEvent,
234+
components: [exampleBlock],
235+
events: {},
236+
});
199237
`).trim();
200238

201239
return `${src}\n`;

0 commit comments

Comments
 (0)