Skip to content

Commit

Permalink
feat(repo): add a command to test create-nx-workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Feb 6, 2020
1 parent c7d075d commit 01575f8
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions e2e/commands/README
@@ -0,0 +1 @@
These aren't exactly e2e tests. We are just using e2e utilities to implement these two commands.
151 changes: 151 additions & 0 deletions 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;
}
@@ -1,4 +1,4 @@
import { ensureProject, forEachCli, newProject, runCLI } from './utils';
import { ensureProject, forEachCli, newProject, runCLI } from '../utils';

forEachCli(() => {
describe('create playground', () => {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "@nrwl/cli",
"version": "0.0.2",
"version": "0.0.1",
"description": "",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions 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


2 changes: 1 addition & 1 deletion 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
Expand Down
114 changes: 62 additions & 52 deletions scripts/nx-release.js
Expand Up @@ -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
*/
Expand All @@ -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: {
Expand All @@ -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);
});
}
File renamed without changes.
4 changes: 4 additions & 0 deletions 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

0 comments on commit 01575f8

Please sign in to comment.