-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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(turbo-gen): support TS config files #4950
Merged
tknickman
merged 1 commit into
main
from
tomknickman/turbo-1103-support-ts-config-files
May 15, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { PlopTypes } from "@turbo/gen"; | ||
|
||
// helpers | ||
const dateToday = (): string => | ||
new Date().toISOString().split("T")[0].replace(/-/g, "/"); | ||
|
||
const majorMinor = (version: string): string => | ||
version.split(".").slice(0, 2).join("."); | ||
|
||
export function init(plop: PlopTypes.NodePlopAPI): void { | ||
// add helpers for use in templates | ||
plop.setHelper("dateToday", dateToday); | ||
plop.setHelper("majorMinor", majorMinor); | ||
} |
This file was deleted.
Oops, something went wrong.
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,64 @@ | ||
import fetch from "node-fetch"; | ||
|
||
interface Answers extends Object { | ||
turboStars: string; | ||
turboDownloads: string; | ||
turboYearsSaved: string; | ||
} | ||
|
||
const PUBLIC_TB_TOKEN = | ||
"p.eyJ1IjogIjAzYzA0Y2MyLTM1YTAtNDhhNC05ZTZjLThhMWE0NGNhNjhkZiIsICJpZCI6ICJmOWIzMTU5Yi0wOTVjLTQyM2UtOWIwNS04ZDZlNzIyNjEwNzIifQ.A3TOPdm3Lhmn-1x5m6jNvulCQbbgUeQfAIO3IaaAt5k"; | ||
|
||
export async function releasePostStats(answers: Answers): Promise<string> { | ||
const [starsResponse, downloadsResponse, timeSavedResponse] = | ||
await Promise.all([ | ||
fetch("https://api.github.com/repos/vercel/turbo"), | ||
fetch("https://api.npmjs.org/versions/turbo/last-week"), | ||
fetch( | ||
`https://api.us-east.tinybird.co/v0/pipes/turborepo_time_saved_ticker.json?token=${PUBLIC_TB_TOKEN}` | ||
), | ||
]); | ||
|
||
const [starsData, downloadsData, timeSavedData] = await Promise.all([ | ||
starsResponse.json() as { stargazers_count: number }, | ||
downloadsResponse.json() as { | ||
downloads: Array<{ [key: string]: number }>; | ||
}, | ||
timeSavedResponse.json() as { | ||
data: [ | ||
{ | ||
remote_cache_minutes_saved: number; | ||
local_cache_minutes_saved: number; | ||
} | ||
]; | ||
}, | ||
]); | ||
|
||
const totalMinutesSaved: number = | ||
timeSavedData.data[0].remote_cache_minutes_saved + | ||
timeSavedData.data[0].local_cache_minutes_saved; | ||
const totalYearsSaved: number = Math.floor(totalMinutesSaved / 60 / 24 / 365); | ||
tknickman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const weeklyDownloads: number = Object.keys(downloadsData.downloads).reduce( | ||
(sum, version) => sum + downloadsData.downloads[version], | ||
0 | ||
); | ||
|
||
console.log(JSON.stringify(weeklyDownloads)); | ||
|
||
const prettyRound = (num: number): string => { | ||
if (num < 1000) { | ||
return num.toString(); | ||
} else if (num < 1000000) { | ||
return (num / 1000).toFixed(1) + "k"; | ||
} else { | ||
return (num / 1000000).toFixed(1) + "M"; | ||
} | ||
}; | ||
|
||
// extend answers | ||
answers.turboStars = prettyRound(starsData.stargazers_count); | ||
answers.turboDownloads = prettyRound(weeklyDownloads); | ||
answers.turboYearsSaved = prettyRound(totalYearsSaved); | ||
|
||
return "Fetched stats for release post"; | ||
} |
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
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,27 +1,36 @@ | ||
import fs from "fs-extra"; | ||
import { Project } from "@turbo/workspaces"; | ||
import nodePlop, { NodePlopAPI, PlopGenerator } from "node-plop"; | ||
import { register } from "ts-node"; | ||
import path from "path"; | ||
import inquirer from "inquirer"; | ||
import { searchUp, getTurboConfigs, logger } from "@turbo/utils"; | ||
import { GeneratorError } from "./error"; | ||
|
||
// TODO: Support a TS config file | ||
const TURBO_GENERATOR_CONFIG = path.join("turbo", "generators", "config.js"); | ||
const SUPPORTED_CONFIG_EXTENSIONS = ["ts", "js", "cjs"]; | ||
const TURBO_GENERATOR_DIRECTORY = path.join("turbo", "generators"); | ||
|
||
// support root plopfile so that users with existing configurations can use them immediately | ||
const DEFAULT_ROOT_CONFIG_LOCATIONS = [ | ||
TURBO_GENERATOR_CONFIG, | ||
"plopfile.js", | ||
"plopfile.cjs", | ||
"plopfile.mjs", | ||
// config formats that will be automatically loaded from within workspaces | ||
const SUPPORTED_WORKSPACE_GENERATOR_CONFIGS = SUPPORTED_CONFIG_EXTENSIONS.map( | ||
(ext) => path.join(TURBO_GENERATOR_DIRECTORY, `config.${ext}`) | ||
); | ||
|
||
// config formats that will be automatically loaded from the root (support plopfiles so that users with existing configurations can use them immediately) | ||
const SUPPORTED_ROOT_GENERATOR_CONFIGS = [ | ||
...SUPPORTED_WORKSPACE_GENERATOR_CONFIGS, | ||
...SUPPORTED_CONFIG_EXTENSIONS.map((ext) => path.join(`plopfile.${ext}`)), | ||
]; | ||
|
||
export type Generator = PlopGenerator & { | ||
basePath: string; | ||
name: string; | ||
}; | ||
|
||
// init ts-node for plop to support ts configs | ||
register({ | ||
transpileOnly: true, | ||
}); | ||
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plop does support TS configs, but it relies on ts-node being configured plopjs/plop#192 (comment) |
||
|
||
export function getPlop({ | ||
project, | ||
configPath, | ||
|
@@ -44,8 +53,8 @@ export function getPlop({ | |
} | ||
} else { | ||
// look for a root config | ||
for (const defaultConfigPath of DEFAULT_ROOT_CONFIG_LOCATIONS) { | ||
const plopFile = path.join(project.paths.root, defaultConfigPath); | ||
for (const configPath of SUPPORTED_ROOT_GENERATOR_CONFIGS) { | ||
const plopFile = path.join(project.paths.root, configPath); | ||
try { | ||
plop = nodePlop(plopFile, { | ||
destBasePath: project.paths.root, | ||
|
@@ -70,10 +79,14 @@ export function getPlop({ | |
if (plop) { | ||
// add in all the workspace configs | ||
workspaceConfigs.forEach((c) => { | ||
plop?.load(c.config, { | ||
destBasePath: c.root, | ||
force: false, | ||
}); | ||
try { | ||
plop?.load(c.config, { | ||
destBasePath: c.root, | ||
force: false, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
} | ||
|
||
|
@@ -199,11 +212,13 @@ function getWorkspaceGeneratorConfigs({ project }: { project: Project }) { | |
root: string; | ||
}> = []; | ||
project.workspaceData.workspaces.forEach((w) => { | ||
if (fs.existsSync(path.join(w.paths.root, TURBO_GENERATOR_CONFIG))) { | ||
workspaceGeneratorConfigs.push({ | ||
config: path.join(w.paths.root, TURBO_GENERATOR_CONFIG), | ||
root: w.paths.root, | ||
}); | ||
for (const configPath of SUPPORTED_WORKSPACE_GENERATOR_CONFIGS) { | ||
if (fs.existsSync(path.join(w.paths.root, configPath))) { | ||
workspaceGeneratorConfigs.push({ | ||
config: path.join(w.paths.root, configPath), | ||
root: w.paths.root, | ||
}); | ||
} | ||
} | ||
}); | ||
return workspaceGeneratorConfigs; | ||
|
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
Oops, something went wrong.
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.
Just for you @mehulkar 😉
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.
YESSSSSSSSS. Next up we drop
jest
fornode:test