@supernovaio/cli
Version:
Supernova.io Command Line Interface
52 lines (50 loc) • 2.47 kB
JavaScript
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="b2f91823-ca3b-5c4a-9b84-889510c44041")}catch(e){}}();
import { globSync } from "glob";
import path from "node:path";
import * as ts from "typescript";
import { mapToSupernovaComponent } from "./mappers/component.js";
import { defaultOptions, ModuleParser } from "./parser/index.js";
import { getModuleEntryPath } from "./utils/get-module-exports-path.js";
function findAndParseTsConfig(directory) {
const tsconfigPath = path.join(directory, "tsconfig.json");
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
if (configFile.error) {
return undefined;
}
return ts.parseJsonConfigFileContent(configFile.config, ts.sys, directory, undefined, tsconfigPath);
}
async function getDefaultFiles(rootDir) {
return globSync("**/*.{ts,tsx,js,jsx,mts,mjs,cts,cjs}", {
absolute: true,
cwd: rootDir,
ignore: [
"**/.supernova/**",
"**/build/**",
"**/dist/**",
"**/node_modules/**",
"**/*.d.ts",
"**/*.d.mts",
"**/*.d.cts",
],
});
}
export async function analyzeComponents(input) {
const entryFilePath = await getModuleEntryPath(input.rootDir, input.importFrom);
const tsConfig = findAndParseTsConfig(input.rootDir);
const isNodeModuleImport = entryFilePath.includes("/node_modules/");
const discoveredTsFiles = tsConfig?.fileNames.length && !isNodeModuleImport
? tsConfig.fileNames
: await getDefaultFiles(isNodeModuleImport ? `${input.rootDir}/node_modules` : input.rootDir);
const tsFiles = discoveredTsFiles.includes(entryFilePath) ? discoveredTsFiles : [entryFilePath, ...discoveredTsFiles];
const program = ts.createProgram(tsFiles, {
...defaultOptions,
...(tsConfig?.options ?? {}),
allowJs: true,
checkJs: false,
});
const moduleParser = new ModuleParser(program, entryFilePath, input.parserOptions);
const moduleComponentsInfo = moduleParser.parseModule();
return moduleComponentsInfo.map(componentInfo => mapToSupernovaComponent(componentInfo, path.dirname(entryFilePath)));
}
//# sourceMappingURL=analyze.js.map
//# debugId=b2f91823-ca3b-5c4a-9b84-889510c44041