Skip to content

feat(deploy): add custom-domain support for gh-pages deployment (#1781) #3392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions packages/angular-cli/commands/github-pages-deploy.run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { GithubPagesDeployOptions } from './github-pages-deploy';

const fsReadDir = <any>denodeify(fs.readdir);
const fsCopy = <any>denodeify(fse.copy);
const fsWriteFile = <any>denodeify(fse.writeFile);

export default function githubPagesDeployRun(options: GithubPagesDeployOptions, rawArgs: string[]) {
const ui = this.ui;
Expand Down Expand Up @@ -53,11 +54,15 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,

/**
* BaseHref tag setting logic:
* First, use --base-href flag value if provided.
* First, no value if --custom-domain is provided.
* Second, use --base-href flag value if provided.
* Else if --user-page is true, then keep baseHref default as declared in index.html.
* Otherwise auto-replace with `/${projectName}/`.
*/
const baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
let baseHref: String = null;
if (!options.customDomain) {
baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
}

const buildOptions = {
target: options.target,
Expand Down Expand Up @@ -85,6 +90,7 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
.then(cleanGhPagesBranch)
.then(copyFiles)
.then(createNotFoundPage)
.then(createCustomDomainFile)
.then(addAndCommit)
.then(returnStartingBranch)
.then(pushToGitRepo)
Expand Down Expand Up @@ -176,6 +182,15 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
return fsCopy(indexHtml, notFoundPage);
}

function createCustomDomainFile() {
if (!options.customDomain) {
return;
}

const cnameFile = path.join(root, 'CNAME');
return fsWriteFile(cnameFile, options.customDomain);
}

function addAndCommit() {
return execPromise('git add .', execOptions)
.then(() => execPromise(`git commit -m "${options.message}"`))
Expand Down Expand Up @@ -203,7 +218,14 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
function printProjectUrl() {
return getUsernameFromGitOrigin()
.then((userName) => {
let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
let url = '';

if (options.customDomain) {
url = `http://${options.customDomain}/`;
} else {
url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
}

ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
});
Expand Down
7 changes: 7 additions & 0 deletions packages/angular-cli/commands/github-pages-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface GithubPagesDeployOptions {
ghToken?: string;
ghUsername?: string;
baseHref?: string;
customDomain?: string;
}

const githubPagesDeployCommand = Command.extend({
Expand Down Expand Up @@ -62,6 +63,12 @@ const githubPagesDeployCommand = Command.extend({
type: String,
default: null,
aliases: ['bh']
}, {
name: 'custom-domain',
type: String,
default: null,
aliases: ['cd'],
description: 'Custom domain for Github Pages'
}],

run: function(options: GithubPagesDeployOptions, rawArgs: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-cli/utilities/completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ng_opts='b build completion doc e2e g generate get github-pages:deploy gh-pages:

build_opts='--aot --base-href --environment --i18n-file --i18n-format --locale --output-path --progress --sourcemap --suppress-sizes --target --vendor-chunk --verbose --watch --watcher -bh -dev -e -o -prod -sm -t -w'
generate_opts='class component directive enum module pipe route service c cl d e m p r s --help'
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page -bh -e -t'
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page --custom-domain -cd -bh -e -t'
help_opts='--json --verbose -v'
init_opts='--dry-run inline-style inline-template --link-cli --mobile --name --prefix --routing --skip-npm --source-dir --style --verbose -d -is -it -lc -n -p -sb -sd -sn -v'
new_opts='--directory --dry-run inline-style inline-template --link-cli --mobile --prefix --routing --skip-git --skip-npm --source-dir --style --verbose -d -dir -is -it -lc -p -sb -sd -sg -sn -v'
Expand Down