From 70f167d8a77261108a40479c35eed94635cd25c6 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Tue, 14 Dec 2021 15:24:00 -0500 Subject: [PATCH 1/9] Improve create-turbo --- create-turbo/src/index.ts | 74 ++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/create-turbo/src/index.ts b/create-turbo/src/index.ts index 12e1bf6329d7e..b10a819174efd 100644 --- a/create-turbo/src/index.ts +++ b/create-turbo/src/index.ts @@ -20,7 +20,9 @@ const help = ` If is not provided up front you will be prompted for it. - Flags: + Flags: + --use-npm Explicitly tell the CLI to bootstrap the app using npm. + --no-install Explicitly do not run the package mananger's install command --help, -h Show this help message --version, -v Show the version of this script `; @@ -45,8 +47,11 @@ run() async function run() { let { input, flags, showHelp, showVersion } = meow(help, { + booleanDefault: undefined, flags: { help: { type: "boolean", default: false, alias: "h" }, + useNpm: { type: "boolean", default: false }, + install: { type: "boolean", default: true }, version: { type: "boolean", default: false, alias: "v" }, }, }); @@ -80,33 +85,28 @@ async function run() { ); const isYarnInstalled = shouldUseYarn(); - let answers = await inquirer.prompt<{ - packageManager: "yarn" | "npm"; - install: boolean; - }>([ - { - name: "packageManager", - type: "list", - message: "Which package manager do you want to use?", - choices: [ - { - name: "Yarn", - value: "yarn", - disabled: !isYarnInstalled && "not installed", - }, - { name: "NPM", value: "npm" }, - // { name: "PNPM", value: "pnpm" }, - ], - }, - { - name: "install", - type: "confirm", - message: function (answers) { - return `Do you want me to run \`${answers.packageManager} install\`?`; + let answers; + if (flags.useNpm) { + answers = { packageManager: "npm " }; + } else { + answers = await inquirer.prompt<{ + packageManager: "yarn" | "npm"; + }>([ + { + name: "packageManager", + type: "list", + message: "Which package manager do you want to use?", + choices: [ + { + name: "Yarn", + value: "yarn", + disabled: !isYarnInstalled && "(not installed)", + }, + { name: "NPM", value: "npm" }, + ], }, - default: true, - }, - ]); + ]); + } // Create the app directory let relativeProjectDir = path.relative(process.cwd(), projectDir); @@ -169,7 +169,7 @@ async function run() { JSON.stringify(appPkg, null, 2) ); - if (answers.install) { + if (flags.install) { console.log(); console.log(`>>> Bootstrapping a new turborepo with the following:`); console.log(); @@ -199,6 +199,24 @@ async function run() { cwd: projectDir, }); spinner.stop(); + } else { + console.log(); + console.log(`>>> Bootstrapped a new turborepo with the following:`); + console.log(); + console.log(` - ${chalk.bold("apps/web")}: Next.js with TypeScript`); + console.log(` - ${chalk.bold("apps/docs")}: Next.js with TypeScript`); + console.log( + ` - ${chalk.bold("packages/ui")}: Shared React component library` + ); + console.log( + ` - ${chalk.bold("packages/config")}: Shared configuration (ESLint)` + ); + console.log( + ` - ${chalk.bold( + "packages/tsconfig" + )}: Shared TypeScript \`tsconfig.json\`` + ); + console.log(); } process.chdir(projectDir); From 3454125baa959e6024dc82fd22ae7700aba9fa2a Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Tue, 14 Dec 2021 15:54:26 -0500 Subject: [PATCH 2/9] Better install output --- create-turbo/src/index.ts | 58 ++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/create-turbo/src/index.ts b/create-turbo/src/index.ts index b10a819174efd..f281dcf95027a 100644 --- a/create-turbo/src/index.ts +++ b/create-turbo/src/index.ts @@ -87,7 +87,7 @@ async function run() { const isYarnInstalled = shouldUseYarn(); let answers; if (flags.useNpm) { - answers = { packageManager: "npm " }; + answers = { packageManager: "npm" }; } else { answers = await inquirer.prompt<{ packageManager: "yarn" | "npm"; @@ -221,46 +221,42 @@ async function run() { process.chdir(projectDir); tryGitInit(relativeProjectDir); - - console.log( - `${chalk.bold(turboGradient(">>> Success!"))} Your new Turborepo is ready. ` - ); - console.log(); - console.log(`To build all apps and packages, run the following:`); - console.log(); - if (!projectDirIsCurrentDir) { - console.log(` cd ${relativeProjectDir}`); + if (projectDirIsCurrentDir) { + console.log( + `${chalk.bold( + turboGradient(">>> Success!") + )} Your new Turborepo is ready. ` + ); + console.log("Inside this directory, you can run several commands:"); + } else { + console.log( + `${chalk.bold( + turboGradient(">>> Success!") + )} Created a new Turborepo at "${relativeProjectDir}". ` + ); + console.log("Inside that directory, you can run several commands:"); } - console.log(` ${answers.packageManager} run build`); + console.log(); - console.log(`To develop all apps and packages, run the following:`); + console.log(chalk.cyan(` ${answers.packageManager} run build`)); + console.log(` Build all apps and packages`); console.log(); - if (!projectDirIsCurrentDir) { - console.log(` cd ${relativeProjectDir}`); - } - console.log(` ${answers.packageManager} run dev`); + console.log(chalk.cyan(` ${answers.packageManager} run dev`)); + console.log(` Develop all apps and packages`); console.log(); console.log(`Turborepo will cache locally by default. For an additional`); console.log(`speed boost, enable Remote Caching (beta) with Vercel by`); - console.log(`entering the following commands:`); + console.log(`entering the following command:`); + console.log(); + console.log(chalk.cyan(` npx turbo login`)); + console.log(); + console.log(`We suggest that you begin by typing:`); console.log(); if (!projectDirIsCurrentDir) { - console.log(` cd ${relativeProjectDir}`); + console.log(` ${chalk.cyan("cd")} ${relativeProjectDir}`); } - console.log(` npx turbo login`); + console.log(chalk.cyan(` npx turbo login`)); console.log(); - if (projectDirIsCurrentDir) { - console.log(`For more info, checkout the README`); - } else { - console.log( - `For more info, checkout the README in ${chalk.bold(relativeProjectDir)}` - ); - } - console.log( - `as well as the official Turborepo docs ${chalk.underline( - "https://turborepo.org/docs" - )}` - ); } const update = checkForUpdate(cliPkgJson).catch(() => null); From e15c9f808b0d87921dd732fe1ac4127852807a44 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Tue, 14 Dec 2021 15:58:53 -0500 Subject: [PATCH 3/9] Update tests --- create-turbo/__tests__/cli.test.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/create-turbo/__tests__/cli.test.ts b/create-turbo/__tests__/cli.test.ts index 3c95a39f3e665..f3528357252d7 100644 --- a/create-turbo/__tests__/cli.test.ts +++ b/create-turbo/__tests__/cli.test.ts @@ -72,17 +72,6 @@ describe("create-turbo cli", () => { expect(getPromptChoices(prompt)).toEqual(["Yarn", "NPM"]); cli.stdin.write(keys.enter); break; - - case 4: - expect(prompt).toEqual( - "? Do you want me to run `yarn install`? (Y/n)" - ); - cli.stdin.write("n"); - - // At this point the CLI will create directories and all that fun stuff - // TODO: We should actually test this stuff too, kinda a big deal - cli.kill("SIGINT"); - break; } previousPrompt = prompt; @@ -126,7 +115,9 @@ describe("create-turbo cli", () => { If is not provided up front you will be prompted for it. - Flags: + Flags: + --use-npm Explicitly tell the CLI to bootstrap the app using npm. + --no-install Explicitly do not run the package mananger's install command --help, -h Show this help message --version, -v Show the version of this script @@ -147,7 +138,9 @@ describe("create-turbo cli", () => { If is not provided up front you will be prompted for it. - Flags: + Flags: + --use-npm Explicitly tell the CLI to bootstrap the app using npm. + --no-install Explicitly do not run the package mananger's install command --help, -h Show this help message --version, -v Show the version of this script From dac931a73ed69c0e1994fe8a77c08e725654e73b Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Wed, 15 Dec 2021 08:46:32 -0500 Subject: [PATCH 4/9] Fix tests --- create-turbo/__tests__/cli.test.ts | 2 +- create-turbo/src/shouldUseYarn.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/create-turbo/__tests__/cli.test.ts b/create-turbo/__tests__/cli.test.ts index f3528357252d7..e02a04737a7d9 100644 --- a/create-turbo/__tests__/cli.test.ts +++ b/create-turbo/__tests__/cli.test.ts @@ -37,7 +37,7 @@ describe("create-turbo cli", () => { }); it("guides the user through the process", (done) => { - let cli = spawn("node", [createTurbo], {}); + let cli = spawn("node", [createTurbo, "--no-install"], {}); let promptCount = 0; let previousPrompt: string; diff --git a/create-turbo/src/shouldUseYarn.ts b/create-turbo/src/shouldUseYarn.ts index 0153ae01e5548..69ef256ce1f48 100644 --- a/create-turbo/src/shouldUseYarn.ts +++ b/create-turbo/src/shouldUseYarn.ts @@ -3,8 +3,8 @@ import { execSync } from "child_process"; export function shouldUseYarn(): boolean { try { const userAgent = process.env.npm_config_user_agent; - if (userAgent) { - return Boolean(userAgent && userAgent.startsWith("yarn")); + if (userAgent && userAgent.startsWith("yarn")) { + return true; } execSync("yarnpkg --version", { stdio: "ignore" }); return true; From 559112b2ebd4cf841efbe38b2396b8d8882238ef Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Wed, 15 Dec 2021 09:42:49 -0500 Subject: [PATCH 5/9] Use a snapshot test --- .../__tests__/__snapshots__/cli.test.ts.snap | 29 +++++++++++++++++++ create-turbo/__tests__/cli.test.ts | 25 +++++++++++----- create-turbo/src/index.ts | 2 +- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 create-turbo/__tests__/__snapshots__/cli.test.ts.snap diff --git a/create-turbo/__tests__/__snapshots__/cli.test.ts.snap b/create-turbo/__tests__/__snapshots__/cli.test.ts.snap new file mode 100644 index 0000000000000..7c2cc0b8badd5 --- /dev/null +++ b/create-turbo/__tests__/__snapshots__/cli.test.ts.snap @@ -0,0 +1,29 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`create-turbo cli guides the user through the process 1`] = ` +">>> Bootstrapped a new turborepo with the following: + + - apps/web: Next.js with TypeScript +- apps/docs: Next.js with TypeScript + - packages/ui: Shared React component library + - packages/config: Shared configuration (ESLint) +- packages/tsconfig: Shared TypeScript \`tsconfig.json\` +>>> Success! Created a new Turborepo at \\"my-turborepo\\". +Inside that directory, you can run several commands: + + yarn run build + Build all apps and packages +yarn run dev + Develop all apps and packages + +Turborepo will cache locally by default. For an additional +speed boost, enable Remote Caching (beta) with Vercel by +entering the following command: + + npx turbo login + +We suggest that you begin by typing: + + cd my-turborepo + npx turbo login" +`; diff --git a/create-turbo/__tests__/cli.test.ts b/create-turbo/__tests__/cli.test.ts index e02a04737a7d9..2361dacbddaa5 100644 --- a/create-turbo/__tests__/cli.test.ts +++ b/create-turbo/__tests__/cli.test.ts @@ -30,22 +30,25 @@ describe("create-turbo cli", () => { `Cannot run Turbrepo CLI tests without building create-turbo` ); } + fs.rmdirSync(path.join(__dirname, "../my-turborepo"), { recursive: true }); }); afterAll(() => { jest.setTimeout(DEFAULT_JEST_TIMEOUT); + fs.rmdirSync(path.join(__dirname, "../my-turborepo"), { recursive: true }); }); it("guides the user through the process", (done) => { let cli = spawn("node", [createTurbo, "--no-install"], {}); let promptCount = 0; let previousPrompt: string; - + const messages: string[] = []; cli.stdout.on("data", async (data) => { let prompt = cleanPrompt(data); + if ( !prompt || - prompt === ">>> TURBOREPO" || + prompt.startsWith(">>> TURBOREPO") || isSamePrompt(prompt, previousPrompt) ) { return; @@ -72,6 +75,9 @@ describe("create-turbo cli", () => { expect(getPromptChoices(prompt)).toEqual(["Yarn", "NPM"]); cli.stdin.write(keys.enter); break; + default: + messages.push(prompt); + break; } previousPrompt = prompt; @@ -79,7 +85,7 @@ describe("create-turbo cli", () => { cli.on("exit", () => { try { - expect(promptCount).toEqual(4); + expect(messages.join("\n")).toMatchSnapshot(); done(); } catch (error) { done(error); @@ -175,9 +181,14 @@ function isSamePrompt( if (previousPrompt === undefined) { return false; } - let promptStart = previousPrompt.split("\n")[0]; - promptStart = promptStart.slice(0, promptStart.lastIndexOf("(")); - - return currentPrompt.startsWith(promptStart); + let nextPrompt = promptStart.slice(0, promptStart.lastIndexOf("(")); + // console.log(` + // "prev" ${previousPrompt.split("\n")[0]} + // "current" ${currentPrompt} + // "prefix" ${nextPrompt} + // "isSame" ${currentPrompt.startsWith(nextPrompt)} + // `); + + return currentPrompt.startsWith(nextPrompt); } diff --git a/create-turbo/src/index.ts b/create-turbo/src/index.ts index f281dcf95027a..374cdffbdba82 100644 --- a/create-turbo/src/index.ts +++ b/create-turbo/src/index.ts @@ -100,7 +100,7 @@ async function run() { { name: "Yarn", value: "yarn", - disabled: !isYarnInstalled && "(not installed)", + disabled: !isYarnInstalled && "not installed", }, { name: "NPM", value: "npm" }, ], From 2353e8967bd6accc854f4d64c223f565098f9080 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Wed, 15 Dec 2021 09:55:53 -0500 Subject: [PATCH 6/9] Fix tests --- .../__tests__/__snapshots__/cli.test.ts.snap | 29 ------------------- create-turbo/__tests__/cli.test.ts | 13 ++++++--- package.json | 5 ++++ 3 files changed, 14 insertions(+), 33 deletions(-) delete mode 100644 create-turbo/__tests__/__snapshots__/cli.test.ts.snap diff --git a/create-turbo/__tests__/__snapshots__/cli.test.ts.snap b/create-turbo/__tests__/__snapshots__/cli.test.ts.snap deleted file mode 100644 index 7c2cc0b8badd5..0000000000000 --- a/create-turbo/__tests__/__snapshots__/cli.test.ts.snap +++ /dev/null @@ -1,29 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`create-turbo cli guides the user through the process 1`] = ` -">>> Bootstrapped a new turborepo with the following: - - - apps/web: Next.js with TypeScript -- apps/docs: Next.js with TypeScript - - packages/ui: Shared React component library - - packages/config: Shared configuration (ESLint) -- packages/tsconfig: Shared TypeScript \`tsconfig.json\` ->>> Success! Created a new Turborepo at \\"my-turborepo\\". -Inside that directory, you can run several commands: - - yarn run build - Build all apps and packages -yarn run dev - Develop all apps and packages - -Turborepo will cache locally by default. For an additional -speed boost, enable Remote Caching (beta) with Vercel by -entering the following command: - - npx turbo login - -We suggest that you begin by typing: - - cd my-turborepo - npx turbo login" -`; diff --git a/create-turbo/__tests__/cli.test.ts b/create-turbo/__tests__/cli.test.ts index 2361dacbddaa5..3fbeaf54d046e 100644 --- a/create-turbo/__tests__/cli.test.ts +++ b/create-turbo/__tests__/cli.test.ts @@ -24,13 +24,13 @@ const DEFAULT_JEST_TIMEOUT = 5000; describe("create-turbo cli", () => { beforeAll(() => { jest.setTimeout(DEFAULT_JEST_TIMEOUT * 3); + fs.rmdirSync(path.join(__dirname, "../my-turborepo"), { recursive: true }); if (!fs.existsSync(createTurbo)) { // TODO: Consider running the build here instead of throwing throw new Error( `Cannot run Turbrepo CLI tests without building create-turbo` ); } - fs.rmdirSync(path.join(__dirname, "../my-turborepo"), { recursive: true }); }); afterAll(() => { @@ -75,8 +75,14 @@ describe("create-turbo cli", () => { expect(getPromptChoices(prompt)).toEqual(["Yarn", "NPM"]); cli.stdin.write(keys.enter); break; - default: - messages.push(prompt); + case 4: + // Bootstrap info + expect( + prompt.startsWith( + ">>> Bootstrapped a new turborepo with the following:" + ) + ).toBe(true); + break; } @@ -85,7 +91,6 @@ describe("create-turbo cli", () => { cli.on("exit", () => { try { - expect(messages.join("\n")).toMatchSnapshot(); done(); } catch (error) { done(error); diff --git a/package.json b/package.json index 2c048357ebf4d..cf78c546684f7 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,11 @@ "baseBranch": "origin/main", "pipeline": { "test": { + "outputs": [ + "coverage/**/*" + ] + }, + "create-turbo#test": { "dependsOn": [ "build" ], From 21d82c66056727f0a30fb84995a4646e73b07191 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Wed, 15 Dec 2021 10:04:48 -0500 Subject: [PATCH 7/9] Fix tests --- create-turbo/__tests__/cli.test.ts | 12 +++--------- package.json | 15 ++++++++------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/create-turbo/__tests__/cli.test.ts b/create-turbo/__tests__/cli.test.ts index 3fbeaf54d046e..6819c750b77d1 100644 --- a/create-turbo/__tests__/cli.test.ts +++ b/create-turbo/__tests__/cli.test.ts @@ -187,13 +187,7 @@ function isSamePrompt( return false; } let promptStart = previousPrompt.split("\n")[0]; - let nextPrompt = promptStart.slice(0, promptStart.lastIndexOf("(")); - // console.log(` - // "prev" ${previousPrompt.split("\n")[0]} - // "current" ${currentPrompt} - // "prefix" ${nextPrompt} - // "isSame" ${currentPrompt.startsWith(nextPrompt)} - // `); - - return currentPrompt.startsWith(nextPrompt); + promptStart = promptStart.slice(0, promptStart.lastIndexOf("(")); + + return currentPrompt.startsWith(promptStart); } diff --git a/package.json b/package.json index cf78c546684f7..9950daf2b5514 100644 --- a/package.json +++ b/package.json @@ -37,14 +37,9 @@ "test": { "outputs": [ "coverage/**/*" - ] - }, - "create-turbo#test": { - "dependsOn": [ - "build" ], - "outputs": [ - "coverage/**/*" + "dependsOn": [ + "^build" ] }, "lint": { @@ -68,6 +63,12 @@ "turbo.exe" ], "dependsOn": [] + }, + "create-turbo#test": { + "dependsOn": [ + "create-turbo#build" + ], + "outputs": [] } } } From e4f9c7ba0900270eafd7cff2dde4ed27a997f1d4 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Wed, 15 Dec 2021 10:11:23 -0500 Subject: [PATCH 8/9] Tweak gh actions --- .github/workflows/ci-go.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-go.yml b/.github/workflows/ci-go.yml index 0f5dd4dbf0fc1..56d25eb8a75af 100644 --- a/.github/workflows/ci-go.yml +++ b/.github/workflows/ci-go.yml @@ -2,7 +2,7 @@ name: CI on: push: - branches: ["*"] + branches: ["main"] pull_request: types: [opened, synchronize] @@ -30,10 +30,12 @@ jobs: - name: Setup Node.js environment uses: actions/setup-node@v2.5.0 with: - cache: yarn - cache-dependency-path: yarn.lock node-version: 14.x + - name: Conditionally install Yarn if local + if: ${{ env.ACT }} + run: npm i -g yarn + - name: Yarn Install run: yarn install From a0c1570667b4e83f92579367f8e3c830bfbbf59b Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Wed, 15 Dec 2021 10:11:46 -0500 Subject: [PATCH 9/9] Tweak gh actions push --- .github/workflows/ci-go.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci-go.yml b/.github/workflows/ci-go.yml index 56d25eb8a75af..b2dd7b3ad8b92 100644 --- a/.github/workflows/ci-go.yml +++ b/.github/workflows/ci-go.yml @@ -30,12 +30,10 @@ jobs: - name: Setup Node.js environment uses: actions/setup-node@v2.5.0 with: + cache: yarn + cache-dependency-path: yarn.lock node-version: 14.x - - name: Conditionally install Yarn if local - if: ${{ env.ACT }} - run: npm i -g yarn - - name: Yarn Install run: yarn install