Skip to content

Commit cb7a1cb

Browse files
Fix all tests + handling of dotfiles, notfound, and nojekyll for Commander vs. Angular CLI (#180)
* fix jest / ts-jest setup * Looks like the angularfire team has now changed their setup like this: https://github.com/angular/angularfire/blob/master/package.json only dependencies and devDependencies, no peerDependencies any longer. And they skipped all previous version, too. So I think I will try this out too: * fixing snapshot file * remove support for deprecated defaultProject setting * fix all unit tests * tests for Commander CLI Options * tests for Angular CLI options * remove own consoleLogger * 2.0.0-beta.2 * Update docs/README_standalone.md Co-authored-by: Ferdinand Malcher <[email protected]> * Update docs/README_standalone.md Co-authored-by: Ferdinand Malcher <[email protected]> * Update docs/README_standalone.md Co-authored-by: Ferdinand Malcher <[email protected]> --------- Co-authored-by: Ferdinand Malcher <[email protected]>
1 parent 95d8603 commit cb7a1cb

15 files changed

+132
-166
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ This will install the NPM package and add the necessary `deploy` configuration t
158158
ng add angular-cli-ghpages
159159
```
160160

161-
The `deploy` config will be added for the specified `defaultProject`.
162-
If there is no `defaultProject` set and there is only one project in your workspace, this project will be used.
163-
164-
If you have multiple projects in one workspace, you can manually define the project name:
161+
If you have multiple projects in one workspace, you should manually define the project name:
165162

166163
```sh
167164
ng add angular-cli-ghpages --project MYPROJECTNAME

docs/README_standalone.md

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,29 @@ The command `npx` is also able to install `angular-cli-ghpages` on the first usa
1616

1717
## Usage
1818

19-
Execute `npx angular-cli-ghpages` in order to deploy the project with a build from `dist` folder.
20-
**Note: You have to create the `dist` folder first (e.g. by running `ng build --prod`).**
19+
Execute `npx angular-cli-ghpages` in order to deploy the project with a build from `dist` folder. (`dist` is the default)
20+
**Note: You have to create the `dist` folder first (e.g. by running `ng build`).**
2121

22-
Usage:
22+
Since Angular CLI 6 the build artifacts will be put in a subfolder, e.g. `dist/PROJECTNAME`.
23+
Since Angular CLI 17 the build artifacts will be put in a subfolder, followed by the folder `browser`, e.g. `dist/PROJECTNAME/browser`.
2324

24-
```bash
25-
ng build --prod --base-href "https://USERNAME.github.io/REPOSITORY_NAME/"
26-
npx angular-cli-ghpages [OPTIONS]
27-
```
25+
Please take a look at the `dist` folder to see whether there is a subfolder with your project's name or not.
26+
If yes, you need to specify the deploy directory manually then when using this tool:
2827

29-
or
28+
Usage:
3029

3130
```bash
32-
ng build --prod --base-href "/REPOSITORY_NAME/"
33-
npx angular-cli-ghpages [OPTIONS]
31+
ng build --base-href "/REPOSITORY_NAME/"
32+
npx angular-cli-ghpages --dir=dist/[PROJECTNAME]/browser
3433
```
3534

3635
or (`<base href="/">` stays untouched)
3736

3837
```bash
39-
ng build --prod
40-
npx angular-cli-ghpages [OPTIONS]
38+
ng build
39+
npx angular-cli-ghpages [OPTIONS] --dir=dist/[PROJECTNAME]/browser
4140
```
4241

43-
If you want to push to `gh-pages` on the same repository with your default credentials, then just enter `npx angular-cli-ghpages` without any options.
44-
45-
### Usage with Angular CLI 6 or higher
46-
47-
With Angular CLI 6 the build artifacts will be put in a subfolder under `dist`.
48-
Please take a look at the `dist` folder to see whether there is a subfolder with your project's name or not.
49-
If yes, you need to specify the deploy directory manually then when using this tool:
50-
51-
```bash
52-
npx angular-cli-ghpages --dir=dist/[PROJECTNAME]
53-
```
54-
55-
I most cases, the `[PROJECTNAME]` can be found in the `angular.json` file at `defaultProject`.
56-
5742
### Usage with Ionic
5843

5944
You can use the tool with Angular based Ionic projects, too. Instead of the ` dist` folder, the Ionic CLI will create a `www` folder you have to point the tool to. Just use the following commands:
@@ -76,7 +61,7 @@ In example, the following command runs [on our Travis-CI](https://travis-ci.org/
7661
npx angular-cli-ghpages --repo=https://[email protected]/<username>/<repositoryname>.git --name="Displayed Username" [email protected]
7762
```
7863

79-
> You have to treat the GH_TOKEN as secure as a password!
64+
> Don't share the GH_TOKEN with anyone! It is essentially a password to your GitHub account.
8065
8166
## Options
8267

src/__snapshots__/ng-add.spec.ts.snap

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
exports[`ng-add generating files generates new files if starting from scratch 1`] = `
44
"{
5-
\\"version\\": 1,
6-
\\"projects\\": {
7-
\\"THEPROJECT\\": {
8-
\\"projectType\\": \\"application\\",
9-
\\"root\\": \\"PROJECTROOT\\",
10-
\\"architect\\": {
11-
\\"build\\": {
12-
\\"options\\": {
13-
\\"outputPath\\": \\"dist/THEPROJECT\\"
5+
"version": 1,
6+
"projects": {
7+
"THEPROJECT": {
8+
"projectType": "application",
9+
"root": "PROJECTROOT",
10+
"architect": {
11+
"build": {
12+
"options": {
13+
"outputPath": "dist/THEPROJECT"
1414
}
1515
},
16-
\\"deploy\\": {
17-
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
16+
"deploy": {
17+
"builder": "angular-cli-ghpages:deploy"
1818
}
1919
}
2020
},
21-
\\"OTHERPROJECT\\": {
22-
\\"projectType\\": \\"application\\",
23-
\\"root\\": \\"PROJECTROOT\\",
24-
\\"architect\\": {
25-
\\"build\\": {
26-
\\"options\\": {
27-
\\"outputPath\\": \\"dist/OTHERPROJECT\\"
21+
"OTHERPROJECT": {
22+
"projectType": "application",
23+
"root": "PROJECTROOT",
24+
"architect": {
25+
"build": {
26+
"options": {
27+
"outputPath": "dist/OTHERPROJECT"
2828
}
2929
}
3030
}
@@ -35,33 +35,33 @@ exports[`ng-add generating files generates new files if starting from scratch 1`
3535

3636
exports[`ng-add generating files overrides existing files 1`] = `
3737
"{
38-
\\"version\\": 1,
39-
\\"projects\\": {
40-
\\"THEPROJECT\\": {
41-
\\"projectType\\": \\"application\\",
42-
\\"root\\": \\"PROJECTROOT\\",
43-
\\"architect\\": {
44-
\\"build\\": {
45-
\\"options\\": {
46-
\\"outputPath\\": \\"dist/THEPROJECT\\"
38+
"version": 1,
39+
"projects": {
40+
"THEPROJECT": {
41+
"projectType": "application",
42+
"root": "PROJECTROOT",
43+
"architect": {
44+
"build": {
45+
"options": {
46+
"outputPath": "dist/THEPROJECT"
4747
}
4848
},
49-
\\"deploy\\": {
50-
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
49+
"deploy": {
50+
"builder": "angular-cli-ghpages:deploy"
5151
}
5252
}
5353
},
54-
\\"OTHERPROJECT\\": {
55-
\\"projectType\\": \\"application\\",
56-
\\"root\\": \\"PROJECTROOT\\",
57-
\\"architect\\": {
58-
\\"build\\": {
59-
\\"options\\": {
60-
\\"outputPath\\": \\"dist/OTHERPROJECT\\"
54+
"OTHERPROJECT": {
55+
"projectType": "application",
56+
"root": "PROJECTROOT",
57+
"architect": {
58+
"build": {
59+
"options": {
60+
"outputPath": "dist/OTHERPROJECT"
6161
}
6262
},
63-
\\"deploy\\": {
64-
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
63+
"deploy": {
64+
"builder": "angular-cli-ghpages:deploy"
6565
}
6666
}
6767
}

src/angular-cli-ghpages

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
// the standalone version
3+
// the standalone version, pure js, no compilation necessary
44

55
var path = require('path'),
66
engine = require('./engine/engine'),

src/angular-cli-ghpages.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
4+
import { execSync } from 'child_process';
5+
6+
7+
function runCliWithArgs(args) {
8+
9+
const distFolder = path.resolve(__dirname, 'dist');
10+
11+
if (!fs.existsSync(distFolder)) {
12+
throw new Error(`Dist directory ${distFolder} not found. Can't execute test! The directory must exist from the last build.`);
13+
}
14+
const program = path.resolve(__dirname, 'dist/angular-cli-ghpages');
15+
return execSync(`node ${program} --dry-run ${args}`).toString();
16+
}
17+
18+
describe('Commander CLI options', () => {
19+
20+
test('should set dotfiles, notfound, and nojekyll to `true` by default', () => {
21+
const output = runCliWithArgs('');
22+
expect(output).toContain(`"dotfiles": "files starting with dot ('.') will be included"`);
23+
expect(output).toContain('"notfound": "a 404.html file will be created"');
24+
expect(output).toContain('"nojekyll": "a .nojekyll file will be created"');
25+
});
26+
27+
test('should set dotfiles, notfound, and nojekyll to `false` with no- flags', () => {
28+
const output = runCliWithArgs('--no-dotfiles --no-notfound --no-nojekyll');
29+
expect(output).toMatch(`"dotfiles": "files starting with dot ('.') will be ignored"`);
30+
expect(output).toMatch('"notfound": "a 404.html file will NOT be created"');
31+
expect(output).toMatch('"nojekyll": "a .nojekyll file will NOT be created"');
32+
});
33+
});

src/deploy/actions.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Target
77
} from '@angular-devkit/architect/src';
88
import { JsonObject, logging } from '@angular-devkit/core';
9-
import { BuildTarget } from 'interfaces';
9+
import { BuildTarget } from '../interfaces';
1010

1111
import deploy from './actions';
1212

@@ -22,7 +22,7 @@ describe('Deploy Angular apps', () => {
2222
beforeEach(() => initMocks());
2323

2424
it('should invoke the builder', async () => {
25-
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
25+
const spy = jest.spyOn(context, 'scheduleTarget');
2626
await deploy(mockEngine, context, BUILD_TARGET, {});
2727

2828
expect(spy).toHaveBeenCalledWith(
@@ -36,7 +36,7 @@ describe('Deploy Angular apps', () => {
3636
});
3737

3838
it('should invoke the builder with the baseHref', async () => {
39-
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
39+
const spy = jest.spyOn(context, 'scheduleTarget');
4040
await deploy(mockEngine, context, BUILD_TARGET, { baseHref: '/folder' });
4141

4242
expect(spy).toHaveBeenCalledWith(
@@ -50,10 +50,10 @@ describe('Deploy Angular apps', () => {
5050
});
5151

5252
it('should invoke engine.run', async () => {
53-
const spy = spyOn(mockEngine, 'run').and.callThrough();
53+
const spy = jest.spyOn(mockEngine, 'run');
5454
await deploy(mockEngine, context, BUILD_TARGET, {});
5555

56-
expect(spy).toHaveBeenCalledWith('dist/some-folder', {}, context.logger);
56+
expect(spy).toHaveBeenCalledWith('dist/some-folder/browser', {}, context.logger);
5757
});
5858

5959
describe('error handling', () => {
@@ -100,7 +100,7 @@ const initMocks = () => {
100100
},
101101
currentDirectory: 'cwd',
102102
id: 1,
103-
logger: new logging.NullLogger() as any,
103+
logger: new logging.NullLogger(),
104104
workspaceRoot: 'cwd',
105105
addTeardown: _ => {},
106106
validateOptions: _ => Promise.resolve({} as any),

src/deploy/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import * as engine from '../engine/engine';
88
import deploy from './actions';
99
import { Schema } from './schema';
10-
import { BuildTarget } from './interfaces';
10+
import { BuildTarget } from '../interfaces';
1111

1212
// Call the createBuilder() function to create a builder. This mirrors
1313
// createJobHandler() but add typings specific to Architect Builders.

src/deploy/interfaces.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/engine/engine.spec.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { NullLogger } from '@angular-devkit/core/src/logger';
1+
import { logging } from '@angular-devkit/core';
22

33
import * as engine from './engine';
44

55
describe('engine', () => {
66
describe('prepareOptions', () => {
7-
const logger = new NullLogger();
7+
const logger = new logging.NullLogger();
88

99
beforeEach(() => {
1010
process.env = {};
@@ -83,20 +83,31 @@ describe('engine', () => {
8383

8484
expect(finalOptions.repo).toMatch(/angular-schule\/angular-cli-ghpages/);
8585
});
86+
});
8687

87-
/*
88-
// i was not able to somehow catch an error... :-(
89-
it('should should throw an exception, if remote url could not be discovered', async () => {
88+
describe('prepareOptions - handling dotfiles, notfound, and nojekyll', () => {
89+
const logger = new logging.NullLogger();
9090

91-
expect.assertions(1);
91+
it('should set dotfiles, notfound, and nojekyll to false when no- flags are given', async () => {
92+
const options = {
93+
noDotfiles: true,
94+
noNotfound: true,
95+
noNojekyll: true
96+
};
97+
const finalOptions = await engine.prepareOptions(options, logger);
9298

93-
const options = { git: 'xxx' };
99+
expect(finalOptions.dotfiles).toBe(false);
100+
expect(finalOptions.notfound).toBe(false);
101+
expect(finalOptions.nojekyll).toBe(false);
102+
});
94103

95-
try {
96-
await engine.prepareOptions(options, logger);
97-
} catch (e) {
98-
expect(e).toBeTruthy();
99-
}
100-
})*/
104+
it('should set dotfiles, notfound, and nojekyll to true when no- flags are not given', async () => {
105+
const options = {};
106+
const finalOptions = await engine.prepareOptions(options, logger);
107+
108+
expect(finalOptions.dotfiles).toBe(true);
109+
expect(finalOptions.notfound).toBe(true);
110+
expect(finalOptions.nojekyll).toBe(true);
111+
});
101112
});
102113
});

src/interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ export interface WorkspaceProject {
1212
}
1313

1414
export interface Workspace {
15-
defaultProject?: string;
1615
projects: Record<string, WorkspaceProject>;
1716
}
1817

1918
export interface BuildTarget {
2019
name: string;
21-
options?: { [name: string]: any };
20+
options?: Record<string, any>;
2221
}

0 commit comments

Comments
 (0)