storybook/scripts/reset.js

63 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-06-06 00:59:10 +02:00
import fs from 'fs';
import { remove } from 'fs-extra';
2020-02-13 13:20:14 +01:00
import { spawn } from 'child_process';
2019-06-06 00:59:10 +02:00
import trash from 'trash';
2019-07-01 03:01:26 +03:00
const logger = console;
2019-06-06 00:59:10 +02:00
fs.writeFileSync('reset.log', '');
const cleaningProcess = spawn('git', [
'clean',
'-xdf',
'-n',
'--exclude="/.vscode"',
'--exclude="/.idea"',
2019-06-06 00:59:10 +02:00
]);
2020-03-27 20:04:50 +01:00
cleaningProcess.stdout.on('data', (data) => {
2019-06-06 00:59:10 +02:00
if (data && data.toString()) {
const l = data
.toString()
.split(/\n/)
2020-03-27 20:04:50 +01:00
.forEach((i) => {
2019-06-06 00:59:10 +02:00
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(/ts3\.4/) ||
2019-06-17 15:54:31 +08:00
uri.match(/\.cache/) ||
uri.match(/dll/)
) {
remove(uri).then(() => {
2019-07-01 03:01:26 +03:00
logger.log(`deleted ${uri}`);
});
} else {
2019-06-16 21:07:37 +02:00
trash(uri)
.then(() => {
2019-07-01 03:01:26 +03:00
logger.log(`trashed ${uri}`);
2019-06-16 21:07:37 +02:00
})
2020-03-27 20:04:50 +01:00
.catch((e) => {
2019-07-01 03:01:26 +03:00
logger.log('failed to trash, will try permanent delete');
remove(uri);
2019-06-16 21:07:37 +02:00
});
}
2019-06-06 00:59:10 +02:00
}
});
}
2020-03-27 20:04:50 +01:00
fs.appendFile('reset.log', data, (err) => {
2019-06-06 00:59:10 +02:00
if (err) {
throw err;
}
});
});
2020-03-27 20:04:50 +01:00
cleaningProcess.on('exit', (code) => {
2019-06-06 00:59:10 +02:00
if (code === 0) {
2019-07-01 03:01:26 +03:00
logger.log('all went well, files are being trashed now');
2019-06-06 00:59:10 +02:00
} else {
2019-07-01 03:01:26 +03:00
logger.error(code);
2019-06-06 00:59:10 +02:00
}
});