Last active
February 9, 2016 10:03
-
-
Save akofman/cd0cd017f959fcc323be to your computer and use it in GitHub Desktop.
Cordova hook to remove files in a plugin bundle after installing it
This file contains 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
#!/usr/bin/env node | |
/* | |
* Hook removing files you do not want to keep in your plugin bundle. | |
* These files have to be configured from the "filesToRemove" attribute of | |
* the package.json file of your plugin. | |
* | |
* Here is an example where demo is a folder and foo.txt a file : | |
* filesToRemove : ["demo", "foo.txt"] | |
*/ | |
var fs = require('fs'); | |
var path = require('path'); | |
var removeDir = function(dirPath) { | |
fs.readdirSync(dirPath).forEach(function(file){ | |
var pathToRemove = path.join(dirPath, file); | |
if(fs.lstatSync(pathToRemove).isDirectory()) { | |
removeDir(pathToRemove); | |
} else { | |
fs.unlinkSync(pathToRemove); | |
} | |
}); | |
fs.rmdirSync(dirPath); | |
}; | |
module.exports = function(context) { | |
var projectRoot = path.resolve(context.opts.projectRoot); | |
var pluginPath = path.join(projectRoot, 'plugins/', context.opts.plugin.id); | |
var pjson = require(path.join(pluginPath, 'package.json')); | |
pjson.filesToRemove.forEach(function(file){ | |
var pathToRemove = path.join(pluginPath, file); | |
if( fs.existsSync(pathToRemove) ) { | |
if(fs.lstatSync(pathToRemove).isDirectory()) { | |
removeDir(pathToRemove); | |
} | |
else { | |
fs.unlinkSync(pathToRemove); | |
} | |
} | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment