Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b234e5e

Browse files
committedFeb 8, 2020
feat(repo): add a command to test create-nx-workspace
1 parent 2eeaae4 commit b234e5e

16 files changed

+963
-98
lines changed
 

‎CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ yarn local-registry enable
6161
yarn local-registry disable
6262
```
6363

64+
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.
65+
6466
### Running Unit Tests
6567

6668
To make sure your changes do not break any unit tests, run the following:

‎e2e/commands/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
These aren't exactly e2e tests. We are just using e2e utilities to implement these two commands.
+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import { exec, execSync } from 'child_process';
2+
import { dirSync } from 'tmp';
3+
import { readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
4+
import * as path from 'path';
5+
6+
describe('create-nx-workspace', () => {
7+
afterEach(() => {
8+
execSync(`yarn local-registry disable`);
9+
});
10+
11+
it('creates a new project', async done => {
12+
if (!process.env.PUBLISHED_VERSION) {
13+
console.error(`Please provision the version you are publishing`);
14+
process.exit(1);
15+
}
16+
17+
const tmpFolder = dirSync().name;
18+
const workspaceDir = `${tmpFolder}/happyorg`;
19+
20+
await startRegistry();
21+
await execCommand('Enabling local registry', `yarn local-registry enable`);
22+
await execCommand(
23+
'Publishing packages',
24+
`yarn nx-release ${process.env.PUBLISHED_VERSION} --local`
25+
);
26+
27+
await execCommand(
28+
`Create a workspace in "${workspaceDir}"`,
29+
`npx create-nx-workspace@latest happyorg --preset=angular --appName=ngapp --style=css`,
30+
tmpFolder
31+
);
32+
await execCommand(
33+
'Add ngrx to the Angular app',
34+
`ng g @nrwl/angular:ngrx state --module=apps/ngapp/src/app/app.module.ts --root`,
35+
workspaceDir
36+
);
37+
38+
await addReact(workspaceDir);
39+
await execCommand(
40+
`Generate a React app`,
41+
`ng g @nrwl/react:app reactapp`,
42+
workspaceDir
43+
);
44+
await execCommand(`Building angular app`, `ng build ngapp`, workspaceDir);
45+
await execCommand(`Building react app`, `ng build reactapp`, workspaceDir);
46+
47+
const webpacks = allVersionsOf(workspaceDir, 'webpack');
48+
if (webpacks.length > 1) {
49+
console.log(`more than one version of webpack: ${webpacks.join(', ')}`);
50+
}
51+
expect(webpacks.length).toEqual(1);
52+
53+
// filtering out rxjs in the listr package.
54+
const rxjs = allVersionsOf(workspaceDir, 'rxjs').filter(
55+
value => value !== '5.5.12'
56+
);
57+
if (rxjs.length > 1) {
58+
console.log(`more than one version of rxjs: ${rxjs.join(', ')}`);
59+
}
60+
expect(rxjs.length).toEqual(1);
61+
62+
console.log('The automatic tests have passed.');
63+
console.log(
64+
`Go to "${workspaceDir}" to verify that the workspace works as expected`
65+
);
66+
67+
done();
68+
}, 120000);
69+
});
70+
71+
function wait() {
72+
return new Promise(r => {
73+
setTimeout(() => r(), 500);
74+
});
75+
}
76+
77+
function startRegistry() {
78+
return new Promise((res, rej) => {
79+
const server = exec('yarn local-registry start');
80+
server.stdout.on('data', d => {
81+
if (d.toString().indexOf('http address') > -1) {
82+
res();
83+
}
84+
});
85+
86+
server.on('exit', s => {
87+
if (s !== 0) {
88+
rej(`Cannot start local registry`);
89+
}
90+
});
91+
});
92+
}
93+
94+
function allVersionsOf(dir: string, packageToCheck: string) {
95+
const r = packageJsonFilesInNodeModules(`${dir}/node_modules`)
96+
.map(p => {
97+
try {
98+
const parsed = JSON.parse(readFileSync(p).toString());
99+
if (parsed.name == packageToCheck) {
100+
return parsed.version;
101+
}
102+
return null;
103+
} catch (e) {
104+
return null;
105+
}
106+
})
107+
.filter(p => !!p);
108+
return r.filter((value, index, self) => self.indexOf(value) === index);
109+
}
110+
111+
function addReact(workspaceDir: string) {
112+
const packageJson = JSON.parse(
113+
readFileSync(`${workspaceDir}/package.json`).toString()
114+
);
115+
packageJson.dependencies[`@nrwl/react`] = process.env.PUBLISHED_VERSION;
116+
writeFileSync(
117+
`${workspaceDir}/package.json`,
118+
JSON.stringify(packageJson, null, 2)
119+
);
120+
execSync(`npm config set registry http://localhost:4873/ && npm install`, {
121+
cwd: workspaceDir,
122+
shell: 'bash'
123+
});
124+
}
125+
126+
async function execCommand(description: string, cmd: string, cwd?: string) {
127+
console.log(description);
128+
execSync(cmd, {
129+
stdio: [0, 1, 2],
130+
cwd
131+
});
132+
await wait();
133+
}
134+
135+
function packageJsonFilesInNodeModules(dirName: string): string[] {
136+
let res = [];
137+
try {
138+
readdirSync(dirName).forEach(c => {
139+
try {
140+
const child = path.join(dirName, c);
141+
const s = statSync(child);
142+
if (child.endsWith('package.json')) {
143+
res.push(child);
144+
} else if (s.isDirectory()) {
145+
res = [...res, ...packageJsonFilesInNodeModules(child)];
146+
}
147+
} catch (e) {}
148+
});
149+
} catch (e) {}
150+
return res;
151+
}

