forked from bower/bower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.js
52 lines (44 loc) · 1.65 KB
/
cleanup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var spawn = require('child_process').spawn;
var path = require('path');
var fs = require('fs');
var rimraf = require('rimraf');
var _ = require('lodash');
// Get the previous bower version
var version = '';
var cp = spawn('bower', ['-v']);
cp
.stdout.on('data', function (data) {
version += data;
})
.on('close', function (code) {
version = version.replace(/\n$/, '');
if (code || !version) return;
// The cache folders changed, so we cleanup the old cache structure
var temp = process.env.TMPDIR
|| process.env.TMP
|| process.env.TEMP
|| process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp';
var home = (process.platform === 'win32'
? process.env.USERPROFILE
: process.env.HOME) || temp;
var roaming = process.platform === 'win32'
? path.resolve(process.env.APPDATA || home || temp)
: path.resolve(home || temp);
// If on windows, delete the old bower-cache folder
if (process.platform === 'win32') {
return rimraf(path.join(roaming, 'bower-cache'), function (err) {
if (err) console.log('Error deleting the deprecated bower-cache folder');
});
}
// If on linux / mac delete the contents inside the .bower folder, except the cache and links, need to be removed
var folder = path.join(roaming, '.bower');
fs.readdir(folder, function (err, files) {
if (err) return;
files = _.without(files, 'cache', 'links');
files.forEach(function (file) {
rimraf(path.join(folder, file), function (err) {
if (err) console.log('Error deleting the deprecated .bower contents folder');
});
});
});
});