-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: setup src and util directories, update build script
- Loading branch information
1 parent
03149ca
commit 52e0a14
Showing
8 changed files
with
109 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import path from 'path'; | ||
import { existsSync } from 'fs'; | ||
|
||
// Recursively looks for a given file, going up directories until it is found or not | ||
// Used here to find a package.json to confirm where to install dependencies | ||
export const findFileUpParent = ( | ||
filename: string, | ||
startDir: string | ||
): string | null => { | ||
let currentDir = startDir || process.cwd(); | ||
while (currentDir !== path.parse(currentDir).root) { | ||
const filePath = path.join(currentDir, filename); | ||
if (existsSync(filePath)) { | ||
return filePath; | ||
} | ||
currentDir = path.dirname(currentDir); | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { builtinModules } from 'module'; | ||
import ts from 'typescript'; | ||
|
||
// Returns array of imports for a given file path | ||
export const getImports = (fileName: string, tsHost): string[] => { | ||
const sourceFile = tsHost.getSourceFile( | ||
fileName, | ||
ts.ScriptTarget.Latest, | ||
(msg) => { | ||
throw new Error(`Failed to parse ${fileName}: ${msg}`); | ||
} | ||
); | ||
if (!sourceFile) throw ReferenceError(`Failed to find file ${fileName}`); | ||
const importing: string[] = []; | ||
delintNode(sourceFile); | ||
return importing; | ||
|
||
function delintNode(node: ts.Node) { | ||
if (ts.isImportDeclaration(node)) { | ||
const moduleName = node.moduleSpecifier.getText().replace(/['"]/g, ''); | ||
if ( | ||
!moduleName.startsWith('node:') && | ||
!builtinModules.includes(moduleName) | ||
) | ||
importing.push(moduleName); | ||
} else ts.forEachChild(node, delintNode); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import ts from 'typescript'; | ||
|
||
// Returns true if a given import is external | ||
export const isExternalImport = ( | ||
fileName: string, | ||
importPath: string, | ||
tsHost | ||
): boolean => { | ||
const program = ts.createProgram([fileName], {}, tsHost); | ||
const sourceFile = program.getSourceFile(fileName); | ||
if (sourceFile) { | ||
const importDeclaration = sourceFile.statements.find((node) => { | ||
return ( | ||
ts.isImportDeclaration(node) && | ||
node.moduleSpecifier.getText() === `'${importPath}'` | ||
); | ||
}); | ||
|
||
if (importDeclaration) { | ||
const resolvedModule = ts.resolveModuleName( | ||
importPath, | ||
fileName, | ||
program.getCompilerOptions(), | ||
ts.sys | ||
); | ||
|
||
return resolvedModule.resolvedModule?.isExternalLibraryImport ?? false; | ||
} | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"module": "ESNext", | ||
"esModuleInterop": true, | ||
"target": "esnext", | ||
"moduleResolution": "node", | ||
"moduleResolution": "bundler", | ||
"sourceMap": true, | ||
"outDir": "dist" | ||
"outDir": "dist", | ||
"rootDir": "src" | ||
}, | ||
"lib": ["es2015"], | ||
"files": [ | ||
"index.ts" | ||
], | ||
"include": ["typings.d.ts"], | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["node_modules", ".vscode"] | ||
} |