Skip to content

Instantly share code, notes, and snippets.

@joeylin
Last active August 29, 2015 13:56
Show Gist options
  • Save joeylin/8879911 to your computer and use it in GitHub Desktop.
Save joeylin/8879911 to your computer and use it in GitHub Desktop.
nodejs file copy
var fs = require('fs'),
url = require('url'),
when = require('when'),
errors = require('../errorHandling'),
path = require('path'),
paths = require('./paths'),
appRoot = paths().appRoot,
configExample = paths().configExample,
configFile = process.env.GHOST_CONFIG || paths().config,
rejectMessage = 'Unable to load config';
function writeConfigFile() {
var written = when.defer();
/* Check for config file and copy from config.example.js
if one doesn't exist. After that, start the server. */
fs.exists(configExample, function checkTemplate(templateExists) {
var read,
write;
if (!templateExists) {
return errors.logError(new Error('Could not locate a configuration file.'), appRoot, 'Please check your deployment for config.js or config.example.js.');
}
// Copy config.example.js => config.js
read = fs.createReadStream(configExample);
read.on('error', function (err) {
/*jslint unparam:true*/
return errors.logError(new Error('Could not open config.example.js for read.'), appRoot, 'Please check your deployment for config.js or config.example.js.');
});
read.on('end', written.resolve);
write = fs.createWriteStream(configFile);
write.on('error', function (err) {
/*jslint unparam:true*/
return errors.logError(new Error('Could not open config.js for write.'), appRoot, 'Please check your deployment for config.js or config.example.js.');
});
read.pipe(write);
});
return written.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment