Skip to content

Commit e6739d4

Browse files
committed
preseve imports from stale files
1 parent 9d96652 commit e6739d4

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/app/parser/typescript/cleanup.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ export function fixImports(generatedCodeList: types.GeneratedCode[]) {
1919
project.createSourceFile(
2020
generatedCode.filePath,
2121
generatedCode.fileContent,
22-
{
23-
overwrite: true,
24-
},
22+
{ overwrite: true },
2523
);
2624
}
2725

2826
try {
2927
for (const sourceFile of project.getSourceFiles()) {
30-
sourceFile.fixMissingImports().organizeImports();
31-
3228
// find the source file in the generated code file list
3329
let curSourceFile =
3430
generatedCodeList.filter(
@@ -41,12 +37,18 @@ export function fixImports(generatedCodeList: types.GeneratedCode[]) {
4137
path.basename(sourceFile.getFilePath()),
4238
)[0];
4339

44-
if (curSourceFile) {
40+
if (curSourceFile) { // if the current file is either functions.ts or api.ts, fix imports and organize them
41+
sourceFile.fixMissingImports().organizeImports();
4542
curSourceFile!.fileContent = sourceFile.getFullText();
4643
} else {
47-
logger.error(
48-
`Error while fixing imports: Unable to find the source file for ${sourceFile.getFilePath()}\n\nSkipping import fixing and cleanup for this file`,
49-
);
44+
if (
45+
path.basename(sourceFile.getFilePath()) === "functions.ts" ||
46+
path.basename(sourceFile.getFilePath()) === "api.ts"
47+
) { // we only want to show this error if we're unable to find functions.ts or api.ts
48+
logger.error(
49+
`Error while fixing imports: Unable to find the source file for ${sourceFile.getFilePath()}\n\nSkipping import fixing and cleanup for this file`,
50+
);
51+
}
5052
}
5153
}
5254
} catch (error) {

src/app/parser/typescript/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ export function preserveUserChanges(
2626
morph.preserveSavedVariables(staleSourceFile, freshSourceFile);
2727
morph.preserveSavedFunctions(staleSourceFile, freshSourceFile);
2828

29+
// imports don't need @save annotation, they are always preserved and organized
30+
morph.preserveImportDeclarations(staleSourceFile, freshSourceFile);
31+
2932
return freshSourceFile.getFullText();
3033
}

src/app/writer/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export async function writeToFileSystem(codeToWrite: types.GeneratedCode[]) {
1616
(element) => element.fileType === "functions-ts",
1717
)[0]!;
1818

19-
await apiWriter.writeToFileSystem(apiTsCode);
20-
await functionsWriter.writeToFileSystem(functionsTsCode, apiTsCode);
2119
await packageJsonWriter.writeToFileSystem();
2220
tsConfigWriter.writeToFileSystem();
21+
await apiWriter.writeToFileSystem(apiTsCode);
22+
await functionsWriter.writeToFileSystem(functionsTsCode, apiTsCode);
2323

2424
logger.info("running npm install :: installing dependencies");
2525
if (process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH) {

0 commit comments

Comments
 (0)