Skip to content
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

Don't destroy existing outputPath on fail #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 29 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var async = require("async")
var connect = require("connect")
var middleware = require("./middleware")
var mime = require('mime')
var tmp = require("os").tmpdir()


/**
Expand Down Expand Up @@ -160,17 +161,37 @@ exports.compile = function(projectPath, outputPath, callback){
outputPath = "www"
}

// harp will write the project out here first, and if successul, it'll get
// moved across to the real outputPath - to allow for compile to fail, without
// blowing away the previous version.
var tmpOutputPath = path.join(tmp, "www" + (Math.random() * 1000 | 0))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "www"... bit should probably just be a random hash: crypto.createHash('md5').update(Math.random() + '').digest('hex') as the pathname.

outputPath = path.resolve(projectPath, outputPath)

// if the compile was successful, move across the outputPath then call callback
var complete = function (err, config) {
if (!err) {
fs.copy(tmpOutputPath, outputPath, function () {
fs.remove(tmpOutputPath, function () {
callback(err, config)
})
})
} else {
// blow away the old directory
fs.remove(tmpOutputPath, function () {
callback(err, config)
})
}
}

/**
* Setup all the paths and collect all the data
*/

try{
outputPath = path.resolve(projectPath, outputPath)
var setup = helpers.setup(projectPath, "production")
var poly = polymer.root(setup.publicPath, setup.config.globals)
}catch(err){
return callback(err)
return complete(err)
}


Expand All @@ -180,7 +201,7 @@ exports.compile = function(projectPath, outputPath, callback){
*/

if(!helpers.willAllow(projectPath, outputPath)){
return callback({
return complete({
type: "Invalid Output Path",
message: "Output path cannot be greater then one level up from project path and must be in directory starting with `_` (underscore).",
projectPath: projectPath,
Expand All @@ -199,7 +220,7 @@ exports.compile = function(projectPath, outputPath, callback){
done(error)
}else{
if(body){
var dest = path.resolve(outputPath, polymer.helpers.outputPath(file))
var dest = path.resolve(tmpOutputPath, polymer.helpers.outputPath(file))
fs.mkdirp(path.dirname(dest), function(err){
fs.writeFile(dest, body, done)
})
Expand All @@ -220,7 +241,7 @@ exports.compile = function(projectPath, outputPath, callback){
var copyFile = function(file, done){
var ext = path.extname(file)
if(!polymer.helpers.shouldIgnore(file) && [".jade", ".ejs", ".md", ".styl", ".less", ".scss", ".coffee"].indexOf(ext) === -1){
var localPath = path.resolve(outputPath, file)
var localPath = path.resolve(tmpOutputPath, file)
fs.mkdirp(path.dirname(localPath), function(err){
fs.copy(path.resolve(setup.publicPath, file), localPath, done)
})
Expand All @@ -233,18 +254,18 @@ exports.compile = function(projectPath, outputPath, callback){
* Scan dir, Compile Less and Jade, Copy the others
*/

helpers.prime(outputPath, { ignore: projectPath }, function(err){
helpers.prime(tmpOutputPath, { ignore: projectPath }, function(err){
if(err) console.log(err)

helpers.ls(setup.publicPath, function(err, results){
async.eachLimit(results, 72, compileFile, function(err){
if(err){
callback(err)
complete(err)
}else{
async.eachLimit(results, 72, copyFile, function(err){
setup.config['harp_version'] = pkg.version
delete setup.config.globals
callback(null, setup.config)
complete(null, setup.config)
})
}
})
Expand Down