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

feat(turbo-gen): support TS config files #4950

Merged
merged 1 commit into from
May 15, 2023
Merged
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
2 changes: 2 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
"next-themes": "^0.2.0",
"nextra": "^2.1.0",
"nextra-theme-docs": "^2.1.0",
"node-fetch": "^2.6.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sharp": "^0.32.1",
"swr": "1.3.0"
},
"devDependencies": {
"@babel/core": "7.20.12",
"@turbo/gen": "workspace:*",
"@turbo/types": "workspace:*",
"@types/node": "^16.11.12",
"@types/react": "18.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const fs = require("fs");
const { releasePostStats, dateToday, majorMinor } = require("./utils");
import fs from "fs";
import { releasePostStats } from "./utils";
import * as helpers from "./helpers";
import type { PlopTypes } from "@turbo/gen";

module.exports = function (plop) {
export default function generator(plop: PlopTypes.NodePlopAPI): void {
// add helpers for use in templates
plop.setHelper("dateToday", dateToday);
plop.setHelper("majorMinor", majorMinor);
helpers.init(plop);

// create generators
plop.setGenerator("blog - release post", {
Expand Down Expand Up @@ -97,7 +98,6 @@ module.exports = function (plop) {
{
type: "list",
name: "post",
loop: false,
pageSize: 20,
message: "Which release post should the stats be updated?",
choices: () => {
Expand Down Expand Up @@ -143,4 +143,4 @@ module.exports = function (plop) {
},
],
});
};
}
14 changes: 14 additions & 0 deletions docs/turbo/generators/helpers.ts
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);
}
51 changes: 0 additions & 51 deletions docs/turbo/generators/utils.js

This file was deleted.

64 changes: 64 additions & 0 deletions docs/turbo/generators/utils.ts
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}`
),
Comment on lines +15 to +19
Copy link
Member Author

Choose a reason for hiding this comment

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

Just for you @mehulkar 😉

Copy link
Contributor

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 for node:test

]);

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);
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";
}
11 changes: 6 additions & 5 deletions packages/turbo-codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"update-check": "^1.5.4"
},
"devDependencies": {
"@turbo/gen": "workspace:*",
"@turbo/test-utils": "workspace:*",
"@turbo/tsconfig": "workspace:*",
"@turbo/types": "workspace:*",
"@turbo/utils": "workspace:*",
"@types/chalk-animation": "^1.6.0",
"@types/diff": "^5.0.2",
"@types/fs-extra": "^9.0.13",
Expand All @@ -51,11 +56,7 @@
"plop": "^3.1.1",
"semver": "^7.3.5",
"ts-jest": "^27.1.1",
"@turbo/tsconfig": "workspace:*",
"tsup": "^5.10.3",
"@turbo/test-utils": "workspace:*",
"@turbo/types": "workspace:*",
"@turbo/utils": "workspace:*",
"tsup": "^6.7.0",
"typescript": "^4.5.5"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const fs = require("fs-extra");
const path = require("path");
import path from "path";
import fs from "fs-extra";
import type { PlopTypes } from "@turbo/gen";

module.exports = function (plop, config) {
export default function generator(
plop: PlopTypes.NodePlopAPI,
config: PlopTypes.PlopCfg
): void {
plop.setGenerator("transformer", {
description: "Add a new transformer",
prompts: [
Expand Down Expand Up @@ -34,8 +38,11 @@ module.exports = function (plop, config) {
path: "__tests__/{{name}}.test.ts",
templateFile: "templates/transformer.test.hbs",
},
function createFixturesDirectory(answers) {
process.chdir(plop.getPlopfilePath());
function createFixturesDirectory(answers: { name?: string }) {
if (!answers.name) {
return "no name provided, skipping fixture directory creation";
}

const directory = path.join(
config.destBasePath,
"__tests__",
Expand All @@ -48,4 +55,4 @@ module.exports = function (plop, config) {
},
],
});
};
}
2 changes: 2 additions & 0 deletions packages/turbo-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"url": "https://github.com/vercel/turbo/issues"
},
"bin": "dist/cli.js",
"types": "dist/types.d.ts",
"scripts": {
"build": "tsup",
"test": "jest",
Expand All @@ -27,6 +28,7 @@
"minimatch": "^9.0.0",
"node-plop": "^0.26.3",
"semver": "^7.3.8",
"ts-node": "^10.9.1",
"update-check": "^1.5.4",
"validate-npm-package-name": "^5.0.0"
},
Expand Down
8 changes: 6 additions & 2 deletions packages/turbo-gen/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export interface DependencyGroups {
import type * as PlopTypes from "node-plop";

interface DependencyGroups {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
}

export interface PackageJson extends DependencyGroups {
interface PackageJson extends DependencyGroups {
name: string;
version: string;
private?: boolean;
Expand All @@ -16,3 +18,5 @@ export interface PackageJson extends DependencyGroups {
exports?: object;
scripts?: Record<string, string>;
}

export type { PlopTypes, DependencyGroups, PackageJson };
53 changes: 34 additions & 19 deletions packages/turbo-gen/src/utils/plop.ts
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
Copy link
Member Author

Choose a reason for hiding this comment

The 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,
Expand All @@ -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,
Expand All @@ -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);
}
});
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/turbo-gen/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defineConfig, Options } from "tsup";

export default defineConfig((options: Options) => ({
entry: ["src/cli.ts"],
entry: ["src/cli.ts", "src/types.ts"],
format: ["cjs"],
dts: true,
clean: true,
minify: true,
...options,
Expand Down
1 change: 1 addition & 0 deletions packages/turbo-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * as logger from "./logger";
// types
export type { PackageManagerAvailable } from "./managers";
export type { RepoInfo } from "./examples";
export type { TurboConfigs } from "./getTurboConfigs";
Loading