Tesla CEO Elon Musk says the firm will leave California after he is ordered to keep a factory shut.