Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
a3a4978
Added frameworkSpec and different types used for discoving frameworks
svnsairam Jun 8, 2023
56e7844
Added repository file system to check if file exists and to read file…
svnsairam Jun 8, 2023
560a9b3
Removed comments and modified error handling.
svnsairam Jun 9, 2023
0c8197b
Resolved indentation issues
svnsairam Jun 9, 2023
56195cf
Discovery: Added code analyse codebase and match to a frameworkSpec
svnsairam Jun 9, 2023
e516d65
Added test express app
svnsairam Jun 9, 2023
d8c3286
FileSystem now tests file existance and reads contents from test app
svnsairam Jun 9, 2023
99c6687
Update filesystem.ts
svnsairam Jun 9, 2023
3da93d6
Update filesystem.spec.ts
svnsairam Jun 9, 2023
a0dd5b1
Update filesystem.ts
svnsairam Jun 9, 2023
b856a70
Changes to filesystem.ts
svnsairam Jun 9, 2023
b3b8cca
Update filesystem.spec.ts
svnsairam Jun 9, 2023
e56d9ad
Update filesystem.spec.ts
svnsairam Jun 9, 2023
253700a
Updated file system current working directory
svnsairam Jun 9, 2023
c29a58d
Changes to filesystem.spec.ts
svnsairam Jun 9, 2023
89426da
Added code to test if correct frameworkSpec is recognized
svnsairam Jun 9, 2023
8b54742
Modified next commands
svnsairam Jun 9, 2023
e01a9af
Removed types which aren't focused at this time.
svnsairam Jun 9, 2023
5db9a4e
Added mockFileSystem for testing
svnsairam Jun 9, 2023
132aa90
Added error handling
svnsairam Jun 9, 2023
6a018dd
Resolved code comments
svnsairam Jun 9, 2023
e6fb837
Removed lint errors
svnsairam Jun 9, 2023
02821af
Removed lint errors
svnsairam Jun 9, 2023
ee3d404
Merge branch 'firebase:master' into framework-discover-v3
svnsairam Jun 12, 2023
886c73a
changes to desription name
svnsairam Jun 10, 2023
ba47e79
Resolved error handling and test cases changes
svnsairam Jun 13, 2023
373547a
Resolved comments
svnsairam Jun 13, 2023
d4d1a6e
Error handling changes
svnsairam Jun 13, 2023
29fa655
Resolved comments
svnsairam Jun 13, 2023
30eb4db
Added frameworkSpec and different types used for discoving frameworks
svnsairam Jun 8, 2023
ca0ead1
Added repository file system to check if file exists and to read file…
svnsairam Jun 8, 2023
4806390
Removed comments and modified error handling.
svnsairam Jun 9, 2023
7660946
Resolved indentation issues
svnsairam Jun 9, 2023
5324af0
Discovery: Added code analyse codebase and match to a frameworkSpec
svnsairam Jun 9, 2023
eff7156
Added test express app
svnsairam Jun 9, 2023
5154acc
FileSystem now tests file existance and reads contents from test app
svnsairam Jun 9, 2023
c939217
Update filesystem.ts
svnsairam Jun 9, 2023
7ee698b
Update filesystem.spec.ts
svnsairam Jun 9, 2023
1806915
Update filesystem.ts
svnsairam Jun 9, 2023
f470107
Changes to filesystem.ts
svnsairam Jun 9, 2023
ea339cd
Update filesystem.spec.ts
svnsairam Jun 9, 2023
b5053e3
Update filesystem.spec.ts
svnsairam Jun 9, 2023
2b6d373
Updated file system current working directory
svnsairam Jun 9, 2023
cd5bbb8
Changes to filesystem.spec.ts
svnsairam Jun 9, 2023
a7b7b32
Added code to test if correct frameworkSpec is recognized
svnsairam Jun 9, 2023
9252f3d
Created node runtime to analyse framework
svnsairam Jun 9, 2023
fcf2bdb
Modified next commands
svnsairam Jun 9, 2023
608cc64
Removed types which aren't focused at this time.
svnsairam Jun 9, 2023
0d3b2d2
Added mockFileSystem for testing
svnsairam Jun 9, 2023
aebaf5b
Added error handling
svnsairam Jun 9, 2023
25fc256
Resolved code comments
svnsairam Jun 9, 2023
196828b
Removed lint errors
svnsairam Jun 9, 2023
5252b9e
Removed lint errors
svnsairam Jun 9, 2023
aac9003
Added code for Node runtime
svnsairam Jun 10, 2023
abf80ef
changes to desription name
svnsairam Jun 10, 2023
435c3fc
Tests for runtime functionality
svnsairam Jun 12, 2023
090e4fb
Resolved error handling and test cases changes
svnsairam Jun 13, 2023
d7e1ba1
Resolved comments
svnsairam Jun 13, 2023
23605aa
Error handling changes
svnsairam Jun 13, 2023
c831cf9
Formatting
svnsairam Jun 13, 2023
ac3ff13
Error handling corrections
svnsairam Jun 13, 2023
06eff9e
Fix firebase-vscode/package-lock.json and add it to linting. (#5965)
yuchenshi Jun 12, 2023
c3c3fb9
Created node runtime to analyse framework
svnsairam Jun 9, 2023
ac7375f
Added code for Node runtime
svnsairam Jun 10, 2023
eceb283
Tests for runtime functionality
svnsairam Jun 12, 2023
5565556
Formatting
svnsairam Jun 13, 2023
822fc93
Error handling corrections
svnsairam Jun 13, 2023
e46b375
Merge local with remote
svnsairam Jun 13, 2023
cc6f4d9
Error handling
svnsairam Jun 13, 2023
eb88ced
Merge branch 'firebase:master' into framework-discover-v3
svnsairam Jun 13, 2023
43e63c3
Resolved comments
svnsairam Jun 14, 2023
4d764c4
Created node runtime to analyse framework
svnsairam Jun 9, 2023
c1e98b0
Added code for Node runtime
svnsairam Jun 10, 2023
aea0198
Tests for runtime functionality
svnsairam Jun 12, 2023
3a1628f
Formatting
svnsairam Jun 13, 2023
8abf35b
Error handling corrections
svnsairam Jun 13, 2023
f29eae7
Removed comments and modified error handling.
svnsairam Jun 9, 2023
0a8e444
Resolved indentation issues
svnsairam Jun 9, 2023
336a530
Added test express app
svnsairam Jun 9, 2023
cb02e7d
Update filesystem.ts
svnsairam Jun 9, 2023
caaa7a4
Update filesystem.ts
svnsairam Jun 9, 2023
b9863de
Changes to filesystem.spec.ts
svnsairam Jun 9, 2023
e2667bf
Added code to test if correct frameworkSpec is recognized
svnsairam Jun 9, 2023
555caf0
Created node runtime to analyse framework
svnsairam Jun 9, 2023
43c1ad9
Added mockFileSystem for testing
svnsairam Jun 9, 2023
9cb0953
Resolved code comments
svnsairam Jun 9, 2023
c9e9ab1
Removed lint errors
svnsairam Jun 9, 2023
f26e859
Added code for Node runtime
svnsairam Jun 10, 2023
63e6477
Tests for runtime functionality
svnsairam Jun 12, 2023
6d11771
Formatting
svnsairam Jun 13, 2023
b8308bf
Error handling
svnsairam Jun 13, 2023
a6b39b1
Added code for Node runtime
svnsairam Jun 10, 2023
b0e0f9e
Tests for runtime functionality
svnsairam Jun 12, 2023
f1cabdc
Formatting
svnsairam Jun 13, 2023
231716f
Error handling corrections
svnsairam Jun 13, 2023
f2c9aa3
Removed comments and modified error handling.
svnsairam Jun 9, 2023
2af246c
Resolved indentation issues
svnsairam Jun 9, 2023
5e21afc
Added test express app
svnsairam Jun 9, 2023
55b073f
Update filesystem.ts
svnsairam Jun 9, 2023
bc3bae4
Update filesystem.ts
svnsairam Jun 9, 2023
07cc9c6
Changes to filesystem.spec.ts
svnsairam Jun 9, 2023
3f37849
Added code to test if correct frameworkSpec is recognized
svnsairam Jun 9, 2023
6311519
Created node runtime to analyse framework
svnsairam Jun 9, 2023
3376a3c
Added mockFileSystem for testing
svnsairam Jun 9, 2023
56bea8e
Added code for Node runtime
svnsairam Jun 10, 2023
ce7344a
Tests for runtime functionality
svnsairam Jun 12, 2023
ae2fadb
Formatting
svnsairam Jun 13, 2023
30829ef
Merging local to remote v4 branch as local is ahead of remote v4.
svnsairam Jun 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/frameworks/compose/discover/filesystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { FileSystem } from "./types";
import { pathExists, readFile } from "fs-extra";
import * as path from "path";
import { FirebaseError } from "../../../error";
import { logger } from "../../../../src/logger";

/**
* Find files or read file contents present in the directory.
*/
export class LocalFileSystem implements FileSystem {
private readonly existsCache: Record<string, boolean> = {};
private readonly contentCache: Record<string, string> = {};

constructor(private readonly cwd: string) {}

async exists(file: string): Promise<boolean> {
try {
if (!(file in this.contentCache)) {
this.existsCache[file] = await pathExists(path.resolve(this.cwd, file));
}

return this.existsCache[file];
} catch (error) {
throw new FirebaseError(`Error occured while searching for file: ${error}`);
}
}

async read(file: string): Promise<string> {
try {
if (!(file in this.contentCache)) {
const fileContents = await readFile(path.resolve(this.cwd, file), "utf-8");
this.contentCache[file] = fileContents;
}
return this.contentCache[file];
} catch (error) {
logger.error("Error occured while reading file contents.");
throw error;
}
}
}

/**
* Convert ENOENT errors into null
*/
export async function readOrNull(fs: FileSystem, path: string): Promise<string | null> {
try {
return fs.read(path);
} catch (err: any) {
if (err && typeof err === "object" && err?.code === "ENOENT") {
logger.debug("ENOENT error occured while reading file.");
return null;
}
throw new Error(`Unknown error occured while reading file: ${err}`);
}
}
103 changes: 103 additions & 0 deletions src/frameworks/compose/discover/frameworkMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { FirebaseError } from "../../../error";
import { FrameworkSpec, FileSystem } from "./types";
import { logger } from "../../../logger";

export function filterFrameworksWithDependencies(
allFrameworkSpecs: FrameworkSpec[],
dependencies: Record<string, string>
): FrameworkSpec[] {
return allFrameworkSpecs.filter((framework) => {
return framework.requiredDependencies.every((dependency) => {
return dependency.name in dependencies;
});
});
}

export async function filterFrameworksWithFiles(
allFrameworkSpecs: FrameworkSpec[],
fs: FileSystem
): Promise<FrameworkSpec[]> {
try {
const filteredFrameworks = [];
for (const framework of allFrameworkSpecs) {
if (!framework.requiredFiles) {
filteredFrameworks.push(framework);
continue;
}
let isRequired = true;
for (let files of framework.requiredFiles) {
files = Array.isArray(files) ? files : [files];
for (const file of files) {
isRequired = isRequired && (await fs.exists(file));
if (!isRequired) {
break;
}
}
}
if (isRequired) {
filteredFrameworks.push(framework);
}
}

return filteredFrameworks;
} catch (error) {
logger.error("Error: Unable to filter frameworks based on required files", error);
throw error;
}
}

/**
* Embeded frameworks help to resolve tiebreakers when multiple frameworks are discovered.
* Ex: "next" embeds "react", so if both frameworks are discovered,
* we can suggest "next" commands by removing its embeded framework (react).
*/
export function removeEmbededFrameworks(allFrameworkSpecs: FrameworkSpec[]): FrameworkSpec[] {
const embededFrameworkSet: Set<string> = new Set<string>();

for (const framework of allFrameworkSpecs) {
if (!framework.embedsFrameworks) {
continue;
}
for (const item of framework.embedsFrameworks) {
embededFrameworkSet.add(item);
}
}

return allFrameworkSpecs.filter((item) => !embededFrameworkSet.has(item.id));
}

/**
* Identifies the best FrameworkSpec for the codebase.
*/
export async function frameworkMatcher(
runtime: string,
fs: FileSystem,
frameworks: FrameworkSpec[],
dependencies: Record<string, string>
): Promise<FrameworkSpec | null> {
try {
const filterRuntimeFramework = frameworks.filter((framework) => framework.runtime === runtime);
const frameworksWithDependencies = filterFrameworksWithDependencies(
filterRuntimeFramework,
dependencies
);
const frameworkWithFiles = await filterFrameworksWithFiles(frameworksWithDependencies, fs);
const allMatches = removeEmbededFrameworks(frameworkWithFiles);

if (allMatches.length === 0) {
return null;
}
if (allMatches.length > 1) {
const frameworkNames = allMatches.map((framework) => framework.id);
throw new FirebaseError(
`Multiple Frameworks are matched: ${frameworkNames.join(
", "
)} Manually set up override commands in firebase.json`
);
}

return allMatches[0];
} catch (error: any) {
throw new FirebaseError(`Failed to match the correct framework: ${error}`);
}
}
38 changes: 38 additions & 0 deletions src/frameworks/compose/discover/frameworkSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FrameworkSpec } from "./types";

export const frameworkSpecs: FrameworkSpec[] = [
{
id: "express",
runtime: "nodejs",
webFrameworkId: "Express.js",
requiredDependencies: [
{
name: "express",
},
],
},
{
id: "nextjs",
runtime: "nodejs",
webFrameworkId: "Next.js",
requiredFiles: ["next.config.js", "next.config.ts"],
requiredDependencies: [
{
name: "next",
},
],
commands: {
build: {
cmd: "next build",
},
dev: {
cmd: "next dev",
env: { NODE_ENV: "dev" },
},
run: {
cmd: "next run",
env: { NODE_ENV: "production" },
},
},
},
];
Loading