Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Commit

Permalink
Basic support for a couple server configs. Replacing dest.write with …
Browse files Browse the repository at this point in the history
…writeFileFromString
  • Loading branch information
x1ddos committed Nov 12, 2014
1 parent 45b4885 commit a394231
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 26 deletions.
37 changes: 34 additions & 3 deletions app/hosting.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
'use strict';

function configFilename(provider) {
var request = require('request');


// TODO: support other providers
var SERVER_CONFIG = {

This comment has been minimized.

Copy link
@arthurvr

arthurvr Jun 10, 2015

Member

This seems like just copying the h5bp server configs. Why don't we just rely on composability and generator-server-configs? cc @sindresorhus

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Jun 10, 2015

Member

👍

// @SBoudrias

This comment has been minimized.

Copy link
@arthurvr

arthurvr Jun 11, 2015

Member

=> #84

gae: {
filename: 'app.yaml',
url: 'https://raw.githubusercontent.com/h5bp/server-configs-gae/master/app.yaml'
},
apache: {
filename: '.htaccess',
url: 'https://raw.githubusercontent.com/h5bp/server-configs-apache/master/dist/.htaccess'

This comment has been minimized.

Copy link
@arthurvr

arthurvr Jun 10, 2015

Member

Use the latest release, not master.

}
}

function fetchConfig(provider, callback) {

function config(provider) {
return SERVER_CONFIG[provider];
}

function isSupported(provider) {
return !!config(provider);
}

function fetchConfig(provider, callback) {
var cfg = config(provider);
if (!cfg) {
callback(new Error('unknown provider: ' + provider));
return;
}
request(cfg.url, function(err, res, body) {
if (!err && res.statusCode != 200) {
err = new Error('config fetch error ' + res.statusCode);
}
callback(err, cfg, body);
});
}

function deployTaskFilename(provider, tool) {

}

module.exports = {
configFilename: configFilename,
config: config,
isSupported: isSupported,
fetchConfig: fetchConfig,
deployTaskFilename: deployTaskFilename
}
58 changes: 35 additions & 23 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ var MobileGenerator = yeoman.generators.Base.extend({

writing: {
gulpfile: function () {
var filename = 'gulpfile.js',
gulpfile = this.dest.read(filename);
this.log.info('Configuring gulpfile.js');

var filepath = path.join(this.destinationRoot(), 'gulpfile.js'),
gulpfile = this.readFileAsString(filepath);

// pagespeed
if (this.prompts.siteUrl) {
Expand All @@ -78,11 +80,11 @@ var MobileGenerator = yeoman.generators.Base.extend({
}

// server-config
var srvfile = hosting.configFilename(this.prompts.hostingChoice);
if (srvfile) {
gulpfile.replace(/['"].*apache-server-configs.*['"]/m, "'" + srvfile + "'");
var cfg = hosting.config(this.prompts.hostingChoice);
if (cfg) {
gulpfile.replace(/['"].*apache-server-configs.*['"]/m, "'" + cfg.filename + "'");
} else {
gulpfile.replace(/^(.*apache-server-configs.*)$/m, '');
gulpfile.replace(/^.*apache-server-configs.*$/m, '');
}

// gulp deploy task
Expand All @@ -91,19 +93,30 @@ var MobileGenerator = yeoman.generators.Base.extend({
gulpfile += '\n\n' + this.engine(this.read(deployfile), this.prompts);
}

// TODO: replace with this.writeFileFromString
this.dest.delete(filename);
this.dest.write(filename, gulpfile);
this.writeFileFromString(gulpfile, filepath);
},

serverconfig: function() {
// TODO: fetch server config and store it in app/
// hosting.fetchConfig(this.prompts.hostingChoice, this.async());
if (!hosting.isSupported(this.prompts.hostingChoice))
return;

var done = this.async();
this.log.info('Fetching server config');
hosting.fetchConfig(this.prompts.hostingChoice, function(err, cfg, content) {
if (!err) {
this.dest.write(path.join('app', cfg.filename), content);
} else {
this.log.error(err);
}
done();
}.bind(this));
},

packagejson: function() {
var filename = 'package.json',
pkg = this.dest.readJSON(filename);
this.log.info('Configuring package.json');

var filepath = path.join(this.destinationRoot(), 'package.json'),
pkg = JSON.parse(this.readFileAsString(filepath));

pkg.name = this.prompts.siteName || 'replace me';
pkg.version = '0.0.0';
Expand All @@ -112,12 +125,11 @@ var MobileGenerator = yeoman.generators.Base.extend({
pkg.main = 'app/index.html';
delete pkg.devDependencies['apache-server-configs'];

this.dest.delete(filename);
this.dest.write(filename, JSON.stringify(pkg, null, 2));
this.writeFileFromString(JSON.stringify(pkg, null, 2), filepath);
},

webmanifest: function() {
this.log.write('Configuring manifest.webapp');
this.log.info('Configuring manifest.webapp');

var filepath = path.join(this.destinationRoot(), 'app', 'manifest.webapp'),
manifest = JSON.parse(this.readFileAsString(filepath));
Expand All @@ -133,19 +145,19 @@ var MobileGenerator = yeoman.generators.Base.extend({
},

layout: function () {
var basic = path.join('app', 'basic.html'),
index = path.join('app', 'index.html'),
this.log.info('Configuring layout and contents');

var basic = path.join(this.destinationRoot(), 'app', 'basic.html'),
index = path.join(this.destinationRoot(), 'app', 'index.html'),
content;

// Layout
if (this.prompts.layoutChoice === 'default') {
content = this.dest.read(index);
content = this.read(index);
} else if (this.prompts.layoutChoice === 'ie8') {
content = this.dest.read(basic);
content = this.read(basic);
}

this.dest.delete(basic);
this.dest.delete(index);

// Google Analytics
if (this.prompts.gaTrackId) {
Expand All @@ -162,7 +174,7 @@ var MobileGenerator = yeoman.generators.Base.extend({
content = content.replace(/(<meta\s+name=["']description["']\s+content=["']).*(["'])/, repl);
}

this.dest.write(index, content);
this.writeFileFromString(content, index);
}
},

Expand Down

0 comments on commit a394231

Please sign in to comment.