Skip to content
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

Feature | graph init Add support to download spkg files from spkg.io #1642

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1541c32
src/command-helpers: Add priority sort function
joshuanazareth97 Apr 16, 2024
286a778
src/command-helpers: Add priority sort tests
joshuanazareth97 Apr 16, 2024
dbc2f94
src/commands/init: Sort imports
joshuanazareth97 Apr 16, 2024
70b799a
src/commands/init: sort network choices before showing them to user
joshuanazareth97 Apr 16, 2024
dd909e9
remove jsdoc
joshuanazareth97 Apr 16, 2024
dba1fa3
src/command-helpers: improve priority sort JSDoc
joshuanazareth97 Apr 16, 2024
4d63078
Add changeset file
joshuanazareth97 Apr 16, 2024
2e6448a
Fix formatting
joshuanazareth97 Apr 16, 2024
eb840d7
Add file donwload utility
joshuanazareth97 Apr 26, 2024
d995b1b
Add url processing logic
joshuanazareth97 Apr 26, 2024
cddafba
Add spkg validation and processing utils
joshuanazareth97 Apr 26, 2024
e4fdb3c
Modify spkg prompt
joshuanazareth97 Apr 26, 2024
74e2721
Add imports
joshuanazareth97 Apr 26, 2024
1d62121
Merge branch 'main' of github.com:graphprotocol/graph-tooling into fe…
joshuanazareth97 Apr 26, 2024
908438c
Fix formatting error
joshuanazareth97 Apr 26, 2024
461a3cd
Add changeset file
joshuanazareth97 Apr 26, 2024
239e528
Update .changeset
joshuanazareth97 May 9, 2024
8e13155
Update spkg url validity check
joshuanazareth97 May 15, 2024
5c7ac2d
Merge branch 'feature/graph-init-spkg-io-support' of github.com:joshu…
joshuanazareth97 May 15, 2024
91e488c
Merge branch 'main' of github.com:graphprotocol/graph-tooling into fe…
joshuanazareth97 May 15, 2024
7d24759
src/command-helpers/download: Update download to use custom fetchwrapper
joshuanazareth97 May 15, 2024
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
5 changes: 5 additions & 0 deletions .changeset/early-walls-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

Order list of evm chains in graph init command
joshuanazareth97 marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions packages/cli/src/command-helpers/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createWriteStream } from 'fs';
import { http } from 'gluegun';