‎e2e/create-playground.test.ts renamed to ‎e2e/commands/create-playground.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ensureProject, forEachCli, newProject, runCLI } from './utils';
1+
import { ensureProject, forEachCli, newProject, runCLI } from '../utils';
22

33
forEachCli(() => {
44
describe('create playground', () => {

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"e2e": "./scripts/e2e.sh",
1313
"e2e-rerun": "./scripts/e2e-rerun.sh",
1414
"create-playground": "./scripts/e2e.sh create-playground",
15+
"test-create-nx-workspace": "./scripts/test-create-nx-workspace.sh",
1516
"e2e-ci1": "./scripts/e2e-ci1.sh",
1617
"e2e-ci2": "./scripts/e2e-ci2.sh",
1718
"format": "./scripts/format.sh",

‎packages/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nrwl/cli",
3-
"version": "0.0.2",
3+
"version": "0.0.1",
44
"description": "",
55
"repository": {
66
"type": "git",

‎packages/create-nx-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-nx-plugin",
3-
"version": "0.0.2",
3+
"version": "0.0.1",
44
"description": "Extensible Dev Tools for Monorepos",
55
"repository": {
66
"type": "git",

‎packages/create-nx-workspace/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-nx-workspace",
3-
"version": "0.0.2",
3+
"version": "0.0.1",
44
"description": "Extensible Dev Tools for Monorepos",
55
"repository": {
66
"type": "git",
File renamed without changes.
File renamed without changes.

‎scripts/create-playground.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
./scripts/link.sh
4+
5+
rm -rf tmp
6+
mkdir -p tmp/angular
7+
mkdir -p tmp/nx
8+
9+
jest --maxWorkers=1 ./build/e2e/commands/create-playground.test.js
10+
11+

‎scripts/link.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
if [ "$1" = "fast" ]; then
4-
./scripts/build_for_test.sh
4+
./scripts/build-for-test.sh
55
fi
66

77
if [ "$1" != "fast" ]; then

‎scripts/nx-release.js

+63-53
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ childProcess.execSync(`find build/npm -maxdepth 1 -name "*.tgz" -delete`, {
132132
*/
133133
const DRY_RUN = !!parsedArgs['dry-run'];
134134

135+
const pkgFiles = [
136+
'package.json',
137+
'build/npm/create-nx-workspace/package.json',
138+
'build/npm/create-nx-plugin/package.json',
139+
'build/npm/schematics/package.json',
140+
'build/npm/jest/package.json',
141+
'build/npm/cypress/package.json',
142+
'build/npm/storybook/package.json',
143+
'build/npm/angular/package.json',
144+
'build/npm/react/package.json',
145+
'build/npm/next/package.json',
146+
'build/npm/web/package.json',
147+
'build/npm/node/package.json',
148+
'build/npm/express/package.json',
149+
'build/npm/nest/package.json',
150+
'build/npm/workspace/package.json',
151+
'build/npm/cli/package.json',
152+
'build/npm/tao/package.json',
153+
'build/npm/eslint-plugin-nx/package.json',
154+
'build/npm/linter/package.json',
155+
'build/npm/nx-plugin/package.json'
156+
];
135157
/**
136158
* Set the static options for release-it
137159
*/
@@ -147,28 +169,7 @@ const options = {
147169
* All the package.json files that will have their version updated
148170
* by release-it
149171
*/
150-
pkgFiles: [
151-
'package.json',
152-
'build/npm/schematics/package.json',
153-
'build/npm/create-nx-workspace/package.json',
154-
'build/npm/create-nx-plugin/package.json',
155-
'build/npm/jest/package.json',
156-
'build/npm/cypress/package.json',
157-
'build/npm/storybook/package.json',
158-
'build/npm/angular/package.json',
159-
'build/npm/react/package.json',
160-
'build/npm/next/package.json',
161-
'build/npm/web/package.json',
162-
'build/npm/node/package.json',
163-
'build/npm/express/package.json',
164-
'build/npm/nest/package.json',
165-
'build/npm/workspace/package.json',
166-
'build/npm/cli/package.json',
167-
'build/npm/tao/package.json',
168-
'build/npm/eslint-plugin-nx/package.json',
169-
'build/npm/linter/package.json',
170-
'build/npm/nx-plugin/package.json'
171-
],
172+
pkgFiles: pkgFiles,
172173
increment: parsedVersion.version,
173174
requireUpstream: false,
174175
github: {
@@ -192,37 +193,46 @@ const options = {
192193
requireCleanWorkingDir: false
193194
};
194195

195-
releaseIt(options)
196-
.then(output => {
197-
if (DRY_RUN) {
198-
console.warn('WARNING: In DRY_RUN mode - not running publishing script');
199-
process.exit(0);
200-
return;
201-
}
196+
childProcess.execSync('rm -rf ./build/packages/bazel');
197+
childProcess.execSync('rm -rf ./build/npm/bazel');
202198

203-
// if (parsedArgs.nobazel) {
204-
childProcess.execSync('rm -rf ./build/packages/bazel');
205-
childProcess.execSync('rm -rf ./build/npm/bazel');
206-
// }
207-
208-
/**
209-
* We always use either "latest" or "next" (i.e. no separate tags for alpha, beta etc)
210-
*/
211-
const npmTag = parsedVersion.isPrerelease ? 'next' : 'latest';
212-
const npmPublishCommand = `./scripts/publish.sh ${
213-
output.version
214-
} ${npmTag} ${parsedArgs.local ? '--local' : ''}`;
215-
console.log('Executing publishing script for all packages:');
216-
console.log(`> ${npmPublishCommand}`);
217-
console.log(
218-
`Note: You will need to authenticate with your NPM credentials`
219-
);
220-
childProcess.execSync(npmPublishCommand, {
199+
if (parsedArgs.local) {
200+
pkgFiles.forEach(p => {
201+
const content = JSON.parse(fs.readFileSync(p).toString());
202+
content.version = parsedVersion.version;
203+
fs.writeFileSync(p, JSON.stringify(content, null, 2));
204+
});
205+
childProcess.execSync(
206+
`./scripts/publish.sh ${parsedVersion.version} latest --local`,
207+
{
221208
stdio: [0, 1, 2]
209+
}
210+
);
211+
process.exit(0);
212+
} else {
213+
releaseIt(options)
214+
.then(output => {
215+
if (DRY_RUN) {
216+
console.warn(
217+
'WARNING: In DRY_RUN mode - not running publishing script'
218+
);
219+
process.exit(0);
220+
return;
221+
}
222+
const npmTag = parsedVersion.isPrerelease ? 'next' : 'latest';
223+
const npmPublishCommand = `./scripts/publish.sh ${output.version} ${npmTag}`;
224+
console.log('Executing publishing script for all packages:');
225+
console.log(`> ${npmPublishCommand}`);
226+
console.log(
227+
`Note: You will need to authenticate with your NPM credentials`
228+
);
229+
childProcess.execSync(npmPublishCommand, {
230+
stdio: [0, 1, 2]
231+
});
232+
process.exit(0);
233+
})
234+
.catch(err => {
235+
console.error(err.message);
236+
process.exit(1);
222237
});
223-
process.exit(0);
224-
})
225-
.catch(err => {
226-
console.error(err.message);
227-
process.exit(1);
228-
});
238+
}
File renamed without changes.

‎scripts/test-create-nx-workspace.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
./scripts/link.sh
4+
PUBLISHED_VERSION=$1 jest --maxWorkers=1 ./build/e2e/commands/create-nx-workspace.test.js

0 commit comments

Comments
 (0)
Please sign in to comment.