Skip to content

Instantly share code, notes, and snippets.

@ridem
Last active June 27, 2022 07:37
Show Gist options
  • Save ridem/531967e26f87779aa3b5e7ce0741d42a to your computer and use it in GitHub Desktop.
Save ridem/531967e26f87779aa3b5e7ce0741d42a to your computer and use it in GitHub Desktop.
Download a Shopify store's assets used in your theme

It's hard to migrate section themes that rely heavily on images. This bit of code helps you download all the CDN assets of your theme.

  1. Create a cdn_assets folder
  2. Create the download_assets.js file at the root of your project
  3. Edit the download_assets.js file to match the path to your settings_data.json (line 3)
  4. Edit the download_assets.js file to set the "CDN code" of your store. Each file that you upload from /admin/settings/files gets uploaded with the following format: https://cdn.shopify.com/s/files/1/YOUR_CDN_CODE/files/YOURFILE. The format of the code is /\d{4}\/\d{4}/ (four digits, a forward slash, and four digits)

To run the code, simply run: node download_assets.js mainstore.myshopify.com to download all the assets in your theme that are store on mainstore.myshopify.com

var request = require('request');
var fs = require('fs');
var settings = require('./src/config/settings_data.json'); // Set here the path to your "settings_data.json"
console.log("Downloading assets for: " + process.argv[2])
var themeDict = {
"mainstore.myshopify.com": "0912/5154",
"anotherstore.myshopify.com": "2659/8692"
};
var assets = new Set();
var assetRegex = /shopify:\/\/shop_images\/(\S+\.\w+)/;
function iterateSettings(obj) {
Object.keys(obj).forEach(function (key) {
if (typeof obj[key] === 'object') {
return iterateSettings(obj[key]);
}
if (typeof obj[key] === "string") {
var match = obj[key].match(assetRegex)
if (match) {
assets.add(match[1])
}
}
});
}
iterateSettings(settings);
assets.forEach(function (asset) {
var url = `https://cdn.shopify.com/s/files/1/${themeDict[process.argv[2]]}/files/${asset}`
request.get(url).pipe(fs.createWriteStream("./cdn_assets/" + asset))
});
@TheSuburbs
Copy link

TheSuburbs commented Feb 26, 2020

@germ777 the path is relative so if the 'settings_data.json' file is in the same folder a download_asset.js you can use the following line:

var settings = require('./settings_data.json');

I also made the following edit so the script only downloads new files which might help some people maintaining multiple regional stores:

`assets.forEach(function (asset) {
var url = 'https://cdn.shopify.com/s/files/1/${themeDict[process.argv[2]]}/files/${asset}'

try {
if (fs.existsSync("./cdn_assets/" + asset)) {
console.log("file already exists: " + asset)
} else {
console.log("downloading: " + asset)
request.get(url).pipe(fs.createWriteStream("./cdn_assets/" + asset))
}
} catch(err) {
console.error(err)
}
});`

@duongpro01
Copy link

thanks

@discostu001
Copy link

discostu001 commented Jun 27, 2022

Combined both here to pull assets from Shopify 2.0 themes, template & config directories with JSON files;

var request = require("request");
var fs = require("fs");
const glob = require("glob");

console.log("Downloading assets for: " + process.argv[2])

var themeDict = {
"#####.myshopify.com": "####/####/###"
};

var assets = new Set();

var assetRegex = /shopify://shop_images/(\S+.\w+)/;

function iterateSettings(obj) {
Object.keys(obj).forEach(function (key) {
if (typeof obj[key] === "object") {
return iterateSettings(obj[key]);
}
if (typeof obj[key] === "string") {
var match = obj[key].match(assetRegex)
if (match) {
if (!assets.has(match[1])) {
assets.add(match[1])
}
}
}
});
}

// get template assets
glob(__dirname + "/+(templates|config)/*.json", {}, (err, files) => {
files.forEach(e => {
var file = require(e);
iterateSettings(file);
})

assets.forEach(function (asset) {
try {
var url = https://cdn.shopify.com/s/files/1/${themeDict[process.argv[2]]}/files/${asset};

  if (fs.existsSync("./cdn_assets/" + asset)) {
    console.log("file already exists: " + asset)
  } else {
    console.log("downloading: " + asset)        
    request.get(url).pipe(fs.createWriteStream("./cdn_assets/" + asset))
  }
} catch (err) {
  console.error(err)
}

});

})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment