clean.mjs (1262B)
1 #!/usr/bin/env node 2 3 /** 4 * @license 5 * Copyright 2022 Google Inc. 6 * SPDX-License-Identifier: Apache-2.0 7 */ 8 9 import {exec} from 'child_process'; 10 import {readdirSync} from 'fs'; 11 import path from 'path'; 12 import url from 'url'; 13 import {promisify} from 'util'; 14 15 const execAsync = promisify(exec); 16 17 try { 18 await execAsync('git status'); 19 } catch { 20 // If `git status` threw an error, we are not in a git repository, bail out. 21 console.log('Not inside a .git repository'); 22 process.exit(0); 23 } 24 25 const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); 26 const puppeteerRoot = path.join(__dirname, '../'); 27 28 const {stdout} = await execAsync('git rev-parse --show-toplevel'); 29 const gitTopLevel = stdout.replaceAll('\n', '').trim(); 30 31 // https://github.com/puppeteer/puppeteer/issues/12917 32 if (path.relative(gitTopLevel, puppeteerRoot) !== '') { 33 console.log('Top level .git is not Puppeteer'); 34 process.exit(1); 35 } 36 37 // Prevents cleaning config files such as in `.vscode` 38 if (path.relative(puppeteerRoot, process.cwd()) === '') { 39 console.log('Trying to execute from Puppeteer root'); 40 process.exit(1); 41 } 42 43 await execAsync( 44 `git clean -Xf ${readdirSync(process.cwd()) 45 .filter(file => { 46 return file !== 'node_modules'; 47 }) 48 .join(' ')}`, 49 );