Skip to content

Instantly share code, notes, and snippets.

@billyct
Created October 4, 2016 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billyct/42b19dd913f2ffd87c509ffc5285531b to your computer and use it in GitHub Desktop.
Save billyct/42b19dd913f2ffd87c509ffc5285531b to your computer and use it in GitHub Desktop.
it work nice when you want change the image url in your css, and copy the image to the new path
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const filename = path.resolve(__dirname, 'index.css');
fs.readFile(filename, 'utf8', (err, data) => {
if (err) throw err;
var reg = new RegExp(/'([a-zA-z_0-9]+(?:.jpg|.png))'/, 'g');
//get all image url in css
var asset_urls = data.match(reg);
//replace with prepend string '/img/packs/$1'
data = data.replace(reg, '\'/img/packs/$1\'');
fs.writeFile(filename, data, (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
//do mv command
asset_urls = asset_urls.map(url => url.substring(1, url.length - 1));
asset_urls.forEach((url) => {
exec('mv ' + url + ' ./img/packs', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
// return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment