import fs from 'fs' ; import os from 'os' ; import path from 'path' ; import { SubqlRuntimeHandler , SubqlCustomHandler , SubqlHandler , SubqlHandlerKind , } from '@subql/types' ; import tar from 'tar' ; export async function prepareProjectDir ( projectPath ) { const stats = fs . statSync ( projectPath ) ; if ( stats . isFile ( ) ) { const sep = path . sep ; const tmpDir = os . tmpdir ( ) ; const tempPath = fs . mkdtempSync ( `${ tmpDir }${ sep }` ) ; await tar . x ( { file : projectPath , cwd : tempPath } ) ; return tempPath . concat ( '/package' ) ; } else if ( stats . isDirectory ( ) ) { return projectPath ; } } const projectEntryCache = { } ; export function getProjectEntry ( root ) { const pkgPath = path . join ( root , 'package.json' ) ; try { if ( ! projectEntryCache [ pkgPath ] ) { const content = fs . readFileSync ( pkgPath ) . toString ( ) ; const pkg = JSON . parse ( content ) ; if ( ! pkg . main ) { return './dist' ; } projectEntryCache [ pkgPath ] = pkg . main . startsWith ( './' ) ? pkg . main : `./${ pkg . main }` ; } return projectEntryCache [ pkgPath ] ; } catch ( err ) { throw new Error ( `can not find package.json within directory ${ this . option . root }` ) ; } } export function isBaseHandler ( handler ) { return Object . values < string > ( SubqlHandlerKind ) . includes ( handler . kind ) ; } export function isCustomHandler < K extends string , F > ( handler ) { return ! isBaseHandler ( handler ) ; }