Refactor and add generators for keystatic and package
The changes include refactoring the create-reader in cms keystatic and changing @keystatic/next version. Additionally, code generators for keystatic and package have been added, and corresponding templates have been organized into their respective directories.
This commit is contained in:
@@ -1,83 +1,11 @@
|
||||
import { execSync } from "node:child_process";
|
||||
import type { PlopTypes } from "@turbo/gen";
|
||||
import type { PlopTypes } from '@turbo/gen';
|
||||
|
||||
import { createKeystaticAdminGenerator } from './templates/keystatic/generator';
|
||||
import { createPackageGenerator } from './templates/package/generator';
|
||||
|
||||
// List of generators to be registered
|
||||
const generators = [createPackageGenerator, createKeystaticAdminGenerator];
|
||||
|
||||
export default function generator(plop: PlopTypes.NodePlopAPI): void {
|
||||
plop.setGenerator("init", {
|
||||
description: "Generate a new package for the Monorepo",
|
||||
prompts: [
|
||||
{
|
||||
type: "input",
|
||||
name: "name",
|
||||
message:
|
||||
"What is the name of the package? (You can skip the `@kit/` prefix)",
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
name: "deps",
|
||||
message:
|
||||
"Enter a space separated list of dependencies you would like to install",
|
||||
},
|
||||
],
|
||||
actions: [
|
||||
(answers) => {
|
||||
if ("name" in answers && typeof answers.name === "string") {
|
||||
if (answers.name.startsWith("@kit/")) {
|
||||
answers.name = answers.name.replace("@kit/", "");
|
||||
}
|
||||
}
|
||||
return "Config sanitized";
|
||||
},
|
||||
{
|
||||
type: "add",
|
||||
path: "packages/{{ name }}/package.json",
|
||||
templateFile: "templates/package.json.hbs",
|
||||
},
|
||||
{
|
||||
type: "add",
|
||||
path: "packages/{{ name }}/tsconfig.json",
|
||||
templateFile: "templates/tsconfig.json.hbs",
|
||||
},
|
||||
{
|
||||
type: "add",
|
||||
path: "packages/{{ name }}/index.ts",
|
||||
template: "export * from './src';",
|
||||
},
|
||||
{
|
||||
type: "add",
|
||||
path: "packages/{{ name }}/src/index.ts",
|
||||
template: "export const name = '{{ name }}';",
|
||||
},
|
||||
{
|
||||
type: "modify",
|
||||
path: "packages/{{ name }}/package.json",
|
||||
async transform(content, answers) {
|
||||
const pkg = JSON.parse(content);
|
||||
|
||||
for (const dep of answers.deps.split(" ").filter(Boolean)) {
|
||||
const version = await fetch(
|
||||
`https://registry.npmjs.org/-/package/${dep}/dist-tags`,
|
||||
)
|
||||
.then((res) => res.json())
|
||||
.then((json) => json.latest);
|
||||
pkg.dependencies![dep] = `^${version}`;
|
||||
}
|
||||
return JSON.stringify(pkg, null, 2);
|
||||
},
|
||||
},
|
||||
async (answers) => {
|
||||
/**
|
||||
* Install deps and format everything
|
||||
*/
|
||||
execSync("pnpm manypkg fix", {
|
||||
stdio: "inherit",
|
||||
});
|
||||
execSync(
|
||||
`pnpm prettier --write packages/${
|
||||
(answers as { name: string }).name
|
||||
}/** --list-different`,
|
||||
);
|
||||
return "Package scaffolded";
|
||||
},
|
||||
],
|
||||
});
|
||||
generators.forEach((gen) => gen(plop));
|
||||
}
|
||||
|
||||
65
turbo/generators/templates/keystatic/generator.ts
Normal file
65
turbo/generators/templates/keystatic/generator.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { PlopTypes } from '@turbo/gen';
|
||||
import { execSync } from 'node:child_process';
|
||||
|
||||
export function createKeystaticAdminGenerator(plop: PlopTypes.NodePlopAPI) {
|
||||
return plop.setGenerator('init', {
|
||||
description: 'Generate a the admin for Keystatic',
|
||||
prompts: [],
|
||||
actions: [
|
||||
{
|
||||
type: 'add',
|
||||
path: 'apps/web/app/keystatic/layout.tsx',
|
||||
templateFile: 'templates/keystatic/layout.tsx.hbs',
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: 'apps/web/app/keystatic/[[...params]]/page.tsx',
|
||||
templateFile: 'templates/keystatic/page.tsx.hbs',
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: 'apps/web/app/api/keystatic/[...params]/route.ts',
|
||||
templateFile: 'templates/keystatic/route.ts.hbs',
|
||||
},
|
||||
{
|
||||
type: 'modify',
|
||||
path: 'apps/web/package.json',
|
||||
async transform(content, answers) {
|
||||
const pkg = JSON.parse(content);
|
||||
const dep = `@keystatic/next`;
|
||||
|
||||
const version = await fetch(
|
||||
`https://registry.npmjs.org/-/package/${dep}/dist-tags`,
|
||||
)
|
||||
.then((res) => res.json())
|
||||
.then((json) => json.latest);
|
||||
|
||||
pkg.dependencies![dep] = `^${version}`;
|
||||
pkg.dependencies!['@kit/keystatic'] = `workspace:*`;
|
||||
|
||||
return JSON.stringify(pkg, null, 2);
|
||||
},
|
||||
},
|
||||
async (answers) => {
|
||||
/**
|
||||
* Install deps and format everything
|
||||
*/
|
||||
execSync('pnpm manypkg fix', {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
|
||||
execSync('pnpm i', {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
|
||||
execSync(
|
||||
`pnpm prettier --write packages/${
|
||||
(answers as { name: string }).name
|
||||
}/** --list-different`,
|
||||
);
|
||||
|
||||
return `Keystatic admin generated!`;
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
7
turbo/generators/templates/keystatic/layout.tsx.hbs
Normal file
7
turbo/generators/templates/keystatic/layout.tsx.hbs
Normal file
@@ -0,0 +1,7 @@
|
||||
import KeystaticAdmin from '@kit/keystatic/admin';
|
||||
|
||||
export default function Layout() {
|
||||
return (
|
||||
<KeystaticAdmin />
|
||||
);
|
||||
}
|
||||
5
turbo/generators/templates/keystatic/page.tsx.hbs
Normal file
5
turbo/generators/templates/keystatic/page.tsx.hbs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/app/keystatic/[[...params]]/page.tsx
|
||||
|
||||
export default function Page() {
|
||||
return null;
|
||||
}
|
||||
3
turbo/generators/templates/keystatic/route.ts.hbs
Normal file
3
turbo/generators/templates/keystatic/route.ts.hbs
Normal file
@@ -0,0 +1,3 @@
|
||||
import { keystaticRouteHandlers } from '@kit/keystatic/route-handler';
|
||||
|
||||
export const { POST, GET } = keystaticRouteHandlers;
|
||||
84
turbo/generators/templates/package/generator.ts
Normal file
84
turbo/generators/templates/package/generator.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import type { PlopTypes } from '@turbo/gen';
|
||||
import { execSync } from 'node:child_process';
|
||||
|
||||
export function createPackageGenerator(plop: PlopTypes.NodePlopAPI) {
|
||||
plop.setGenerator('init', {
|
||||
description: 'Generate a new package for the Monorepo',
|
||||
prompts: [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'name',
|
||||
message:
|
||||
'What is the name of the package? (You can skip the `@kit/` prefix)',
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'deps',
|
||||
message:
|
||||
'Enter a space separated list of dependencies you would like to install',
|
||||
},
|
||||
],
|
||||
actions: [
|
||||
(answers) => {
|
||||
if ('name' in answers && typeof answers.name === 'string') {
|
||||
if (answers.name.startsWith('@kit/')) {
|
||||
answers.name = answers.name.replace('@kit/', '');
|
||||
}
|
||||
}
|
||||
return 'Config sanitized';
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: 'packages/{{ name }}/package.json',
|
||||
templateFile: 'templates/package/package.json.hbs',
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: 'packages/{{ name }}/tsconfig.json',
|
||||
templateFile: 'templates/package/tsconfig.json.hbs',
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: 'packages/{{ name }}/index.ts',
|
||||
template: "export * from './src';",
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: 'packages/{{ name }}/src/index.ts',
|
||||
template: "export const name = '{{ name }}';",
|
||||
},
|
||||
{
|
||||
type: 'modify',
|
||||
path: 'packages/{{ name }}/package.json',
|
||||
async transform(content, answers) {
|
||||
const pkg = JSON.parse(content);
|
||||
|
||||
for (const dep of answers.deps.split(' ').filter(Boolean)) {
|
||||
const version = await fetch(
|
||||
`https://registry.npmjs.org/-/package/${dep}/dist-tags`,
|
||||
)
|
||||
.then((res) => res.json())
|
||||
.then((json) => json.latest);
|
||||
|
||||
pkg.dependencies![dep] = `^${version}`;
|
||||
}
|
||||
return JSON.stringify(pkg, null, 2);
|
||||
},
|
||||
},
|
||||
async (answers) => {
|
||||
/**
|
||||
* Install deps and format everything
|
||||
*/
|
||||
execSync('pnpm manypkg fix', {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
execSync(
|
||||
`pnpm prettier --write packages/${
|
||||
(answers as { name: string }).name
|
||||
}/** --list-different`,
|
||||
);
|
||||
return 'Package scaffolded';
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user