import { rimraf, fsStatSync } from '@cirrusct/fs'; import { createError, MrBuildSpecBuilderCommandHandlerArgs, MrPackageBuilderCommandResults } from '@cirrusct/mr-core'; import { existsSync } from 'fs'; import * as nodePath from 'path'; import { CleanCommandOptions } from '../../commands/clean'; import { SpecBuilderCommandHandlerBase } from './SpecBuilderCommandHandlerBase'; export abstract class SpecBuilderCleanCommandHandlerBase extends SpecBuilderCommandHandlerBase { protected constructor(args: MrBuildSpecBuilderCommandHandlerArgs) { super(args); } public async run(): Promise { const { deep = false, removeLock = false } = this.options; await this.clearOutputPath(false); if (deep) { await this.clearNodeModules(); } if (removeLock) { await this.runRemoveLock(); } return { exitCode: 0, }; } protected clearNodeModules = () => { const nodeModulesPath = nodePath.join(this.mrPackage.path, 'node_modules'); if (existsSync(nodeModulesPath)) { this.logger.debug({ nodeModulesPath, package: this.mrPackage.name, }, 'Removing node_modules in package') try { rimraf(nodeModulesPath); } catch (e) { throw createError(`Failed to clear node_modules path for package: '${nodeModulesPath}'`, e); } } } private runRemoveLock = () => { const lockFileNames = ['yarn.lock', 'package-lock.json']; const lockFilePaths = lockFileNames.map(f => nodePath.join(this.mrPackage.path, f)); for (const lockFilePath of lockFilePaths) { if (existsSync(lockFilePath)) { this.logger.debug({ lockFilePath, package: this.mrPackage.name, }, 'Removing package lock file') try { rimraf(lockFilePath); } catch (e) { throw createError(`Failed to clear lock file for package: '${lockFilePath}'`, e); } } } } }