export async function downloadFile(fileUrl: string, outputLocationPath: string) {
const writer = createWriteStream(outputLocationPath);
const api = http.create({
baseURL: fileUrl,
});
return api.get('', {}, { responseType: 'stream' }).then((response: any) => {
response.data.pipe(writer);
return Promise.resolve(outputLocationPath);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use fetch, we maintain an internal wrapper that also passes some headers that can let origin servers know where the traffic is coming from.

export default function fetchWrapper(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have now resolved this to use the wrapped fetch

41 changes: 41 additions & 0 deletions packages/cli/src/command-helpers/spkg.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from 'path';
import { describe, expect, it } from 'vitest';
import { getSpkgFilePath, isSpkgUrl } from '../../src/command-helpers/spkg';

describe('getSpkgFilePath', () => {
it('should return the correct file path', () => {
const spkgUrl = 'https://example.com/package.spkg';
const directory = '/home/testuser/development/testgraph';
const expectedFilePath = path.join(directory, 'package.spkg');

const filePath = getSpkgFilePath(spkgUrl, directory);

expect(filePath).to.equal(expectedFilePath);
});

it('should throw an error for invalid spkg url', () => {
const spkgUrl = '';
const directory = '/home/joshua/development/graphprotocol/graph-tooling/packages/cli';

expect(() => getSpkgFilePath(spkgUrl, directory)).to.throw('Invalid spkg url');
});
});

describe('isSpkgUrl', () => {
it('should return true for valid spkg url', () => {
const spkgUrl = 'https://spkg.io/streamingfast/package.spkg';
const result = isSpkgUrl(spkgUrl);
expect(result).toBe(true);
});

it('should return false for invalid spkg url', () => {
const spkgUrl = 'https://example.com/package.spkg';
const result = isSpkgUrl(spkgUrl);
expect(result).toBe(false);
});
it('should return false for non-url string', () => {
const spkgUrl = 'streamingfast/package.spkg';
const result = isSpkgUrl(spkgUrl);
expect(result).toBe(false);
});
});
18 changes: 18 additions & 0 deletions packages/cli/src/command-helpers/spkg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// validator that checks if spkg exists or is valid url

import path from 'path';
import { filesystem } from 'gluegun';

export const isSpkgUrl = (value: string) => {
return value.startsWith('https://spkg.io/streamingfast');
joshuanazareth97 marked this conversation as resolved.
Show resolved Hide resolved
};

export const validateSpkg = (value: string) => {
return filesystem.exists(value) || isSpkgUrl(value);
};

export const getSpkgFilePath = (spkgUrl: string, directory: string) => {
const spkgFileName = spkgUrl.split('/').pop();
if (!spkgFileName) throw new Error('Invalid spkg url');
return path.join(directory, spkgFileName);
};
22 changes: 18 additions & 4 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
loadAbiFromEtherscan,
loadStartBlockForContract,
} from '../command-helpers/abi';
import { downloadFile } from '../command-helpers/download';
import { initNetworksConfig } from '../command-helpers/network';
import { chooseNodeUrl, SUBGRAPH_STUDIO_URL } from '../command-helpers/node';
import { generateScaffold, writeScaffold } from '../command-helpers/scaffold';
import { sortWithPriority } from '../command-helpers/sort';
import { withSpinner } from '../command-helpers/spinner';
import { getSpkgFilePath, isSpkgUrl, validateSpkg } from '../command-helpers/spkg';
import { getSubgraphBasename, validateSubgraphName } from '../command-helpers/subgraph';
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
import debugFactory from '../debug';
Expand Down Expand Up @@ -726,11 +728,10 @@
{
type: 'input',
name: 'spkg',
message: 'SPKG file (path)',
message: 'SPKG file (path or spkg.io url)',
initial: () => initSpkgPath,
skip: () => !isSubstreams || !!initSpkgPath,
validate: value =>
filesystem.exists(initSpkgPath || value) ? true : 'SPKG file does not exist',
validate: value => validateSpkg(value) || 'Invalid SPKG file',
},
]);

Expand Down Expand Up @@ -1206,6 +1207,16 @@
this.error(`ABI does not contain any events`, { exit: 1 });
}

let spkgFilePath = spkgPath;
// if url let's overwrite with the file path
if (spkgPath && isSpkgUrl(spkgPath)) {
try {
spkgFilePath = await getSpkgFilePath(spkgPath, directory);
} catch (e) {
this.error(e.message, { exit: 1 });
}
}

// Scaffold subgraph
const scaffold = await withSpinner(
`Create subgraph scaffold`,
Expand All @@ -1223,11 +1234,14 @@
contractName,
startBlock,
node,
spkgPath,
spkgPath: spkgFilePath,
},
spinner,
);
await writeScaffold(scaffold, directory, spinner);
if (spkgPath && spkgFilePath && isSpkgUrl(spkgPath)) {
await downloadFile(spkgPath, spkgFilePath);
}
return true;
},
);
Expand All @@ -1237,7 +1251,7 @@
}

if (protocolInstance.hasContract()) {
const identifierName = protocolInstance.getContract()!.identifierName();

Check warning on line 1254 in packages/cli/src/commands/init.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
const networkConf = await initNetworksConfig(directory, identifierName);
if (networkConf !== true) {
process.exitCode = 1;
Expand Down Expand Up @@ -1300,7 +1314,7 @@
const addContractConfirmation = addContractAnswer.toLowerCase() === 'y';

if (addContractConfirmation) {
const ProtocolContract = protocolInstance.getContract()!;

Check warning on line 1317 in packages/cli/src/commands/init.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion

let contract = '';
for (;;) {
Expand Down
Loading