diff --git a/packages/plugin-fsapp/src/index.ts b/packages/plugin-fsapp/src/index.ts index 5123d0235c..024f8f05ce 100644 --- a/packages/plugin-fsapp/src/index.ts +++ b/packages/plugin-fsapp/src/index.ts @@ -6,6 +6,11 @@ */ import semver from 'semver'; +import glob from 'glob'; +import * as babel from '@babel/core'; +import {types as t} from '@babel/core'; +import {parse} from '@babel/parser'; +import traverse from '@babel/traverse'; import { definePlugin, fs, @@ -15,6 +20,61 @@ import { type PrebuildOptions, } from '@brandingbrand/code-cli-kit'; +async function validateEnvFiles(baseDir: string): Promise { + const pattern = path.join(baseDir, '**', 'env.*.ts'); + const files = glob.sync(pattern, {ignore: '**/node_modules/**'}); + + const results = []; + + for (const file of files) { + const code = await fs.readFile(file, 'utf-8'); + const ast = parse(code, { + sourceType: 'module', + plugins: ['typescript'], + }); + + let hasDefaultExport = false; + let hasDefineEnvImport = false; + + traverse(ast, { + ExportNamedDeclaration(path) { + const declaration = path.node.declaration; + if ( + t.isTypeAlias(declaration) || + t.isInterfaceDeclaration(declaration) || + t.isTSTypeAliasDeclaration(declaration) || + t.isTSInterfaceDeclaration(declaration) + ) { + // Allows type exports + return; + } + }, + ExportDefaultDeclaration() { + hasDefaultExport = true; + }, + ImportDeclaration(path) { + if ( + path.node.source.value === '@brandingbrand/code-cli-kit' && + path.node.specifiers.some( + specifier => + t.isImportSpecifier(specifier) && + t.isIdentifier(specifier.imported) && + specifier.imported.name === 'defineEnv', + ) + ) { + hasDefineEnvImport = true; + } + }, + }); + + if (hasDefaultExport && hasDefineEnvImport) { + results.push(file); + } + } + + return results; +} + /** * Helper function to validate and process environment files. * @param {string} envDir - The path to the environment directory.