-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(generators): support starting from a template #4951
Merged
tknickman
merged 1 commit into
main
from
tomknickman/turbo-1105-give-the-option-to-create-a-generator
May 16, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = function generator(plop) { | ||
plop.setGenerator("example", { | ||
description: | ||
"An example Turborepo generator - creates a new file at the root of the project", | ||
prompts: [ | ||
{ | ||
type: "input", | ||
name: "file", | ||
message: "What is the name of the file to create?", | ||
}, | ||
{ | ||
type: "input", | ||
name: "author", | ||
message: "What is your name? (Will be added as the file author)", | ||
}, | ||
{ | ||
type: "list", | ||
name: "type", | ||
message: "What type of file should be created?", | ||
choices: [".md", ".txt"], | ||
}, | ||
], | ||
actions: [ | ||
{ | ||
type: "add", | ||
path: "{{ turbo.paths.root }}/{{ dashCase file }}{{ type }}", | ||
templateFile: "templates/turborepo-generators.hbs", | ||
}, | ||
], | ||
}); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/turbo-gen/src/templates/simple-js/templates/turborepo-generators.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Turborepo Generators | ||
|
||
Read the docs at [turbo.build](https://turbo.build/repo/docs). | ||
|
||
Created by {{ author }}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { PlopTypes } from "@turbo/gen"; | ||
|
||
export default function generator(plop: PlopTypes.NodePlopAPI): void { | ||
plop.setGenerator("example", { | ||
description: | ||
"An example Turborepo generator - creates a new file at the root of the project", | ||
prompts: [ | ||
{ | ||
type: "input", | ||
name: "file", | ||
message: "What is the name of the file to create?", | ||
}, | ||
{ | ||
type: "input", | ||
name: "author", | ||
message: "What is your name? (Will be added as the file author)", | ||
}, | ||
{ | ||
type: "list", | ||
name: "type", | ||
message: "What type of file should be created?", | ||
choices: [".md", ".txt"], | ||
}, | ||
], | ||
actions: [ | ||
{ | ||
type: "add", | ||
path: "{{ turbo.paths.root }}/{{ dashCase file }}{{ type }}", | ||
templateFile: "templates/turborepo-generators.hbs", | ||
}, | ||
], | ||
}); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/turbo-gen/src/templates/simple-ts/templates/turborepo-generators.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Turborepo Generators | ||
|
||
Read the docs at [turbo.build](https://turbo.build/repo/docs). | ||
|
||
Created by {{ author }}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Project } from "@turbo/workspaces"; | ||
import path from "path"; | ||
import fs from "fs-extra"; | ||
import { GeneratorError } from "./error"; | ||
|
||
export async function setupFromTemplate({ | ||
project, | ||
template, | ||
}: { | ||
project: Project; | ||
template: "ts" | "js"; | ||
}) { | ||
const configDirectory = path.join(project.paths.root, "turbo", "generators"); | ||
|
||
// TODO: could create some more complex starters in the future | ||
const toCopy = `simple-${template}`; | ||
|
||
// required to ensure we don't overwrite any existing files at this location | ||
if (await fs.pathExists(configDirectory)) { | ||
throw new GeneratorError( | ||
`Generator config directory already exists at ${configDirectory}`, | ||
{ type: "config_directory_already_exists" } | ||
); | ||
} | ||
|
||
// copy templates to project | ||
await fs.copy(path.join(__dirname, "templates", toCopy), configDirectory, { | ||
recursive: true, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { defineConfig, Options } from "tsup"; | ||
import fs from "fs-extra"; | ||
import chalk from "chalk"; | ||
|
||
export default defineConfig((options: Options) => ({ | ||
entry: ["src/cli.ts", "src/types.ts"], | ||
format: ["cjs"], | ||
dts: true, | ||
clean: true, | ||
minify: true, | ||
onSuccess: async () => { | ||
// start time | ||
const start = Date.now(); | ||
await fs.copy("src/templates", "dist/templates"); | ||
// make the output match | ||
console.log( | ||
chalk.hex("#7c5cad")("TEMPLATES"), | ||
"copied in", | ||
chalk.green(`${Date.now() - start}ms`) | ||
); | ||
}, | ||
...options, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { PlopTypes } from "@turbo/gen"; | ||
|
||
export default function generator(plop: PlopTypes.NodePlopAPI): void { | ||
plop.setGenerator("example", { | ||
description: | ||
"An example Turborepo generator - creates a new file at the root of the project", | ||
prompts: [ | ||
{ | ||
type: "input", | ||
name: "file", | ||
placeholder: "generator-docs", | ||
message: "What is the name of the file to create?", | ||
}, | ||
{ | ||
type: "input", | ||
name: "author", | ||
default: "turbobot", | ||
message: "What is your name? (Will be added as the file author)", | ||
}, | ||
{ | ||
type: "list", | ||
name: "type", | ||
message: "What type of file should be created?", | ||
choices: [".md", ".txt"], | ||
default: ".md", | ||
}, | ||
], | ||
actions: [ | ||
{ | ||
type: "add", | ||
path: "{{ turbo.paths.root }}/{{ dashCase file }}{{ type }}", | ||
templateFile: "templates/turborepo-generators.hbs", | ||
}, | ||
], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Turborepo Generators | ||
|
||
Read the docs at [turbo.build](https://turbo.build/repo/docs). | ||
|
||
Created by {{ author }}. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @chris-olszewski added here from your prev comment!