Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement plop base #1249

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-ants-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"start-frontend": minor
---

use plop for project code generation
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ dist
build
public
.turbo

# Exclude Handlebars.js (.hbs) files
# TODO: Figure out how to format hanldebar templates
**/*.hbs
ptrkdan marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions code/react/handlebar-templates/App.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Suspense } from "react";
{{#if router}}
import { Router } from './routes';
{{else}}
import Home from './ui/pages';
{{/if}}
import { AppProviders } from "./context";

function App() {
return (
<AppProviders>
<Suspense>
{{#if router}}
<Router />
{{else}}
<Home />
{{/if}}
</Suspense>
</AppProviders>
)
}

export default App;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"eslint-config-custom": "workspace:*",
"husky": "9.1.6",
"lint-staged": "15.2.2",
"prettier": "2.8.4",
"prettier": "3.2.5",
"turbo": "1.13.4"
},
"lint-staged": {
Expand Down
1 change: 1 addition & 0 deletions packages/start-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"gradient-string": "2.0.2",
"inquirer": "8.2.3",
"meow": "7.1.1",
"node-plop": "0.32.0",
"picocolors": "1.1.0"
},
"devDependencies": {
Expand Down
20 changes: 16 additions & 4 deletions packages/start-frontend/src/index.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import { format } from "prettier";
import prettier from "prettier";
import fse from "fs-extra";
import meow from "meow";
import os from "node:os";
Expand Down Expand Up @@ -236,7 +236,7 @@ async function copyTests(
/**
* Copy common files into the target app directory
*/
function copyCommon(appDir: string, sharedConfigDir: string) {
async function copyCommon(appDir: string, sharedConfigDir: string) {
fse.copySync(`${sharedConfigDir}/gitignore`, `${appDir}/gitignore`);

// Rename dot files
Expand Down Expand Up @@ -268,7 +268,9 @@ function copyCommon(appDir: string, sharedConfigDir: string) {
// Rewrite tsconfig.json
fse.writeFileSync(
path.join(appDir, "tsconfig.json"),
format(JSON.stringify(tsconfigObjs, null, 2), { parser: "json" })
await prettier.format(JSON.stringify(tsconfigObjs, null, 2), {
parser: "json",
})
);
}

Expand Down Expand Up @@ -329,8 +331,18 @@ async function run() {
removeEslintConfig();
}

const { default: nodePlop } = await import("node-plop");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally importing nodePlop throws an ERR_REQUIRE_ESM error. (See #1222 (comment))

In order to successfully build with node-plop, dynamic importing it seem to work, with the plopFile.ts exporting using CommonJS syntax.

const plopFilePath = path.resolve(__dirname, "plopfile.js");
const plop = await nodePlop(plopFilePath, {
destBasePath: appDir,
force: true,
});

const baseGenerator = plop.getGenerator("constructBase");
await baseGenerator.runActions({});

// Copy commons
copyCommon(appDir, sharedConfigDir);
await copyCommon(appDir, sharedConfigDir);

// stop spinner
loadingStop();
Expand Down
29 changes: 29 additions & 0 deletions packages/start-frontend/src/plopfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { NodePlopAPI } from "node-plop";
import path from "node:path";
import prettier from "prettier";

const TEMP_DIR = path.resolve(__dirname, "temp");

module.exports = function (plop: NodePlopAPI) {
plop.setHelper("reverseEach", (ctx, { fn }) =>
ctx.reverse().map(fn).join("")
);

plop.setGenerator("constructBase", {
actions: [
{
type: "add",
force: true,
path: "src/app.tsx",
templateFile: path.resolve(
TEMP_DIR,
"handlebar-templates",
"app.tsx.hbs"
),
transform: async (template) => {
return await prettier.format(template, { parser: "typescript" });
},
},
],
});
};
3 changes: 2 additions & 1 deletion packages/start-frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "tsconfig/library.json",
"compilerOptions": {
"rootDir": "."
"rootDir": ".",
"module": "ESNext"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow the (dynamic) import of node-plop

},
"include": ["src", "tsup.config.ts"],
"exclude": ["templates"]
Expand Down
3 changes: 2 additions & 1 deletion packages/start-frontend/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { defineConfig, Options } from "tsup";

export default defineConfig((options: Options) => ({
entry: ["src/index.ts"],
entry: ["src/index.ts", "src/plopfile.ts"],
format: ["cjs"],
clean: true,
define: {
"process.env.BRANCH_NAME": `"${process.env.BRANCH_NAME}"`,
},
shims: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://tsup.egoist.dev/#inject-cjs-and-esm-shims
I believe it's enabled to support both ESM and CJS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without it, the build produces errors since the generated build includes import.meta.url

...options,
}));
Loading
Loading