-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Add compiler option to read project's tsconfig file #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,25 @@ | ||
| import { promises as fs } from 'fs'; | ||
| import { resolve, relative } from 'path'; | ||
| import { resolve, relative, join } from 'path'; | ||
| import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata'; | ||
| import { CompilerOptions } from 'typescript'; | ||
|
|
||
| import { IAppSource, ICompilerFile, IMapCompilerFile } from '../definition'; | ||
| import { AppCompilerOptions } from '../AppsCompiler'; | ||
| import logger from '../misc/logger'; | ||
|
|
||
| async function walkDirectory(directory: string): Promise<ICompilerFile[]> { | ||
| export type TSConfig = { | ||
| compilerOptions?: CompilerOptions; | ||
| exclude?: string[]; | ||
| } | ||
|
|
||
| async function walkDirectory(directory: string, projectExcludes: string[] = []): Promise<ICompilerFile[]> { | ||
| const dirents = await fs.readdir(directory, { withFileTypes: true }); | ||
| const dirsToIgnore = projectExcludes.concat(['node_modules', '.git']); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be included in app.json file? Also, can we add files here too?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what the |
||
| const files = await Promise.all( | ||
| dirents | ||
| .map(async (dirent) => { | ||
| const res = resolve(directory, dirent.name); | ||
|
|
||
| const dirsToIgnore = ['node_modules', '.git']; | ||
| if (dirsToIgnore.some((dir) => res.includes(dir))) { | ||
| return null; | ||
| } | ||
|
|
@@ -48,30 +56,52 @@ function makeICompilerFileMap(compilerFiles: ICompilerFile[]): IMapCompilerFile | |
| .reduce((acc: IMapCompilerFile, curr: IMapCompilerFile) => ({ ...acc, ...curr }), {}); | ||
| } | ||
|
|
||
| async function getTSConfig(projectPath: string): Promise<TSConfig> { | ||
| const tsconfigFile = await fs.readFile(join(projectPath, 'tsconfig.json')); | ||
|
|
||
|
|
||
| if (!tsconfigFile) { | ||
| logger.debug('Project tsconfig.json file not found - ignoring'); | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| try { | ||
| const a = JSON.parse(tsconfigFile.toString()) as TSConfig; | ||
| return a; | ||
| } catch { | ||
| logger.warn('Invalid tsconfig.json file - ignoring'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, let's not ignore the file if it throws any error.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to ignore the tsconfig file if it is invalid, we can't risk breaking backward compatibility |
||
|
|
||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| function getAppInfo(projectFiles: ICompilerFile[]): IAppInfo { | ||
| const appJson = projectFiles.find((file: ICompilerFile) => file.name === 'app.json'); | ||
|
|
||
| if (!appJson) { | ||
| throw new Error('There is no app.json file in the project'); | ||
| throw new Error('There is no app.json file in the folder - is this a Rocket.Chat App project?'); | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(appJson.content) as IAppInfo; | ||
| } catch (error) { | ||
| throw new Error('app.json parsing fail'); | ||
| throw new Error('Error attempting to parse app.json'); | ||
| } | ||
| } | ||
|
|
||
| function getTypescriptFilesFromProject(projectFiles: ICompilerFile[]): ICompilerFile[] { | ||
| return projectFiles.filter((file: ICompilerFile) => file.name.endsWith('.ts')); | ||
| } | ||
|
|
||
| export async function getAppSource(path: string): Promise<IAppSource> { | ||
| const directoryWalkData: ICompilerFile[] = await walkDirectory(path); | ||
| export async function getAppSource(path: string, appCompilerOptions: AppCompilerOptions = {}): Promise<IAppSource> { | ||
| const { compilerOptions = null, exclude = [] } = appCompilerOptions.readTsProjectFile ? await getTSConfig(path) : {}; | ||
|
|
||
| const directoryWalkData: ICompilerFile[] = await walkDirectory(path, exclude); | ||
| const projectFiles: ICompilerFile[] = filterProjectFiles(path, directoryWalkData); | ||
| const tsFiles: ICompilerFile[] = getTypescriptFilesFromProject(projectFiles); | ||
| const appInfo: IAppInfo = getAppInfo(projectFiles); | ||
| const files: IMapCompilerFile = makeICompilerFileMap(tsFiles); | ||
|
|
||
| return { appInfo, sourceFiles: files }; | ||
| return { appInfo, sourceFiles: files, compilerOptions }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import { CompilerOptions } from 'typescript'; | ||
| import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata'; | ||
|
|
||
| import { ICompilerFile } from './ICompilerFile'; | ||
|
|
||
| export interface IAppSource { | ||
| appInfo: IAppInfo; | ||
| sourceFiles: { [filename: string]: ICompilerFile }; | ||
| compilerOptions?: CompilerOptions; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? Is there a reason for this options to be hardcoded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because these options affect the output of the compilation, and that's up to the host system to decide, not the app developer