Skip to content

Instantly share code, notes, and snippets.

@Leftyx
Created September 17, 2015 14:32
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 Leftyx/354ab934f7e44457ac54 to your computer and use it in GitHub Desktop.
Save Leftyx/354ab934f7e44457ac54 to your computer and use it in GitHub Desktop.
Ionic hooks: move fonts in the root\fonts
#!/usr/bin/env node
// Move fonts in lib/ionic/fonts to the root in fonts folder
// v1.0
// Since the lib folder will be deleted we need to move the fonts
// referenced in the css to the root.
// foldersWithFontsToMove = array of folders containing fonts files you want to move in the root.
var fs = require('fs');
var path = require('path');
var rootDir = process.argv[2];
var platformPath = path.join(rootDir, 'platforms');
var platform = process.env.CORDOVA_PLATFORMS;
var cliCommand = process.env.CORDOVA_CMDLINE;
// hook configuration
var isRelease = (cliCommand.indexOf('--release') > -1) || (cliCommand.indexOf('--runrelease') > -1);
// isRelease = true;
if (!isRelease) {
return;
}
switch (platform) {
case 'android':
platformPath = path.join(platformPath, platform, 'assets', 'www');
break;
case 'ios':
platformPath = path.join(platformPath, platform, 'www');
break;
default:
console.log('this hook only supports android and ios currently');
return;
}
console.log('************************************************************************************');
console.log('... Moving fonts to the root of the app ...');
console.log('************************************************************************************');
// Fonts folders you need to move to root fonts for the css file to reference.
var foldersWithFontsToMove = [
'lib/ionic/fonts'
];
var fontsFolder = path.join(platformPath, "fonts");
// Creating folder if it does not exist.
if (!fs.existsSync(fontsFolder)){
fs.mkdirSync(fontsFolder);
}
// Moving Fonts to the root
foldersWithFontsToMove.forEach(function(folder) {
processFilesInFolder(path.join(platformPath, folder));
});
function processFilesInFolder(dir) {
fs.readdir(dir, function (err, list) {
if (err) {
console.log('processFilesInFolder err: ' + err);
return;
}
list.forEach(function(file) {
file = path.join(dir, file);
// console.log('this is the file' + file);
fs.stat(file, function(err, stat) {
if (stat.isDirectory()) {
processFilesInFolder(file);
} else{
copyFileToRoot(file);
}
});
});
});
}
function copyFileToRoot(fileName)
{
var fileDestination = path.join(fontsFolder, path.basename(fileName));
console.log('copying file: ' + fileName + ' to root destination ' + fileDestination);
try {
fs.createReadStream(fileName)
.pipe(fs.createWriteStream(fileDestination));
} catch (err) {
console.log('copyFileToRoot => Error', err);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment