Skip to content

Commit

Permalink
consume turbo types
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Sep 21, 2022
1 parent d89e476 commit bf5fc0e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
migratePipeline,
migrateConfig,
} from "../src/transforms/migrate-env-var-dependencies";
import { TurboConfig } from "../src/types";
import type { Schema } from "turbo-types";

const getTestTurboConfig = (override: TurboConfig = {}): TurboConfig => {
const getTestTurboConfig = (override: Schema = { pipeline: {} }): Schema => {
const config = {
$schema: "./docs/public/schema.json",
globalDependencies: ["$GLOBAL_ENV_KEY"],
Expand Down Expand Up @@ -79,7 +79,7 @@ describe("migrate-env-var-dependencies", () => {
describe("migratePipeline", () => {
it("migrates pipeline with env var dependencies", async () => {
const config = getTestTurboConfig();
const { build } = config?.pipeline ?? {};
const { build } = config.pipeline;
const pipeline = migratePipeline(build);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Expand All @@ -97,7 +97,7 @@ describe("migrate-env-var-dependencies", () => {

it("migrates pipeline with no env var dependencies", async () => {
const config = getTestTurboConfig();
const { test } = config?.pipeline ?? {};
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline.env).toBeUndefined();
expect(pipeline?.dependsOn).toMatchInlineSnapshot(`
Expand All @@ -111,7 +111,7 @@ describe("migrate-env-var-dependencies", () => {
const config = getTestTurboConfig({
pipeline: { test: { env: ["$MY_ENV"], dependsOn: ["^build"] } },
});
const { test } = config?.pipeline ?? {};
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Expand All @@ -132,7 +132,7 @@ describe("migrate-env-var-dependencies", () => {
test: { env: ["$MY_ENV"], dependsOn: ["^build", "$SUPER_COOL"] },
},
});
const { test } = config?.pipeline ?? {};
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Expand All @@ -154,7 +154,7 @@ describe("migrate-env-var-dependencies", () => {
test: { env: ["$MY_ENV"], dependsOn: ["^build", "$MY_ENV"] },
},
});
const { test } = config?.pipeline ?? {};
const { test } = config.pipeline;
const pipeline = migratePipeline(test);
expect(pipeline).toHaveProperty("env");
expect(pipeline?.env).toMatchInlineSnapshot(`
Expand Down
1 change: 1 addition & 0 deletions packages/turbo-codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"semver": "^7.3.5",
"strip-ansi": "^6.0.1",
"tsconfig": "workspace:*",
"turbo-types": "workspace:*",
"tsup": "^5.10.3",
"typescript": "^4.5.5"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fs from "fs-extra";
import path from "path";
import { Flags, Pipeline, TurboConfig } from "../types";
import { Flags } from "../types";
import type { Schema, Pipeline } from "turbo-types";
import chalk from "chalk";
import { skip, ok, error } from "../logger";

export function hasLegacyEnvVarDependencies(config: TurboConfig) {
export function hasLegacyEnvVarDependencies(config: Schema) {
const dependsOn = [
config.globalDependencies,
Object.values(config.pipeline ?? []).flatMap(
Object.values(config.pipeline).flatMap(
(pipeline) => pipeline.dependsOn ?? []
),
].flat();
Expand Down Expand Up @@ -61,7 +62,7 @@ export function migratePipeline(pipeline: Pipeline) {
return migratedPipeline;
}

export function migrateGlobal(config: TurboConfig) {
export function migrateGlobal(config: Schema) {
const { deps: globalDependencies, env } = migrateDependencies({
env: config.env,
deps: config.globalDependencies,
Expand All @@ -81,19 +82,17 @@ export function migrateGlobal(config: TurboConfig) {
return migratedConfig;
}

export function migrateConfig(config: TurboConfig) {
export function migrateConfig(config: Schema) {
let migratedConfig = migrateGlobal(config);
if (config.pipeline) {
Object.keys(config.pipeline).forEach((pipelineKey) => {
if (migratedConfig.pipeline && config.pipeline?.[pipelineKey]) {
const pipeline = migratedConfig.pipeline?.[pipelineKey];
migratedConfig.pipeline[pipelineKey] = {
...pipeline,
...migratePipeline(pipeline),
};
}
});
}
Object.keys(config.pipeline).forEach((pipelineKey) => {
if (migratedConfig.pipeline && config.pipeline?.[pipelineKey]) {
const pipeline = migratedConfig.pipeline?.[pipelineKey];
migratedConfig.pipeline[pipelineKey] = {
...pipeline,
...migratePipeline(pipeline),
};
}
});
return migratedConfig;
}

Expand Down
15 changes: 0 additions & 15 deletions packages/turbo-codemod/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,3 @@ export interface Flags {
force: boolean;
print: boolean;
}

export interface Pipeline {
outputs?: Array<string>;
dependsOn?: Array<string>;
env?: Array<string>;
inputs?: Array<string>;
cache?: boolean;
}

export interface TurboConfig {
$schema?: string;
globalDependencies?: Array<string>;
env?: Array<string>;
pipeline?: Record<string, Pipeline>;
}
30 changes: 20 additions & 10 deletions packages/turbo-types/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ export interface Schema {
$schema?: string;

/**
* A list of globs and environment variables for implicit global hash dependencies.
* Environment variables should be prefixed with $ (e.g. $GITHUB_TOKEN).
* A list of globs for implicit global hash dependencies.
*
* Any other entry without this prefix, will be considered filesystem glob. The
* contents of these files will be included in the global hashing algorithm and affect
* the hashes of all tasks.
* The contents of these files will be included in the global hashing
* algorithm and affect the hashes of all tasks.
*
* This is useful for busting the cache based on .env files (not in Git), environment
* variables, or any root level file that impacts package tasks (but are not represented
* This is useful for busting the cache based on .env files (not in Git),
* or any root level file that impacts package tasks (but are not represented
* in the traditional dependency graph
*
* (e.g. a root tsconfig.json, jest.config.js, .eslintrc, etc.)).
Expand All @@ -21,6 +19,14 @@ export interface Schema {
*/
globalDependencies?: string[];

/**
* A list of environment variables, prefixed with $ (e.g. $GITHUB_TOKEN),
* for implicit global hash dependencies.
*
* @default []
*/
env?: string[];

/**
* An object representing the task dependency graph of your project. turbo interprets
* these conventions to properly schedule, execute, and cache the outputs of tasks in
Expand Down Expand Up @@ -57,13 +63,17 @@ export interface Pipeline {
* package level (e.g. "a package's test and lint commands depend on build being
* completed first").
*
* Prefixing an item in dependsOn with a $ tells turbo that this pipeline task depends
* the value of that environment variable.
*
* @default []
*/
dependsOn?: string[];

/**
* A list of environment variables, prefixed with $ (e.g. $GITHUB_TOKEN), that this task depends on.
*
* @default []
*/
env?: string[];

/**
* The set of glob patterns of a task's cacheable filesystem outputs.
*
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bf5fc0e

Please sign in to comment.