Skip to content

Commit

Permalink
refactor(github-pages-deploy): reuse webpack build --base-href logic
Browse files Browse the repository at this point in the history
Reuse basehref handling logic from webpack build task
  • Loading branch information
dzonatan committed Aug 25, 2016
1 parent d494f64 commit c159363
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 44 deletions.
4 changes: 2 additions & 2 deletions addon/ng2/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Command from 'ember-cli/lib/models/command';
import * as WebpackBuild from '../tasks/build-webpack';
import * as WebpackBuildWatch from '../tasks/build-webpack-watch';

interface BuildOptions {
export interface BuildOptions {
target?: string;
environment?: string;
outputPath?: string;
Expand Down Expand Up @@ -34,7 +34,7 @@ module.exports = Command.extend({
}
if (commandOptions.target === 'production') {
commandOptions.environment = 'prod';
}
}
}

var project = this.project;
Expand Down
27 changes: 15 additions & 12 deletions addon/ng2/commands/github-pages-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,19 @@ module.exports = Command.extend({
outputPath: outDir
});

var buildOptions = {
/**
* BaseHref tag setting logic:
* First, 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}/`);

const buildOptions = {
target: options.target,
environment: options.environment,
outputPath: outDir,
baseHref: options.baseHref,
baseHref: baseHref,
};

var createGithubRepoTask = new CreateGithubRepo({
Expand All @@ -136,7 +144,7 @@ module.exports = Command.extend({
.then(createGitHubRepoIfNeeded)
.then(checkoutGhPages)
.then(copyFiles)
.then(updateBaseHref)
.then(createNotFoundPage)
.then(addAndCommit)
.then(returnStartingBranch)
.then(pushToGitRepo)
Expand Down Expand Up @@ -204,15 +212,10 @@ module.exports = Command.extend({
})));
}

function updateBaseHref() {
if (options.userPage) return Promise.resolve();
let indexHtml = path.join(root, 'index.html');
return fsReadFile(indexHtml, 'utf8')
.then((data) => data.replace(/<base href="\/">/g, `<base href="/${projectName}/">`))
.then((data) => {
fsWriteFile(indexHtml, data, 'utf8');
fsWriteFile(path.join(root, '404.html'), data, 'utf8');
});
function createNotFoundPage() {
const indexHtml = path.join(root, 'index.html');
const notFoundPage = path.join(root, '404.html');
return fsCopy(indexHtml, notFoundPage);
}

function addAndCommit() {
Expand Down
4 changes: 2 additions & 2 deletions addon/ng2/tasks/build-webpack-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import * as webpack from 'webpack';
import * as ProgressPlugin from 'webpack/lib/ProgressPlugin';
import { NgCliWebpackConfig } from '../models/webpack-config';
import { webpackOutputOptions } from '../models/';
import { ServeTaskOptions } from '../commands/serve';
import { BuildOptions } from '../commands/build';

let lastHash: any = null;

module.exports = Task.extend({
run: function(runTaskOptions: ServeTaskOptions) {
run: function(runTaskOptions: BuildOptions) {

const project = this.cliProject;

Expand Down
4 changes: 2 additions & 2 deletions addon/ng2/tasks/build-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as rimraf from 'rimraf';
import * as path from 'path';
import * as Task from 'ember-cli/lib/models/task';
import * as webpack from 'webpack';
import { ServeTaskOptions } from '../commands/serve';
import { BuildOptions } from '../commands/build';
import { NgCliWebpackConfig } from '../models/webpack-config';
import { webpackOutputOptions } from '../models/';

Expand All @@ -11,7 +11,7 @@ let lastHash: any = null;

module.exports = Task.extend({
// Options: String outputPath
run: function (runTaskOptions: ServeTaskOptions) {
run: function (runTaskOptions: BuildOptions) {

var project = this.cliProject;

Expand Down
30 changes: 4 additions & 26 deletions tests/acceptance/github-pages-deploy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var https = require('https');
var SilentError = require('silent-error');

const expect = chai.expect;
const fsReadFile = Promise.denodeify(fs.readFile);
const fsWriteFile = Promise.denodeify(fs.writeFile);
const fsMkdir = Promise.denodeify(fs.mkdir);

Expand Down Expand Up @@ -67,7 +66,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
});
});

it('should deploy with defaults to existing remote', function() {
it('should deploy with defaults to existing remote', function () {
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
Expand All @@ -78,12 +77,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);

return ng(['github-pages:deploy', '--skip-build'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
return ng(['github-pages:deploy', '--skip-build']);
});

it('should deploy with changed defaults', function() {
Expand All @@ -100,13 +94,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
.addExecSuccess(`git push origin ${ghPagesBranch}:${userPageBranch}`)
.addExecSuccess('git remote -v', remote);

return ng(['github-pages:deploy', '--skip-build', `--message=${message}`,
'--user-page'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search('<base href="/">')).to.not.equal(-1));
return ng(['github-pages:deploy', '--skip-build', `--message=${message}`, '--user-page']);
});

it('should create branch if needed', function() {
Expand All @@ -125,12 +113,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);

return ng(['github-pages:deploy', '--skip-build'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
return ng(['github-pages:deploy', '--skip-build']);
});

it('should create repo if needed', function() {
Expand Down Expand Up @@ -183,11 +166,6 @@ describe('Acceptance: ng github-pages:deploy', function() {

return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`,
`--gh-username=${username}`])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1))
.then(() => httpsStub.restore());
});

Expand Down

0 comments on commit c159363

Please sign in to comment.