From 01575f8b3cd16308d13995d7589980099f62d359 Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 5 Feb 2020 12:59:01 -0500 Subject: [PATCH] feat(repo): add a command to test create-nx-workspace --- CONTRIBUTING.md | 2 + e2e/commands/README | 1 + e2e/commands/create-nx-workspace.test.ts | 151 ++++++++++++++++++ e2e/{ => commands}/create-playground.test.ts | 2 +- package.json | 7 +- packages/cli/package.json | 2 +- packages/create-nx-plugin/package.json | 2 +- packages/create-nx-workspace/package.json | 2 +- .../{build_for_test.sh => build-for-test.sh} | 0 scripts/{check_format.sh => check-format.sh} | 0 scripts/create-playground.sh | 11 ++ scripts/link.sh | 2 +- scripts/nx-release.js | 114 +++++++------ ...lar_runtime.sh => test-angular-runtime.sh} | 0 scripts/test-create-nx-workspace.sh | 4 + 15 files changed, 240 insertions(+), 60 deletions(-) create mode 100644 e2e/commands/README create mode 100644 e2e/commands/create-nx-workspace.test.ts rename e2e/{ => commands}/create-playground.test.ts (95%) rename scripts/{build_for_test.sh => build-for-test.sh} (100%) rename scripts/{check_format.sh => check-format.sh} (100%) create mode 100755 scripts/create-playground.sh rename scripts/{test_angular_runtime.sh => test-angular-runtime.sh} (100%) create mode 100755 scripts/test-create-nx-workspace.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 905d0ed825016..61ed86730db0b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,8 @@ yarn local-registry enable yarn local-registry disable ``` +You can also run `yarn test-create-nx-workspace 30.0.0`. It will set up local registry, publish the packages, create the workspace with Angular and React applications in it, and will run some basic checks against the workspace. + ### Running Unit Tests To make sure your changes do not break any unit tests, run the following: diff --git a/e2e/commands/README b/e2e/commands/README new file mode 100644 index 0000000000000..8bb6aaf1d368c --- /dev/null +++ b/e2e/commands/README @@ -0,0 +1 @@ +These aren't exactly e2e tests. We are just using e2e utilities to implement these two commands. diff --git a/e2e/commands/create-nx-workspace.test.ts b/e2e/commands/create-nx-workspace.test.ts new file mode 100644 index 0000000000000..7f1f233c3e202 --- /dev/null +++ b/e2e/commands/create-nx-workspace.test.ts @@ -0,0 +1,151 @@ +import { exec, execSync } from 'child_process'; +import { dirSync } from 'tmp'; +import { readdirSync, readFileSync, statSync, writeFileSync } from 'fs'; +import * as path from 'path'; + +describe('create-nx-workspace', () => { + afterEach(() => { + execSync(`yarn local-registry disable`); + }); + + it('creates a new project', async done => { + if (!process.env.PUBLISHED_VERSION) { + console.error(`Please provision the version you are publishing`); + process.exit(1); + } + + const tmpFolder = dirSync().name; + const workspaceDir = `${tmpFolder}/happyorg`; + + await startRegistry(); + await execCommand('Enabling local registry', `yarn local-registry enable`); + await execCommand( + 'Publishing packages', + `yarn nx-release ${process.env.PUBLISHED_VERSION} --local` + ); + + await execCommand( + `Create a workspace in "${workspaceDir}"`, + `npx create-nx-workspace@latest happyorg --preset=angular --appName=ngapp --style=css`, + tmpFolder + ); + await execCommand( + 'Add ngrx to the Angular app', + `ng g @nrwl/angular:ngrx state --module=apps/ngapp/src/app/app.module.ts --root`, + workspaceDir + ); + + await addReact(workspaceDir); + await execCommand( + `Generate a React app`, + `ng g @nrwl/react:app reactapp`, + workspaceDir + ); + await execCommand(`Building angular app`, `ng build ngapp`, workspaceDir); + await execCommand(`Building react app`, `ng build reactapp`, workspaceDir); + + const webpacks = allVersionsOf(workspaceDir, 'webpack'); + if (webpacks.length > 1) { + console.log(`more than one version of webpack: ${webpacks.join(', ')}`); + } + expect(webpacks.length).toEqual(1); + + // filtering out rxjs in the listr package. + const rxjs = allVersionsOf(workspaceDir, 'rxjs').filter( + value => value !== '5.5.12' + ); + if (rxjs.length > 1) { + console.log(`more than one version of rxjs: ${rxjs.join(', ')}`); + } + expect(rxjs.length).toEqual(1); + + console.log('The automatic tests have passed.'); + console.log( + `Go to "${workspaceDir}" to verify that the workspace works as expected` + ); + + done(); + }, 120000); +}); + +function wait() { + return new Promise(r => { + setTimeout(() => r(), 500); + }); +} + +function startRegistry() { + return new Promise((res, rej) => { + const server = exec('yarn local-registry start'); + server.stdout.on('data', d => { + if (d.toString().indexOf('http address') > -1) { + res(); + } + }); + + server.on('exit', s => { + if (s !== 0) { + rej(`Cannot start local registry`); + } + }); + }); +} + +function allVersionsOf(dir: string, packageToCheck: string) { + const r = packageJsonFilesInNodeModules(`${dir}/node_modules`) + .map(p => { + try { + const parsed = JSON.parse(readFileSync(p).toString()); + if (parsed.name == packageToCheck) { + return parsed.version; + } + return null; + } catch (e) { + return null; + } + }) + .filter(p => !!p); + return r.filter((value, index, self) => self.indexOf(value) === index); +} + +function addReact(workspaceDir: string) { + const packageJson = JSON.parse( + readFileSync(`${workspaceDir}/package.json`).toString() + ); + packageJson.dependencies[`@nrwl/react`] = process.env.PUBLISHED_VERSION; + writeFileSync( + `${workspaceDir}/package.json`, + JSON.stringify(packageJson, null, 2) + ); + execSync(`npm config set registry http://localhost:4873/ && npm install`, { + cwd: workspaceDir, + shell: 'bash' + }); +} + +async function execCommand(description: string, cmd: string, cwd?: string) { + console.log(description); + execSync(cmd, { + stdio: [0, 1, 2], + cwd + }); + await wait(); +} + +function packageJsonFilesInNodeModules(dirName: string): string[] { + let res = []; + try { + readdirSync(dirName).forEach(c => { + try { + const child = path.join(dirName, c); + const s = statSync(child); + if (child.endsWith('package.json')) { + res.push(child); + } else if (s.isDirectory()) { + res = [...res, ...packageJsonFilesInNodeModules(child)]; + } + } catch (e) {} + }); + } catch (e) {} + return res; +} diff --git a/e2e/create-playground.test.ts b/e2e/commands/create-playground.test.ts similarity index 95% rename from e2e/create-playground.test.ts rename to e2e/commands/create-playground.test.ts index 71a31a47c0766..76221e2cf631d 100644 --- a/e2e/create-playground.test.ts +++ b/e2e/commands/create-playground.test.ts @@ -1,4 +1,4 @@ -import { ensureProject, forEachCli, newProject, runCLI } from './utils'; +import { ensureProject, forEachCli, newProject, runCLI } from '../utils'; forEachCli(() => { describe('create playground', () => { diff --git a/package.json b/package.json index b3b3314b02a66..78d15ea8a6866 100644 --- a/package.json +++ b/package.json @@ -11,17 +11,18 @@ "checkcommit": "node ./scripts/commit-lint.js", "e2e": "./scripts/e2e.sh", "e2e-rerun": "./scripts/e2e-rerun.sh", - "create-playground": "./scripts/e2e.sh create-playground", + "create-playground": "./scripts/create-playground.sh", "update-playground": "./scripts/update-playground.sh", "e2e-ci1": "./scripts/e2e-ci1.sh", "e2e-ci2": "./scripts/e2e-ci2.sh", + "test-create-nx-workspace": "./scripts/test-create-nx-workspace.sh", "format": "./scripts/format.sh", "linknpm": "./scripts/link.sh", "nx-release": "./scripts/nx-release.js", "copy": "./scripts/copy.sh", "test": "yarn linknpm fast && ./scripts/test.sh", - "test:all": "yarn linknpm fast && ./scripts/test_angular_runtime.sh && ./scripts/test.sh", - "checkformat": "./scripts/check_format.sh", + "test:all": "yarn linknpm fast && scripts/test-angular-runtime.sh && ./scripts/test.sh", + "checkformat": "scripts/check-format.sh", "checkimports": "node ./scripts/check-imports.js", "checkversions": "ts-node ./scripts/check-versions.ts", "local-registry": "./scripts/local-registry.sh", diff --git a/packages/cli/package.json b/packages/cli/package.json index 0b635c095a9bc..14bb6fba678a8 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@nrwl/cli", - "version": "0.0.2", + "version": "0.0.1", "description": "", "repository": { "type": "git", diff --git a/packages/create-nx-plugin/package.json b/packages/create-nx-plugin/package.json index 4508200d42256..20ac3b5a37344 100644 --- a/packages/create-nx-plugin/package.json +++ b/packages/create-nx-plugin/package.json @@ -1,6 +1,6 @@ { "name": "create-nx-plugin", - "version": "0.0.2", + "version": "0.0.1", "description": "Extensible Dev Tools for Monorepos", "repository": { "type": "git", diff --git a/packages/create-nx-workspace/package.json b/packages/create-nx-workspace/package.json index a89ce4a959d01..d557f62067149 100644 --- a/packages/create-nx-workspace/package.json +++ b/packages/create-nx-workspace/package.json @@ -1,6 +1,6 @@ { "name": "create-nx-workspace", - "version": "0.0.2", + "version": "0.0.1", "description": "Extensible Dev Tools for Monorepos", "repository": { "type": "git", diff --git a/scripts/build_for_test.sh b/scripts/build-for-test.sh similarity index 100% rename from scripts/build_for_test.sh rename to scripts/build-for-test.sh diff --git a/scripts/check_format.sh b/scripts/check-format.sh similarity index 100% rename from scripts/check_format.sh rename to scripts/check-format.sh diff --git a/scripts/create-playground.sh b/scripts/create-playground.sh new file mode 100755 index 0000000000000..968b5a20a3f72 --- /dev/null +++ b/scripts/create-playground.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +./scripts/link.sh + +rm -rf tmp +mkdir -p tmp/angular +mkdir -p tmp/nx + +jest --maxWorkers=1 ./build/e2e/commands/create-playground.test.js + + diff --git a/scripts/link.sh b/scripts/link.sh index 265e590c4e0fc..a09ce68f5da1c 100755 --- a/scripts/link.sh +++ b/scripts/link.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash if [ "$1" = "fast" ]; then - ./scripts/build_for_test.sh + ./scripts/build-for-test.sh fi if [ "$1" != "fast" ]; then diff --git a/scripts/nx-release.js b/scripts/nx-release.js index 179b148c14e23..97e6585b9df46 100755 --- a/scripts/nx-release.js +++ b/scripts/nx-release.js @@ -132,6 +132,27 @@ childProcess.execSync(`find build/npm -maxdepth 1 -name "*.tgz" -delete`, { */ const DRY_RUN = !!parsedArgs['dry-run']; +const pkgFiles = [ + 'package.json', + 'build/npm/create-nx-workspace/package.json', + 'build/npm/create-nx-plugin/package.json', + 'build/npm/jest/package.json', + 'build/npm/cypress/package.json', + 'build/npm/storybook/package.json', + 'build/npm/angular/package.json', + 'build/npm/react/package.json', + 'build/npm/next/package.json', + 'build/npm/web/package.json', + 'build/npm/node/package.json', + 'build/npm/express/package.json', + 'build/npm/nest/package.json', + 'build/npm/workspace/package.json', + 'build/npm/cli/package.json', + 'build/npm/tao/package.json', + 'build/npm/eslint-plugin-nx/package.json', + 'build/npm/linter/package.json', + 'build/npm/nx-plugin/package.json' +]; /** * Set the static options for release-it */ @@ -147,27 +168,7 @@ const options = { * All the package.json files that will have their version updated * by release-it */ - pkgFiles: [ - 'package.json', - 'build/npm/create-nx-workspace/package.json', - 'build/npm/create-nx-plugin/package.json', - 'build/npm/jest/package.json', - 'build/npm/cypress/package.json', - 'build/npm/storybook/package.json', - 'build/npm/angular/package.json', - 'build/npm/react/package.json', - 'build/npm/next/package.json', - 'build/npm/web/package.json', - 'build/npm/node/package.json', - 'build/npm/express/package.json', - 'build/npm/nest/package.json', - 'build/npm/workspace/package.json', - 'build/npm/cli/package.json', - 'build/npm/tao/package.json', - 'build/npm/eslint-plugin-nx/package.json', - 'build/npm/linter/package.json', - 'build/npm/nx-plugin/package.json' - ], + pkgFiles: pkgFiles, increment: parsedVersion.version, requireUpstream: false, github: { @@ -191,37 +192,46 @@ const options = { requireCleanWorkingDir: false }; -releaseIt(options) - .then(output => { - if (DRY_RUN) { - console.warn('WARNING: In DRY_RUN mode - not running publishing script'); - process.exit(0); - return; - } +childProcess.execSync('rm -rf ./build/packages/bazel'); +childProcess.execSync('rm -rf ./build/npm/bazel'); - // if (parsedArgs.nobazel) { - childProcess.execSync('rm -rf ./build/packages/bazel'); - childProcess.execSync('rm -rf ./build/npm/bazel'); - // } - - /** - * We always use either "latest" or "next" (i.e. no separate tags for alpha, beta etc) - */ - const npmTag = parsedVersion.isPrerelease ? 'next' : 'latest'; - const npmPublishCommand = `./scripts/publish.sh ${ - output.version - } ${npmTag} ${parsedArgs.local ? '--local' : ''}`; - console.log('Executing publishing script for all packages:'); - console.log(`> ${npmPublishCommand}`); - console.log( - `Note: You will need to authenticate with your NPM credentials` - ); - childProcess.execSync(npmPublishCommand, { +if (parsedArgs.local) { + pkgFiles.forEach(p => { + const content = JSON.parse(fs.readFileSync(p).toString()); + content.version = parsedVersion.version; + fs.writeFileSync(p, JSON.stringify(content, null, 2)); + }); + childProcess.execSync( + `./scripts/publish.sh ${parsedVersion.version} latest --local`, + { stdio: [0, 1, 2] + } + ); + process.exit(0); +} else { + releaseIt(options) + .then(output => { + if (DRY_RUN) { + console.warn( + 'WARNING: In DRY_RUN mode - not running publishing script' + ); + process.exit(0); + return; + } + const npmTag = parsedVersion.isPrerelease ? 'next' : 'latest'; + const npmPublishCommand = `./scripts/publish.sh ${output.version} ${npmTag}`; + console.log('Executing publishing script for all packages:'); + console.log(`> ${npmPublishCommand}`); + console.log( + `Note: You will need to authenticate with your NPM credentials` + ); + childProcess.execSync(npmPublishCommand, { + stdio: [0, 1, 2] + }); + process.exit(0); + }) + .catch(err => { + console.error(err.message); + process.exit(1); }); - process.exit(0); - }) - .catch(err => { - console.error(err.message); - process.exit(1); - }); +} diff --git a/scripts/test_angular_runtime.sh b/scripts/test-angular-runtime.sh similarity index 100% rename from scripts/test_angular_runtime.sh rename to scripts/test-angular-runtime.sh diff --git a/scripts/test-create-nx-workspace.sh b/scripts/test-create-nx-workspace.sh new file mode 100755 index 0000000000000..1f08884879490 --- /dev/null +++ b/scripts/test-create-nx-workspace.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +./scripts/link.sh +PUBLISHED_VERSION=$1 jest --maxWorkers=1 ./build/e2e/commands/create-nx-workspace.test.js