Skip to content

Commit a54bc16

Browse files
austingreendevfilipesilva
authored andcommitted
feat(deploy): add custom-domain support for gh-pages deployment (#1781) (#3392)
Creates CNAME file if --custom-domain flag is provided. addresses (#1781)
1 parent 878ea09 commit a54bc16

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

packages/angular-cli/commands/github-pages-deploy.run.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { GithubPagesDeployOptions } from './github-pages-deploy';
1313

1414
const fsReadDir = <any>denodeify(fs.readdir);
1515
const fsCopy = <any>denodeify(fse.copy);
16+
const fsWriteFile = <any>denodeify(fse.writeFile);
1617

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

5455
/**
5556
* BaseHref tag setting logic:
56-
* First, use --base-href flag value if provided.
57+
* First, no value if --custom-domain is provided.
58+
* Second, use --base-href flag value if provided.
5759
* Else if --user-page is true, then keep baseHref default as declared in index.html.
5860
* Otherwise auto-replace with `/${projectName}/`.
5961
*/
60-
const baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
62+
let baseHref: String = null;
63+
if (!options.customDomain) {
64+
baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
65+
}
6166

6267
const buildOptions = {
6368
target: options.target,
@@ -85,6 +90,7 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
8590
.then(cleanGhPagesBranch)
8691
.then(copyFiles)
8792
.then(createNotFoundPage)
93+
.then(createCustomDomainFile)
8894
.then(addAndCommit)
8995
.then(returnStartingBranch)
9096
.then(pushToGitRepo)
@@ -176,6 +182,15 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
176182
return fsCopy(indexHtml, notFoundPage);
177183
}
178184

185+
function createCustomDomainFile() {
186+
if (!options.customDomain) {
187+
return;
188+
}
189+
190+
const cnameFile = path.join(root, 'CNAME');
191+
return fsWriteFile(cnameFile, options.customDomain);
192+
}
193+
179194
function addAndCommit() {
180195
return execPromise('git add .', execOptions)
181196
.then(() => execPromise(`git commit -m "${options.message}"`))
@@ -203,7 +218,14 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
203218
function printProjectUrl() {
204219
return getUsernameFromGitOrigin()
205220
.then((userName) => {
206-
let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
221+
let url = '';
222+
223+
if (options.customDomain) {
224+
url = `http://${options.customDomain}/`;
225+
} else {
226+
url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
227+
}
228+
207229
ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
208230
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
209231
});

packages/angular-cli/commands/github-pages-deploy.ts

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface GithubPagesDeployOptions {
1010
ghToken?: string;
1111
ghUsername?: string;
1212
baseHref?: string;
13+
customDomain?: string;
1314
}
1415

1516
const githubPagesDeployCommand = Command.extend({
@@ -62,6 +63,12 @@ const githubPagesDeployCommand = Command.extend({
6263
type: String,
6364
default: null,
6465
aliases: ['bh']
66+
}, {
67+
name: 'custom-domain',
68+
type: String,
69+
default: null,
70+
aliases: ['cd'],
71+
description: 'Custom domain for Github Pages'
6572
}],
6673

6774
run: function(options: GithubPagesDeployOptions, rawArgs: string[]) {

packages/angular-cli/utilities/completion.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ng_opts='b build completion doc e2e g generate get github-pages:deploy gh-pages:
1010

1111
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'
1212
generate_opts='class component directive enum module pipe route service c cl d e m p r s --help'
13-
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page -bh -e -t'
13+
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page --custom-domain -cd -bh -e -t'
1414
help_opts='--json --verbose -v'
1515
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'
1616
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'

0 commit comments

Comments
 (0)