-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from hasura/m-bilal/fix-import-issues--edc-38
remove unused imports, organize imports and fix import issues
- Loading branch information
Showing
27 changed files
with
76,120 additions
and
985 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
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,63 @@ | ||
import * as assert from "assert"; | ||
import path from "path"; | ||
import * as appTypes from "../../types"; | ||
import * as fs from "fs"; | ||
import * as cleanup from "./cleanup"; | ||
|
||
const tests: { | ||
name: string; | ||
_files?: appTypes.GeneratedCode[]; | ||
_goldenFileContent?: string; | ||
_functionsTsFile?: appTypes.GeneratedCode; | ||
_goldenFilePath?: string; | ||
}[] = [ | ||
{ | ||
name: "atlassian-jira", | ||
}, | ||
]; | ||
|
||
describe("cleanup::fixImports", async () => { | ||
for (const testCase of tests) { | ||
before(async () => { | ||
const testFileDir = path.resolve( | ||
__dirname, | ||
"./test-data/cleanup-tests/", | ||
testCase.name, | ||
); | ||
|
||
const apiFilePath = path.resolve(testFileDir, "api"); | ||
const functionsFilePath = path.resolve(testFileDir, "test"); | ||
testCase._goldenFilePath = path.resolve(testFileDir, "golden-file"); | ||
testCase._goldenFileContent = fs | ||
.readFileSync(testCase._goldenFilePath) | ||
.toString(); | ||
|
||
const apiTsFile: appTypes.GeneratedCode = { | ||
fileContent: fs.readFileSync(apiFilePath).toString(), | ||
filePath: `${apiFilePath}.ts`, | ||
fileType: "api-ts", | ||
}; | ||
|
||
const functionsTsFile: appTypes.GeneratedCode = { | ||
fileContent: fs.readFileSync(functionsFilePath).toString(), | ||
filePath: `${functionsFilePath}.ts`, | ||
fileType: "functions-ts", | ||
}; | ||
|
||
testCase._files = [apiTsFile, functionsTsFile]; | ||
testCase._functionsTsFile = functionsTsFile; | ||
}); | ||
|
||
it(`${testCase.name}`, async () => { | ||
cleanup.fixImports(testCase._files!); | ||
|
||
assert.equal( | ||
testCase._functionsTsFile!.fileContent, | ||
testCase._goldenFileContent!, | ||
); | ||
|
||
// update golden file | ||
// fs.writeFileSync(testCase._goldenFilePath!, testCase._functionsTsFile!.fileContent); | ||
}); | ||
} | ||
}); |
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,34 @@ | ||
import * as ts from "ts-morph"; | ||
import * as types from "../../types"; | ||
import * as context from "../../context"; | ||
import * as fs from "fs"; | ||
|
||
export function fixImports(generatedCodeList: types.GeneratedCode[]) { | ||
let project: ts.Project; | ||
if (fs.existsSync(context.getInstance().getTsConfigFilePath())) { | ||
project = new ts.Project({ | ||
tsConfigFilePath: context.getInstance().getTsConfigFilePath(), | ||
}); | ||
} else { | ||
project = new ts.Project(); | ||
} | ||
|
||
for (const generatedCode of generatedCodeList) { | ||
project.createSourceFile( | ||
generatedCode.filePath, | ||
generatedCode.fileContent, | ||
{ | ||
overwrite: true, | ||
}, | ||
); | ||
} | ||
|
||
for (const sourceFile of project.getSourceFiles()) { | ||
sourceFile.fixMissingImports().organizeImports(); | ||
|
||
const code = generatedCodeList.filter( | ||
(item) => item.filePath === sourceFile.getFilePath(), | ||
)[0]!; | ||
code.fileContent = sourceFile.getFullText(); | ||
} | ||
} |
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,16 @@ | ||
import * as prettier from "prettier"; | ||
import * as logger from "../../../util/logger"; | ||
|
||
export async function format(code: string): Promise<string> { | ||
try { | ||
code = await prettier.format(code, { | ||
parser: "typescript", | ||
}); | ||
} catch (e) { | ||
logger.error( | ||
"Error while formatting code. The resulting code will be unformatted and may contain syntax errors. Error: ", | ||
e, | ||
); | ||
} | ||
return code; | ||
} |
Oops, something went wrong.