UNPKG

1.03 kBPlain TextView Raw
1import { tsUtils } from '@neo-one/ts-utils';
2import ts from 'typescript';
3import { createContextForDir } from './createContext';
4
5export type Contracts = { [K in string]?: ReadonlyArray<string> };
6
7export const scan = async (dir: string): Promise<Contracts> => {
8 const context = await createContextForDir(dir);
9 const smartContract = tsUtils.symbol.getDeclarations(context.libs.SmartContract)[0];
10 if (!ts.isClassDeclaration(smartContract)) {
11 throw new Error('Something went wrong!');
12 }
13
14 return tsUtils.class_
15 .getDerivedClasses(context.program, context.languageService, smartContract)
16 .reduce<Contracts>((acc, derived) => {
17 if (!tsUtils.modifier.isAbstract(derived)) {
18 const file = tsUtils.file.getFilePath(tsUtils.node.getSourceFile(derived));
19 const name = tsUtils.node.getNameOrThrow(derived);
20 const files = acc[file];
21
22 return {
23 ...acc,
24 [file]: files === undefined ? [name] : files.concat([name]),
25 };
26 }
27
28 return acc;
29 }, {});
30};
31
\No newline at end of file