Skip to content

Instantly share code, notes, and snippets.

@jhildensperger
Last active September 14, 2017 12:11
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 jhildensperger/ccfd9894a6430c41870e76f57891154a to your computer and use it in GitHub Desktop.
Save jhildensperger/ccfd9894a6430c41870e76f57891154a to your computer and use it in GitHub Desktop.
Node js script to export provisioning profiles with UUID + Name for better readability
/**
* Export provisioning profiles with UUID + Name for better readability.
* Copies of the original profile with the new naming convention will be created in the current working directory
* usage: node profile_export.js -f path/to/profile.mobileprovision
* usage: node profile_export.js -d path/to/profiles/
*/
const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;
const type = process.argv[2];
if (type === '-f') {
const file = process.argv[3];
copyAndRenameProfile(file);
} else if (type === '-d') {
const dir = process.argv[3];
const fileNames = fs.readdirSync(dir);
copyAndRenameProfilesInDirectory(dir, fileNames, 0);
}
function copyAndRenameProfilesInDirectory(dir, fileNames, i) {
const fileName = fileNames[i];
if (!fileName) return;
const file = `${dir}/${fileName}`
const next = i+=1;
if (path.extname(file) === '.mobileprovision') {
copyAndRenameProfile(file, function() {
copyAndRenameProfilesInDirectory(dir, fileNames, next);
});
} else {
copyAndRenameProfilesInDirectory(dir, fileNames, next);
}
}
function sanitizeFileName(val) {
return val.replace(/[^a-z0-9-\.]/gi, '_').replace(/_{2,}/g, '_').replace(/_$/g, '');
}
function plistBuddy(path, cmd, callback) {
const child = exec(`/usr/libexec/PlistBuddy ${path} -c "${cmd}"`,
(error, stdout, stderr) => {
callback(error, stdout.trim());
});
}
function copyAndRenameProfile(file, callback) {
const basename = path.basename(file, '.mobileprovision');
console.log("Reading File: ", basename);
fs.readFile(file, 'utf-8', function read(err, data) {
if (err) return console.log(err);
var plist = data.match(/(<\?xml version="1\.0" encoding="UTF-8"\?>(?:.*\n)+<\/plist>)/g);
const tmpPlistPath = `/tmp/${basename}.plist`;
console.log("Saving plist to tmp path: ", tmpPlistPath);
fs.writeFile(tmpPlistPath, plist, function(err) {
if (err) return console.log(err);
console.log("Plist saved to tmp path: ", tmpPlistPath);
console.log("Reading profile name from plist at tmp path: ", tmpPlistPath);
plistBuddy(tmpPlistPath, 'Print Name', function(err, profileName) {
console.log("Profile name: ", profileName);
plistBuddy(tmpPlistPath, 'Print UUID', function(err, uuid) {
console.log("UUID : ", uuid);
const fileName = sanitizeFileName(`${uuid}.${profileName}`);
const path = `${process.cwd()}/${fileName}.mobileprovision`;
const cmd = `cp "${file}" "${path}"`;
console.log("Running copy command: ", cmd);
const child = exec(cmd,
(error, stdout, stderr) => {
callback(err);
});
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment