storybook/scripts/reset.js

61 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-06-06 00:59:10 +02:00
import fs from 'fs';
import { spawn, exec } from 'child_process';
import trash from 'trash';
import del from 'del';
2019-06-06 00:59:10 +02:00
fs.writeFileSync('reset.log', '');
// let results = [];
const cleaningProcess = spawn('git', [
'clean',
'-xdf',
'-n',
'--exclude=".vscode"',
'--exclude=".idea"',
]);
cleaningProcess.stdout.on('data', data => {
if (data && data.toString()) {
const l = data
.toString()
.split(/\n/)
.forEach(i => {
const [, uri] = i.match(/Would remove (.*)$/) || [];
if (uri) {
2019-06-17 15:54:31 +08:00
if (
uri.match(/node_modules/) ||
uri.match(/dist/) ||
uri.match(/\.cache/) ||
uri.match(/dll/)
) {
del(uri).then(() => {
2019-06-06 10:03:43 +02:00
console.log(`deleted ${uri}`);
});
} else {
2019-06-16 21:07:37 +02:00
trash(uri)
.then(() => {
console.log(`trashed ${uri}`);
})
.catch(e => {
console.log('failed to trash, will try permanent delete');
trash(uri);
});
}
2019-06-06 00:59:10 +02:00
}
});
}
fs.appendFile('reset.log', data, err => {
if (err) {
throw err;
}
});
});
cleaningProcess.on('exit', code => {
if (code === 0) {
console.log('all went well, files are being trashed now');
} else {
console.error(code);
}
});