Skip to content

Commit

Permalink
Fix all tests + handling of dotfiles, notfound, and nojekyll for Comm…
Browse files Browse the repository at this point in the history
…ander 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 <ferdinand@malcher.media>

* Update docs/README_standalone.md

Co-authored-by: Ferdinand Malcher <ferdinand@malcher.media>

* Update docs/README_standalone.md

Co-authored-by: Ferdinand Malcher <ferdinand@malcher.media>

---------

Co-authored-by: Ferdinand Malcher <ferdinand@malcher.media>
  • Loading branch information
JohannesHoppe and fmalcher authored Jan 27, 2024
1 parent 95d8603 commit cb7a1cb
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 166 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ This will install the NPM package and add the necessary `deploy` configuration t
ng add angular-cli-ghpages
```

The `deploy` config will be added for the specified `defaultProject`.
If there is no `defaultProject` set and there is only one project in your workspace, this project will be used.

If you have multiple projects in one workspace, you can manually define the project name:
If you have multiple projects in one workspace, you should manually define the project name:

```sh
ng add angular-cli-ghpages --project MYPROJECTNAME
Expand Down
39 changes: 12 additions & 27 deletions docs/README_standalone.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,29 @@ The command `npx` is also able to install `angular-cli-ghpages` on the first usa

## Usage

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

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

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

or
Usage:

```bash
ng build --prod --base-href "/REPOSITORY_NAME/"
npx angular-cli-ghpages [OPTIONS]
ng build --base-href "/REPOSITORY_NAME/"
npx angular-cli-ghpages --dir=dist/[PROJECTNAME]/browser
```

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

```bash
ng build --prod
npx angular-cli-ghpages [OPTIONS]
ng build
npx angular-cli-ghpages [OPTIONS] --dir=dist/[PROJECTNAME]/browser
```

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.

### Usage with Angular CLI 6 or higher

With Angular CLI 6 the build artifacts will be put in a subfolder under `dist`.
Please take a look at the `dist` folder to see whether there is a subfolder with your project's name or not.
If yes, you need to specify the deploy directory manually then when using this tool:

```bash
npx angular-cli-ghpages --dir=dist/[PROJECTNAME]
```

I most cases, the `[PROJECTNAME]` can be found in the `angular.json` file at `defaultProject`.

