-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
481 additions
and
2,937 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
2,160 changes: 0 additions & 2,160 deletions
2,160
...apshots__/newAdapter2Command.test.ts.snap → ...napshots__/newAdapterCommand.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
packages/adapters-library/src/scripts/adapterBuilder/addProtocol.ts
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,96 @@ | ||
import { promises as fs } from 'node:fs' | ||
import path from 'node:path' | ||
import { parse, print, types, visit } from 'recast' | ||
import { writeAndLintFile } from '../../core/utils/writeAndLintFile' | ||
import { sortEntries } from '../utils/sortEntries' | ||
import n = types.namedTypes | ||
import b = types.builders | ||
|
||
/** | ||
* @description Writes changes to include new adapter in src/adapters/protocols.ts file | ||
*/ | ||
export async function addProtocol({ | ||
protocolKey, | ||
protocolId, | ||
}: { | ||
protocolKey: string | ||
protocolId: string | ||
}) { | ||
const protocolsFile = path.resolve( | ||
'./packages/adapters-library/src/adapters/protocols.ts', | ||
) | ||
const contents = await fs.readFile(protocolsFile, 'utf-8') | ||
const ast = parse(contents, { | ||
parser: require('recast/parsers/typescript'), | ||
}) | ||
|
||
visit(ast, { | ||
visitVariableDeclarator(path) { | ||
const node = path.node | ||
if (!n.Identifier.check(node.id)) { | ||
// Skips any other declaration | ||
return false | ||
} | ||
|
||
if (node.id.name === 'Protocol') { | ||
addProtocolEntry(node, protocolKey, protocolId) | ||
} | ||
|
||
this.traverse(path) | ||
}, | ||
}) | ||
|
||
await writeAndLintFile(protocolsFile, print(ast).code) | ||
} | ||
|
||
/** | ||
* @description Adds a new entry to the Protocol constant if it does not exist. | ||
* | ||
* @param protocolListDeclaratorNode AST node for the Protocol declarator | ||
*/ | ||
function addProtocolEntry( | ||
protocolListDeclaratorNode: n.VariableDeclarator, | ||
protocolKey: string, | ||
protocolId: string, | ||
) { | ||
const protocolListObjectNode = protocolListDeclaratorNode.init | ||
if ( | ||
!n.TSAsExpression.check(protocolListObjectNode) || | ||
!n.ObjectExpression.check(protocolListObjectNode.expression) | ||
) { | ||
throw new Error('Incorrectly typed Protocol object') | ||
} | ||
|
||
const protocolEntryObjectNode = | ||
protocolListObjectNode.expression.properties.find((property) => { | ||
if ( | ||
!n.ObjectProperty.check(property) || | ||
!n.Identifier.check(property.key) | ||
) { | ||
throw new Error('Incorrectly typed Protocol object') | ||
} | ||
|
||
return property.key.name === protocolKey | ||
}) | ||
|
||
if (!protocolEntryObjectNode) { | ||
protocolListObjectNode.expression.properties.push( | ||
buildProtocolEntry(protocolKey, protocolId), | ||
) | ||
|
||
sortEntries( | ||
protocolListObjectNode.expression.properties, | ||
(entry) => ((entry as n.ObjectProperty).key as n.Identifier).name, | ||
) | ||
} | ||
} | ||
|
||
/* | ||
<ProtocolKey>: 'protocol-id' | ||
*/ | ||
function buildProtocolEntry(protocolKey: string, protocolId: string) { | ||
const key = b.identifier(protocolKey) | ||
const value = b.stringLiteral(protocolId) | ||
|
||
return b.objectProperty(key, value) | ||
} |
127 changes: 127 additions & 0 deletions
127
packages/adapters-library/src/scripts/adapterBuilder/buildIntegrationTests.ts
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,127 @@ | ||
import { promises as fs } from 'node:fs' | ||
import path from 'node:path' | ||
import partition from 'lodash/partition' | ||
import { parse, print, types, visit } from 'recast' | ||
import { lowerFirst } from '../../core/utils/caseConversion' | ||
import { writeAndLintFile } from '../../core/utils/writeAndLintFile' | ||
import { fileExists } from '../utils/fileExists' | ||
import { sortEntries } from '../utils/sortEntries' | ||
import { testCases } from './templates/testCases' | ||
import n = types.namedTypes | ||
import b = types.builders | ||
|
||
/** | ||
* @description Creates a new file for integration tests if it doesn't exist | ||
*/ | ||
export async function buildIntegrationTests({ | ||
protocolId, | ||
protocolKey, | ||
productId, | ||
}: { | ||
protocolId: string | ||
protocolKey: string | ||
productId: string | ||
}) { | ||
const testCasesFilePath = `./packages/adapters-library/src/adapters/${protocolId}/tests/testCases.ts` | ||
|
||
if (await fileExists(testCasesFilePath)) { | ||
return | ||
} | ||
|
||
await writeAndLintFile(testCasesFilePath, testCases(productId)) | ||
|
||
const testsFile = path.resolve( | ||
'./packages/adapters-library/src/adapters/integration.test.ts', | ||
) | ||
const contents = await fs.readFile(testsFile, 'utf-8') | ||
const ast = parse(contents, { | ||
parser: require('recast/parsers/typescript'), | ||
}) | ||
|
||
visit(ast, { | ||
visitProgram(path) { | ||
const programNode = path.value as n.Program | ||
|
||
addTestCasesImport(programNode, protocolId, protocolKey) | ||
|
||
this.traverse(path) | ||
}, | ||
visitVariableDeclarator(path) { | ||
const node = path.node | ||
if (!n.Identifier.check(node.id)) { | ||
return false | ||
} | ||
|
||
if ( | ||
node.id.name === 'protocolTestCases' && | ||
n.ObjectExpression.check(node.init) | ||
) { | ||
if ( | ||
node.init.properties.some( | ||
(property) => | ||
n.ObjectProperty.check(property) && | ||
n.MemberExpression.check(property.key) && | ||
n.Identifier.check(property.key.property) && | ||
property.key.property.name === protocolKey, | ||
) | ||
) { | ||
return false | ||
} | ||
|
||
const newEntry = b.objectProperty( | ||
b.memberExpression( | ||
b.identifier('Protocol'), | ||
b.identifier(protocolKey), | ||
), | ||
b.identifier(`${lowerFirst(protocolKey)}TestCases`), | ||
) | ||
newEntry.computed = true | ||
|
||
node.init.properties.push(newEntry) | ||
|
||
sortEntries( | ||
node.init.properties, | ||
(entry) => ((entry as n.ObjectProperty).value as n.Identifier).name, | ||
) | ||
} | ||
|
||
this.traverse(path) | ||
}, | ||
}) | ||
|
||
await writeAndLintFile(testsFile, print(ast).code) | ||
} | ||
|
||
/** | ||
* @description Adds a new entry to the imports for the test cases | ||
* | ||
* @param programNode AST node for the Protocol program | ||
*/ | ||
function addTestCasesImport( | ||
programNode: n.Program, | ||
protocolId: string, | ||
protocolKey: string, | ||
) { | ||
const [importNodes, codeAfterImports] = partition(programNode.body, (node) => | ||
n.ImportDeclaration.check(node), | ||
) | ||
|
||
const newImportEntry = buildImportTestCasesEntry(protocolId, protocolKey) | ||
|
||
programNode.body = [...importNodes, newImportEntry, ...codeAfterImports] | ||
} | ||
|
||
/* | ||
import { testCases as <protocolId>TestCases } from './<protocolId>/tests/testCases' | ||
*/ | ||
function buildImportTestCasesEntry(protocolId: string, protocolKey: string) { | ||
return b.importDeclaration( | ||
[ | ||
b.importSpecifier( | ||
b.identifier('testCases'), | ||
b.identifier(`${lowerFirst(protocolKey)}TestCases`), | ||
), | ||
], | ||
b.literal(`./${protocolId}/tests/testCases`), | ||
) | ||
} |
18 changes: 8 additions & 10 deletions
18
...apters-library/src/scripts/copyAdapter.ts → ...src/scripts/adapterBuilder/copyAdapter.ts
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
Oops, something went wrong.