Cleaner for Xcode

项目地址 https://github.com/waylybaye/XcodeCleaner

Cleaner for Xcode_第1张图片
屏幕快照 2017-11-15 09.45.38.png

其实这是一个RN的项目 , 并不是原生的

其实还挺好用的,很方便,节省了很多空间

技多不压身

把JS代码贴出来学习一下

/**
 * Xcode Cleaner by waylybaye
 * https://github.com/waylybaye/XcodeCleaner
 * @flow
 */

import React, { Component } from 'react';

import { 
  AlertIOS,
  AppRegistry, 
  Dimensions,
  FlatList,
  StyleSheet, 
  Text, 
  View, 
  TouchableOpacity, 
  ProgressViewIOS,
  Button,
  Image,
  NativeModules,
  ActivityIndicator,
  LayoutAnimation,
} from 'react-native';


const {FileManager} = NativeModules;

function removePrefix(fullpath, path){
  let endingSlash = path[path.length - 1] === '/';
  return fullpath.substr(endingSlash ? path.length : path.length + 1);
}


function humanize(value, decimal) {
  value = value || 0;
  let i = -1;
  let byteUnits = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  do {
    value = value / 1024;
    i++;
  } while (value > 1024);

  return value.toFixed(decimal || 0) + byteUnits[i][0];
}

const STRINGS = {
  'xcode_not_found_title': 'Xcode not found',
  'xcode_not_found_body': "No Xcode installation found in selected directory, it's usually at HOME/Library/Developer."
};


export default class XcodeCleaner extends Component {
  constructor(props){
    super(props);

    this.state = {
      data: {},
      progress: {},
      tab: '',
    };
  }

  componentWillMount(){
    LayoutAnimation.easeInEaseOut();
  }

  async componentDidMount(){
    this.calculateXcode();
  }

  updateProgress(section, current, total){
    this.setState({
      progress: {
        ...this.state.progress,
        [section]: [current, total]
      },
    })
  }

  async calculateSubDirectory(path, progressKey, labelGetter) {
    console.log('calculate Path', path);
    let folders = [];
    try{
      folders = await FileManager.listDirectory(path, true);
    } catch (e){
      this.updateProgress(progressKey, 0, 0);
      this.setState({
        data: {
          ...this.state.data,
          [progressKey]: {
            size: 0,
            groups: [],
          }
        }
      });
      return;
    }

    let totalSize = 0;
    let groups = [];
    this.updateProgress(progressKey, 0, folders.length);

    for(let i=0; i console.log('Cancel Pressed'), style: 'cancel'},
          {text: 'Choose Directory ...', onPress: () => this.calculateXcode(true)},
        ],
      );

      return;
    }

    await this.calculateSubDirectory(xcode + 'iOS DeviceSupport/', 'deviceSupport');
    await this.calculateSubDirectory(xcode + 'DerivedData/', 'derivedData');
    await this.calculateSubDirectory(xcode + 'Archives/', 'archives');
    await this.calculateSubDirectory(authorizedPath + 'CoreSimulator/Devices/', 'simulator');
  }

  async componentWillUnmount(){
    let home = await FileManager.getHomeDirectory();
    let developer = `${home}/Library/Developer/`;
    await FileManager.stopAuthorization(developer);
  }

  async trashDirectory(groupKey, item) {
    let path = item.path;

    if (path){
      try{
        await FileManager.trashDirectory(path);
        let group = this.state.data[groupKey];
        let index = group.groups.indexOf(item);

        if (!group || index === -1){
          return;
        }

        let groups = group.groups.slice();
        groups.splice(index, 1)

        this.setState({
          data: {
            ...this.state.data,
            [groupKey]: {
              size: group.size - item.size,
              groups: groups,
            }
          }
        })

      } catch (e){
        console.log('errrr', e);
      }
    }
  }

  renderItem(item, groupKey){
    return (
      
        {item.label}
        {humanize(item.size, 1)}
        

你可能感兴趣的:(Cleaner for Xcode)