UNPKG

style-dictionary

Version:

Style once, use everywhere. A build system for creating cross-platform styles.

64 lines (55 loc) 2.04 kB
/* * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ import chalk from 'chalk'; import { dirname } from 'path-unified'; import { fs } from 'style-dictionary/fs'; import { logVerbosityLevels } from './enums/index.js'; /** * @typedef {import('../types/Volume.d.ts').Volume} Volume * @typedef {import('../types/Config.d.ts').PlatformConfig} Config * @typedef {import('../types/File.d.ts').File} File */ /** * Takes the style property object and a format and returns a * string that can be written to a file. * @memberOf StyleDictionary * @param {File} file * @param {Config} [platform] * @param {Volume} [vol] */ export default async function cleanDir(file, platform = {}, vol = fs) { let { destination } = file; if (typeof destination !== 'string') throw new Error('Please enter a valid destination'); // if there is a clean path, prepend the destination with it if (platform.buildPath) { destination = platform.buildPath + destination; } let dir = dirname(destination); while (dir) { if (vol.existsSync(dir)) { const dirContents = vol.readdirSync(dir, 'buffer'); if (dirContents.length === 0) { if (platform.log?.verbosity !== logVerbosityLevels.silent) { // eslint-disable-next-line no-console console.log(chalk.bold.red('-') + ' ' + dir); } vol.rmSync(dir, { recursive: true }); } else { break; } } const splitDir = dir.split('/'); splitDir.pop(); dir = splitDir.join('/'); } }