Created
November 14, 2017 12:55
-
-
Save ridem/8d211d13c3b893986fad383f57f12937 to your computer and use it in GitHub Desktop.
Langify theme refresh code after deploy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If the script doesn't work, uncomment lines 5 and 32-33 below, and login to shopify. After one whole successful script run, you can comment back these lines. | |
// This is made to be hooked to a deploy script | |
var nightmare = require('nightmare')({ | |
// show: true, | |
waitTimeout: 50000, | |
webPreferences: { | |
partition: 'persist: shopify' | |
} | |
}); | |
var themesDict = { | |
dev: "12345", | |
prod: "23456" | |
}; | |
// var excludeFromProcess = [".woff", ".png", ".svg", ".js", "icon-", ".scss", ".css"]; | |
var excludeFromProcess = []; | |
var themeId; | |
if (process.argv[2] && process.argv[2] === "prod") { | |
themeId = themesDict.prod; | |
} else { | |
themeId = themesDict.dev; | |
} | |
console.log("Processing theme with ID: " + themeId); | |
nightmare | |
.useragent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.30 Safari/537.36") | |
// .goto('https://yourshop.myshopify.com/admin/apps') | |
// .wait('body.page-api-permissions-index') | |
.goto('https://yourshop.myshopify.com/admin/api_permissions/YOURLANGIFYPERMISSIONID/redirect') | |
.wait('#sidebar-navigation') | |
.on('console', (log, msg) => { | |
if (typeof msg == "string" && msg.includes("Progress:")) { | |
console.log(msg) | |
} | |
}) | |
.goto('http://langify-app.com/settings/themes') | |
.wait('a.prepare-theme-link') | |
.evaluate((themeIdToProcess, excludeFromProcess) => { | |
$('.prepare-overlay-title').html('Preparing theme'); | |
$('#prepare-overlay').modal({backdrop: 'static'}) | |
$('#prepare-overlay').modal('show'); | |
var progressBar = $('#prepare-overlay-progress-bar'); | |
var usedUrl = "/ajax/prepare_themes/get_snippets/themeIds"; | |
usedUrl = usedUrl.replace('themeIds', themeIdToProcess); | |
var createPrepareThemesLoader = function(list, progressCallback, completeCallback) { | |
var counter = 0 | |
var ajaxCalls = [] | |
var t0 = performance.now(); | |
var startRequests = function() { | |
for (var i = 0; i < list.length; i++) { | |
var item = list[i]; | |
ajaxCalls.push($.ajax({ | |
type: item.type, | |
url: item.url, | |
contentType: "application/json; charset=utf-8", | |
data: item.data, | |
listIndex: i + 1 | |
}).then(function() { | |
counter++; | |
if(progressCallback && list.length > 0) { | |
progressCallback(counter / list.length, this.listIndex); | |
} | |
}, function(jqXHR, textStatus, errorThrown) { | |
console.log("Progress: " + textStatus + " " + errorThrown); | |
console.log("Progress: Retrying call n°" + this.listIndex); | |
$.ajax(this); | |
})) | |
} | |
$.when.apply(this, ajaxCalls).then(function() { | |
var t1 = performance.now(); | |
console.log("Progress: It took " + Math.round((t1-t0)/1000) + " seconds to process") | |
completeCallback(); | |
}); | |
} | |
return { | |
load: function() { | |
startRequests(); | |
} | |
}; | |
}; | |
$.ajax({ | |
type: "GET", | |
url: usedUrl, | |
success: function (response) { | |
var list = []; | |
var themes = response.data.themes; | |
for(var themeId in themes) { | |
console.log("Progress: Got the list of items: " + themes[themeId].snippets.length + " elements") | |
for(var i = 0; i < themes[themeId].snippets.length; i++) { | |
var usedUrl = "/ajax/prepare_themes/prepare_snippet"; | |
var exclude = false; | |
for (var j = 0; j < excludeFromProcess.length; j++) { | |
if (themes[themeId].snippets[i].key.indexOf(excludeFromProcess[j]) !== -1) { | |
exclude = true; | |
break; | |
} | |
} | |
if (exclude) { | |
console.log("Progress: Excluding " + themes[themeId].snippets[i].key) | |
continue; | |
} | |
list.push({ | |
type: 'POST', | |
url: usedUrl, | |
data: JSON.stringify({ | |
theme: themeId, | |
snippet_key: themes[themeId].snippets[i].key, | |
content_type: themes[themeId].snippets[i].content_type | |
}) | |
}); | |
} | |
} | |
list[list.length - 1].url = "/ajax/prepare_themes/prepare_snippet/1" | |
console.log("Progress: List filtered, " + list.length + " elements to process.") | |
createPrepareThemesLoader( | |
list, | |
function(progress, index) { | |
var usedProgress = Math.round(progress * 100); | |
progressBar.width(usedProgress + '%'); | |
progressBar.attr('aria-valuenow', usedProgress); | |
console.log("Progress: "+ usedProgress + "% (" + Math.round(progress * list.length) + "/" + list.length + ") - Item n°" + index) | |
}, | |
function() { | |
console.log("Progress: Prepared successfully!") | |
$(".content").append($("<div>", {class: "theme-prepared-success"})); | |
} | |
).load(); | |
} | |
}); | |
}, themeId, excludeFromProcess) | |
.wait('.theme-prepared-success') | |
.end() | |
.then() | |
.catch((error)=> { | |
console.log("Error: " + error) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment