Skip to content

Commit

Permalink
feat(plugin): traverse env files
Browse files Browse the repository at this point in the history
  • Loading branch information
crherman7 committed Dec 30, 2024
1 parent 4261b11 commit dbc09fd
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/plugin-fsapp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,6 +20,61 @@ import {
type PrebuildOptions,
} from '@brandingbrand/code-cli-kit';

async function validateEnvFiles(baseDir: string): Promise<any[]> {
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.
Expand Down

0 comments on commit dbc09fd

Please sign in to comment.