UNPKG

es-check-min

Version:

A CLI tool and library to find the minimum compatible ECMAScript version of a JavaScript file.

39 lines (35 loc) 988 B
import { execa } from 'execa'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { findPackage } from 'pkg-types'; async function getEsCheckPath() { const packagePath = await findPackage(path.resolve(path.dirname(fileURLToPath(import.meta.url)))); return path.join(path.dirname(packagePath), "/node_modules/.bin/es-check"); } const esCheckPath = await getEsCheckPath(); const esVersions = [ // 'es3', // not working "es5", "es6/es2015", "es7/es2016", "es8/es2017", "es9/es2018", "es10/es2019", "es11/es2020", "es12/es2021", "es13/es2022" // 'es14/es2023', // not working ]; async function esCheckMin(path) { for (const esVersion of esVersions) { const cleanVersion = esVersion.split("/").at(-1) ?? esVersion; try { await execa(esCheckPath, [cleanVersion, path, "--silent", "--allow-hash-bang", "--module"]); return esVersion; } catch { continue; } } return void 0; } export { esCheckMin };