### Usage with Ionic

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:
Expand All @@ -76,7 +61,7 @@ In example, the following command runs [on our Travis-CI](https://travis-ci.org/
npx angular-cli-ghpages --repo=https://GH_TOKEN@github.com/<username>/<repositoryname>.git --name="Displayed Username" --email=mail@example.org
```

> You have to treat the GH_TOKEN as secure as a password!
> Don't share the GH_TOKEN with anyone! It is essentially a password to your GitHub account.
## Options
Expand Down
76 changes: 38 additions & 38 deletions src/__snapshots__/ng-add.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

exports[`ng-add generating files generates new files if starting from scratch 1`] = `
"{
\\"version\\": 1,
\\"projects\\": {
\\"THEPROJECT\\": {
\\"projectType\\": \\"application\\",
\\"root\\": \\"PROJECTROOT\\",
\\"architect\\": {
\\"build\\": {
\\"options\\": {
\\"outputPath\\": \\"dist/THEPROJECT\\"
"version": 1,
"projects": {
"THEPROJECT": {
"projectType": "application",
"root": "PROJECTROOT",
"architect": {
"build": {
"options": {
"outputPath": "dist/THEPROJECT"
}
},
\\"deploy\\": {
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
"deploy": {
"builder": "angular-cli-ghpages:deploy"
}
}
},
\\"OTHERPROJECT\\": {
\\"projectType\\": \\"application\\",
\\"root\\": \\"PROJECTROOT\\",
\\"architect\\": {
\\"build\\": {
\\"options\\": {
\\"outputPath\\": \\"dist/OTHERPROJECT\\"
"OTHERPROJECT": {
"projectType": "application",
"root": "PROJECTROOT",
"architect": {
"build": {
"options": {
"outputPath": "dist/OTHERPROJECT"
}
}
}
Expand All @@ -35,33 +35,33 @@ exports[`ng-add generating files generates new files if starting from scratch 1`

exports[`ng-add generating files overrides existing files 1`] = `
"{
\\"version\\": 1,
\\"projects\\": {
\\"THEPROJECT\\": {
\\"projectType\\": \\"application\\",
\\"root\\": \\"PROJECTROOT\\",
\\"architect\\": {
\\"build\\": {
\\"options\\": {
\\"outputPath\\": \\"dist/THEPROJECT\\"
"version": 1,
"projects": {
"THEPROJECT": {
"projectType": "application",
"root": "PROJECTROOT",
"architect": {
"build": {
"options": {
"outputPath": "dist/THEPROJECT"
}
},
\\"deploy\\": {
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
"deploy": {
"builder": "angular-cli-ghpages:deploy"
}
}
},
\\"OTHERPROJECT\\": {
\\"projectType\\": \\"application\\",
\\"root\\": \\"PROJECTROOT\\",
\\"architect\\": {
\\"build\\": {
\\"options\\": {
\\"outputPath\\": \\"dist/OTHERPROJECT\\"
"OTHERPROJECT": {
"projectType": "application",
"root": "PROJECTROOT",
"architect": {
"build": {
"options": {
"outputPath": "dist/OTHERPROJECT"
}
},
\\"deploy\\": {
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
"deploy": {
"builder": "angular-cli-ghpages:deploy"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/angular-cli-ghpages
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

// the standalone version
// the standalone version, pure js, no compilation necessary

var path = require('path'),
engine = require('./engine/engine'),
Expand Down
33 changes: 33 additions & 0 deletions src/angular-cli-ghpages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path';
import fs from 'fs';

import { execSync } from 'child_process';


function runCliWithArgs(args) {

const distFolder = path.resolve(__dirname, 'dist');

if (!fs.existsSync(distFolder)) {
throw new Error(`Dist directory ${distFolder} not found. Can't execute test! The directory must exist from the last build.`);
}
const program = path.resolve(__dirname, 'dist/angular-cli-ghpages');
return execSync(`node ${program} --dry-run ${args}`).toString();
}

describe('Commander CLI options', () => {

test('should set dotfiles, notfound, and nojekyll to `true` by default', () => {
const output = runCliWithArgs('');
expect(output).toContain(`"dotfiles": "files starting with dot ('.') will be included"`);
expect(output).toContain('"notfound": "a 404.html file will be created"');
expect(output).toContain('"nojekyll": "a .nojekyll file will be created"');
});

test('should set dotfiles, notfound, and nojekyll to `false` with no- flags', () => {
const output = runCliWithArgs('--no-dotfiles --no-notfound --no-nojekyll');
expect(output).toMatch(`"dotfiles": "files starting with dot ('.') will be ignored"`);
expect(output).toMatch('"notfound": "a 404.html file will NOT be created"');
expect(output).toMatch('"nojekyll": "a .nojekyll file will NOT be created"');
});
});
12 changes: 6 additions & 6 deletions src/deploy/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Target
} from '@angular-devkit/architect/src';
import { JsonObject, logging } from '@angular-devkit/core';
import { BuildTarget } from 'interfaces';
import { BuildTarget } from '../interfaces';

import deploy from './actions';

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

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

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

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

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

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

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

describe('error handling', () => {
Expand Down Expand Up @@ -100,7 +100,7 @@ const initMocks = () => {
},
currentDirectory: 'cwd',
id: 1,
logger: new logging.NullLogger() as any,
logger: new logging.NullLogger(),
workspaceRoot: 'cwd',
addTeardown: _ => {},
validateOptions: _ => Promise.resolve({} as any),
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import * as engine from '../engine/engine';
import deploy from './actions';
import { Schema } from './schema';
import { BuildTarget } from './interfaces';
import { BuildTarget } from '../interfaces';

// Call the createBuilder() function to create a builder. This mirrors
// createJobHandler() but add typings specific to Architect Builders.
Expand Down
4 changes: 0 additions & 4 deletions src/deploy/interfaces.ts

This file was deleted.

37 changes: 24 additions & 13 deletions src/engine/engine.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NullLogger } from '@angular-devkit/core/src/logger';
import { logging } from '@angular-devkit/core';

import * as engine from './engine';

describe('engine', () => {
describe('prepareOptions', () => {
const logger = new NullLogger();
const logger = new logging.NullLogger();

beforeEach(() => {
process.env = {};
Expand Down Expand Up @@ -83,20 +83,31 @@ describe('engine', () => {

expect(finalOptions.repo).toMatch(/angular-schule\/angular-cli-ghpages/);
});
});

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

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

const options = { git: 'xxx' };
expect(finalOptions.dotfiles).toBe(false);
expect(finalOptions.notfound).toBe(false);
expect(finalOptions.nojekyll).toBe(false);
});

try {
await engine.prepareOptions(options, logger);
} catch (e) {
expect(e).toBeTruthy();
}
})*/
it('should set dotfiles, notfound, and nojekyll to true when no- flags are not given', async () => {
const options = {};
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.dotfiles).toBe(true);
expect(finalOptions.notfound).toBe(true);
expect(finalOptions.nojekyll).toBe(true);
});
});
});
3 changes: 1 addition & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export interface WorkspaceProject {
}

export interface Workspace {
defaultProject?: string;
projects: Record<string, WorkspaceProject>;
}

export interface BuildTarget {
name: string;
options?: { [name: string]: any };
options?: Record<string, any>;
}
5 changes: 5 additions & 0 deletions src/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
};
Loading

0 comments on commit cb7a1cb

Please sign in to comment.