@graphprotocol/graph-cli 0.37.4 → 0.37.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/bin.js +6 -0
  3. package/dist/cli.js +26 -0
  4. package/dist/codegen/schema.js +244 -0
  5. package/dist/codegen/schema.test.js +455 -0
  6. package/dist/codegen/template.js +66 -0
  7. package/dist/codegen/types/conversions.js +335 -0
  8. package/dist/codegen/types/index.js +71 -0
  9. package/dist/codegen/types/index.test.js +563 -0
  10. package/dist/codegen/typescript.js +200 -0
  11. package/dist/codegen/util.js +40 -0
  12. package/dist/codegen/util.test.js +93 -0
  13. package/dist/command-helpers/abi.js +76 -0
  14. package/dist/command-helpers/auth.js +76 -0
  15. package/dist/command-helpers/compiler.js +66 -0
  16. package/dist/command-helpers/data-sources.js +29 -0
  17. package/dist/command-helpers/fs.js +9 -0
  18. package/dist/command-helpers/gluegun.js +48 -0
  19. package/dist/command-helpers/ipfs.js +5 -0
  20. package/dist/command-helpers/jsonrpc.js +27 -0
  21. package/dist/command-helpers/network.js +90 -0
  22. package/dist/command-helpers/network.test.js +86 -0
  23. package/dist/command-helpers/node.js +40 -0
  24. package/dist/command-helpers/scaffold.js +110 -0
  25. package/dist/command-helpers/spinner.js +80 -0
  26. package/dist/command-helpers/studio.js +14 -0
  27. package/dist/command-helpers/studio.test.js +41 -0
  28. package/dist/command-helpers/subgraph.js +22 -0
  29. package/dist/command-helpers/version.js +66 -0
  30. package/dist/command-helpers/version.test.js +186 -0
  31. package/dist/commands/add.js +187 -0
  32. package/dist/commands/auth.js +122 -0
  33. package/dist/commands/build.js +136 -0
  34. package/dist/commands/codegen.js +128 -0
  35. package/dist/commands/create.js +94 -0
  36. package/dist/commands/deploy.js +335 -0
  37. package/dist/commands/init.js +800 -0
  38. package/dist/commands/local.js +380 -0
  39. package/dist/commands/remove.js +95 -0
  40. package/dist/commands/test.js +293 -0
  41. package/dist/compiler/asc.js +83 -0
  42. package/dist/compiler/index.js +466 -0
  43. package/dist/debug.js +16 -0
  44. package/dist/migrations/mapping_api_version_0_0_1.js +83 -0
  45. package/dist/migrations/mapping_api_version_0_0_2.js +83 -0
  46. package/dist/migrations/mapping_api_version_0_0_3.js +83 -0
  47. package/dist/migrations/mapping_api_version_0_0_4.js +85 -0
  48. package/dist/migrations/mapping_api_version_0_0_5.js +85 -0
  49. package/dist/migrations/spec_version_0_0_2.js +45 -0
  50. package/dist/migrations/spec_version_0_0_3.js +50 -0
  51. package/dist/migrations/util/load-manifest.js +16 -0
  52. package/dist/migrations/util/versions.js +28 -0
  53. package/dist/migrations.js +54 -0
  54. package/dist/protocols/arweave/manifest.graphql +57 -0
  55. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  56. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  57. package/dist/protocols/arweave/subgraph.js +23 -0
  58. package/dist/protocols/contract.js +2 -0
  59. package/dist/protocols/cosmos/manifest.graphql +76 -0
  60. package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
  61. package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
  62. package/dist/protocols/cosmos/subgraph.js +28 -0
  63. package/dist/protocols/ethereum/abi.js +138 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +474 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +325 -0
  66. package/dist/protocols/ethereum/codegen/template.js +52 -0
  67. package/dist/protocols/ethereum/contract.js +22 -0
  68. package/dist/protocols/ethereum/manifest.graphql +94 -0
  69. package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
  70. package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
  71. package/dist/protocols/ethereum/subgraph.js +172 -0
  72. package/dist/protocols/ethereum/type-generator.js +123 -0
  73. package/dist/protocols/index.js +266 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +53 -0
  75. package/dist/protocols/near/contract.js +41 -0
  76. package/dist/protocols/near/manifest.graphql +64 -0
  77. package/dist/protocols/near/scaffold/manifest.js +16 -0
  78. package/dist/protocols/near/scaffold/mapping.js +38 -0
  79. package/dist/protocols/near/subgraph.js +23 -0
  80. package/dist/protocols/subgraph.js +2 -0
  81. package/dist/protocols/substreams/manifest.graphql +45 -0
  82. package/dist/protocols/substreams/scaffold/manifest.js +12 -0
  83. package/dist/protocols/substreams/subgraph.js +23 -0
  84. package/dist/scaffold/cosmos.test.js +82 -0
  85. package/dist/scaffold/ethereum.test.js +496 -0
  86. package/dist/scaffold/index.js +133 -0
  87. package/dist/scaffold/mapping.js +57 -0
  88. package/dist/scaffold/near.test.js +85 -0
  89. package/dist/scaffold/schema.js +86 -0
  90. package/dist/scaffold/tests.js +179 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +243 -0
  93. package/dist/type-generator.js +225 -0
  94. package/dist/validation/contract.js +44 -0
  95. package/dist/validation/index.js +10 -0
  96. package/dist/validation/manifest.js +266 -0
  97. package/dist/validation/schema.js +768 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/package.json +1 -1
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.updateNetworksFile = exports.initNetworksConfig = exports.updateSubgraphNetwork = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const yaml_1 = __importDefault(require("yaml"));
9
+ const spinner_1 = require("./spinner");
10
+ const updateSubgraphNetwork = async (toolbox, manifest, network, networksFile, identifierName) => await (0, spinner_1.withSpinner)(`Update sources network`, `Failed to update sources network`, `Warnings while updating sources network`, async (spinner) => {
11
+ (0, spinner_1.step)(spinner, `Reading networks config`);
12
+ const allNetworks = await toolbox.filesystem.read(networksFile, 'json');
13
+ const networkConfig = allNetworks[network];
14
+ // Exit if the network passed with --network does not exits in networks.json
15
+ if (!networkConfig) {
16
+ throw new Error(`Network '${network}' was not found in '${networksFile}'`);
17
+ }
18
+ await toolbox.patching.update(manifest, content => {
19
+ const subgraph = yaml_1.default.parse(content);
20
+ const networkSources = Object.keys(networkConfig);
21
+ const subgraphSources = subgraph.dataSources.map((value) => value.name);
22
+ // Update the dataSources network config
23
+ subgraph.dataSources = subgraph.dataSources.map((source) => {
24
+ if (!networkSources.includes(source.name)) {
25
+ throw new Error(`'${source.name}' was not found in the '${network}' configuration, please update!`);
26
+ }
27
+ if (hasChanges(identifierName, network, networkConfig[source.name], source)) {
28
+ (0, spinner_1.step)(spinner, `Update '${source.name}' network configuration`);
29
+ source.network = network;
30
+ source.source = source.source.abi ? { abi: source.source.abi } : {};
31
+ Object.assign(source.source, networkConfig[source.name]);
32
+ }
33
+ else {
34
+ (0, spinner_1.step)(spinner, `Skip '${source.name}': No changes to network configuration`);
35
+ }
36
+ return source;
37
+ });
38
+ // All data sources shoud be on the same network,
39
+ // so we have to update the network of all templates too.
40
+ subgraph.templates &&= subgraph.templates.map((template) => ({
41
+ ...template,
42
+ network,
43
+ }));
44
+ const unsusedSources = networkSources.filter(x => !subgraphSources.includes(x));
45
+ unsusedSources.forEach(source => {
46
+ (0, spinner_1.step)(spinner, `dataSource '${source}' from '${networksFile}' not found in ${manifest}`);
47
+ });
48
+ const yaml_doc = new yaml_1.default.Document();
49
+ yaml_doc.contents = subgraph;
50
+ return yaml_doc.toString();
51
+ });
52
+ });
53
+ exports.updateSubgraphNetwork = updateSubgraphNetwork;
54
+ const initNetworksConfig = async (toolbox, directory, identifierName) => await (0, spinner_1.withSpinner)(`Initialize networks config`, `Failed to initialize networks config`, `Warnings while initializing networks config`, async () => {
55
+ const subgraphStr = toolbox.filesystem.read(path_1.default.join(directory, 'subgraph.yaml'));
56
+ const subgraph = yaml_1.default.parse(subgraphStr);
57
+ const networks = subgraph.dataSources.reduce((acc, source) => Object.assign(acc, {
58
+ [source.network]: {
59
+ [source.name]: {
60
+ [identifierName]: source.source.address,
61
+ startBlock: source.source.startBlock,
62
+ },
63
+ },
64
+ }), {});
65
+ toolbox.filesystem.write(`${directory}/networks.json`, networks);
66
+ return true;
67
+ });
68
+ exports.initNetworksConfig = initNetworksConfig;
69
+ // Checks if any network attribute has been changed
70
+ function hasChanges(identifierName, network, networkConfig, dataSource) {
71
+ const networkChanged = dataSource.network !== network;
72
+ // Return directly if the network is different
73
+ if (networkChanged)
74
+ return networkChanged;
75
+ const addressChanged = networkConfig[identifierName] !== dataSource.source[identifierName];
76
+ const startBlockChanged = networkConfig.startBlock !== dataSource.source.startBlock;
77
+ return networkChanged || addressChanged || startBlockChanged;
78
+ }
79
+ const updateNetworksFile = async (toolbox, network, dataSource, address, networksFile) => {
80
+ await toolbox.patching.update(networksFile, config => {
81
+ if (Object.keys(config).includes(network)) {
82
+ Object.assign(config[network], { [dataSource]: { address } });
83
+ }
84
+ else {
85
+ Object.assign(config, { [network]: { [dataSource]: { address } } });
86
+ }
87
+ return config;
88
+ });
89
+ };
90
+ exports.updateNetworksFile = updateNetworksFile;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const path_1 = __importDefault(require("path"));
30
+ const toolbox = __importStar(require("gluegun"));
31
+ const yaml_1 = __importDefault(require("yaml"));
32
+ const network_1 = require("./network");
33
+ const SUBGRAPH_PATH_BASE = path_1.default.join(__dirname, '..', '..', '..', '..', 'examples', 'example-subgraph');
34
+ describe('initNetworksConfig', () => {
35
+ beforeAll(async () => {
36
+ await (0, network_1.initNetworksConfig)(toolbox, SUBGRAPH_PATH_BASE, 'address');
37
+ });
38
+ afterAll(async () => {
39
+ toolbox.filesystem.remove(`${SUBGRAPH_PATH_BASE}/networks.json`);
40
+ });
41
+ test('generates networks.json from subgraph.yaml', () => {
42
+ expect(toolbox.filesystem.exists(`${SUBGRAPH_PATH_BASE}/networks.json`)).toBe('file');
43
+ });
44
+ test('Populates the networks.json file with the data from subgraph.yaml', async () => {
45
+ const networksStr = toolbox.filesystem.read(`${SUBGRAPH_PATH_BASE}/networks.json`);
46
+ const networks = JSON.parse(networksStr);
47
+ const expected = {
48
+ mainnet: {
49
+ ExampleSubgraph: { address: '0x22843e74c59580b3eaf6c233fa67d8b7c561a835' },
50
+ },
51
+ };
52
+ expect(networks).toStrictEqual(expected);
53
+ });
54
+ });
55
+ describe('updateSubgraphNetwork', () => {
56
+ beforeAll(async () => {
57
+ const content = {
58
+ optimism: {
59
+ ExampleSubgraph: { address: '0x12345...' },
60
+ },
61
+ };
62
+ toolbox.filesystem.write(`${SUBGRAPH_PATH_BASE}/networks.json`, content);
63
+ toolbox.filesystem.copy(`${SUBGRAPH_PATH_BASE}/subgraph.yaml`, `${SUBGRAPH_PATH_BASE}/subgraph_copy.yaml`);
64
+ });
65
+ afterAll(async () => {
66
+ toolbox.filesystem.remove(`${SUBGRAPH_PATH_BASE}/networks.json`);
67
+ toolbox.filesystem.remove(`${SUBGRAPH_PATH_BASE}/subgraph_copy.yaml`);
68
+ });
69
+ test('Updates subgraph.yaml', async () => {
70
+ const manifest = `${SUBGRAPH_PATH_BASE}/subgraph_copy.yaml`;
71
+ const networksFie = `${SUBGRAPH_PATH_BASE}/networks.json`;
72
+ let subgraph = toolbox.filesystem.read(manifest);
73
+ let subgraphObj = yaml_1.default.parse(subgraph);
74
+ let network = subgraphObj.dataSources[0].network;
75
+ let address = subgraphObj.dataSources[0].source.address;
76
+ expect(network).toBe('mainnet');
77
+ expect(address).toBe('0x22843e74c59580b3eaf6c233fa67d8b7c561a835');
78
+ await (0, network_1.updateSubgraphNetwork)(toolbox, manifest, 'optimism', networksFie, 'address');
79
+ subgraph = toolbox.filesystem.read(manifest);
80
+ subgraphObj = yaml_1.default.parse(subgraph);
81
+ network = subgraphObj.dataSources[0].network;
82
+ address = subgraphObj.dataSources[0].source.address;
83
+ expect(network).toBe('optimism');
84
+ expect(address).toBe('0x12345...');
85
+ });
86
+ });
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.chooseNodeUrl = exports.normalizeNodeUrl = exports.validateNodeUrl = void 0;
4
+ const url_1 = require("url");
5
+ const gluegun_1 = require("gluegun");
6
+ const SUBGRAPH_STUDIO_URL = 'https://api.studio.thegraph.com/deploy/';
7
+ const HOSTED_SERVICE_URL = 'https://api.thegraph.com/deploy/';
8
+ const validateNodeUrl = (node) => new url_1.URL(node);
9
+ exports.validateNodeUrl = validateNodeUrl;
10
+ const normalizeNodeUrl = (node) => new url_1.URL(node).toString();
11
+ exports.normalizeNodeUrl = normalizeNodeUrl;
12
+ function chooseNodeUrl({ product, studio, node, allowSimpleName, }) {
13
+ if (node) {
14
+ try {
15
+ (0, exports.validateNodeUrl)(node);
16
+ }
17
+ catch (e) {
18
+ gluegun_1.print.error(`Graph node "${node}" is invalid: ${e.message}`);
19
+ process.exit(1);
20
+ }
21
+ }
22
+ else {
23
+ if (studio) {
24
+ product = 'subgraph-studio';
25
+ }
26
+ switch (product) {
27
+ case 'subgraph-studio':
28
+ node = SUBGRAPH_STUDIO_URL;
29
+ break;
30
+ case 'hosted-service':
31
+ node = HOSTED_SERVICE_URL;
32
+ break;
33
+ }
34
+ }
35
+ if (node?.match(/studio/) || product === 'subgraph-studio') {
36
+ allowSimpleName = true;
37
+ }
38
+ return { node, allowSimpleName };
39
+ }
40
+ exports.chooseNodeUrl = chooseNodeUrl;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.writeTestsFiles = exports.writeMapping = exports.writeSchema = exports.writeABI = exports.writeScaffold = exports.generateScaffold = exports.generateDataSource = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const gluegun_1 = require("gluegun");
10
+ const immutable_1 = require("immutable");
11
+ const prettier_1 = __importDefault(require("prettier"));
12
+ const yaml_1 = __importDefault(require("yaml"));
13
+ const scaffold_1 = __importDefault(require("../scaffold"));
14
+ const mapping_1 = require("../scaffold/mapping");
15
+ const schema_1 = require("../scaffold/schema");
16
+ const tests_1 = require("../scaffold/tests");
17
+ const spinner_1 = require("./spinner");
18
+ const generateDataSource = async (protocol, contractName, network, contractAddress, abi) => {
19
+ const protocolManifest = protocol.getManifestScaffold();
20
+ return immutable_1.Map.of('kind', protocol.name, 'name', contractName, 'network', network, 'source', yaml_1.default.parse(prettier_1.default.format(protocolManifest.source({ contract: contractAddress, contractName }), {
21
+ parser: 'yaml',
22
+ })), 'mapping', yaml_1.default.parse(prettier_1.default.format(protocolManifest.mapping({ abi, contractName }), {
23
+ parser: 'yaml',
24
+ }))).asMutable();
25
+ };
26
+ exports.generateDataSource = generateDataSource;
27
+ const generateScaffold = async ({ protocolInstance, abi, contract, network, subgraphName, indexEvents, contractName = 'Contract', node, }, spinner) => {
28
+ (0, spinner_1.step)(spinner, 'Generate subgraph');
29
+ const scaffold = new scaffold_1.default({
30
+ protocol: protocolInstance,
31
+ abi,
32
+ indexEvents,
33
+ contract,
34
+ network,
35
+ contractName,
36
+ subgraphName,
37
+ node,
38
+ });
39
+ return scaffold.generate();
40
+ };
41
+ exports.generateScaffold = generateScaffold;
42
+ const writeScaffoldDirectory = async (scaffold, directory, spinner) => {
43
+ // Create directory itself
44
+ await fs_extra_1.default.mkdirs(directory);
45
+ const promises = Object.keys(scaffold).map(async (basename) => {
46
+ const content = scaffold[basename];
47
+ const filename = path_1.default.join(directory, basename);
48
+ // Write file or recurse into subdirectory
49
+ if (typeof content === 'string') {
50
+ await fs_extra_1.default.writeFile(filename, content, 'utf-8');
51
+ }
52
+ else if (content == null) {
53
+ return; // continue loop
54
+ }
55
+ else {
56
+ writeScaffoldDirectory(content, path_1.default.join(directory, basename), spinner);
57
+ }
58
+ });
59
+ await Promise.all(promises);
60
+ };
61
+ const writeScaffold = async (scaffold, directory, spinner) => {
62
+ (0, spinner_1.step)(spinner, `Write subgraph to directory`);
63
+ await writeScaffoldDirectory(scaffold, directory, spinner);
64
+ };
65
+ exports.writeScaffold = writeScaffold;
66
+ const writeABI = async (abi, contractName) => {
67
+ const data = prettier_1.default.format(JSON.stringify(abi.data), {
68
+ parser: 'json',
69
+ });
70
+ await fs_extra_1.default.writeFile(`./abis/${contractName}.json`, data, 'utf-8');
71
+ };
72
+ exports.writeABI = writeABI;
73
+ const writeSchema = async (abi, protocol, schemaPath, entities) => {
74
+ const events = protocol.hasEvents()
75
+ ? (0, schema_1.abiEvents)(abi)
76
+ .filter(event => !entities.includes(event.get('name')))
77
+ .toJS()
78
+ : [];
79
+ const data = prettier_1.default.format(events.map(event => (0, schema_1.generateEventType)(event, protocol.name)).join('\n\n'), {
80
+ parser: 'graphql',
81
+ });
82
+ await fs_extra_1.default.appendFile(schemaPath, data, { encoding: 'utf-8' });
83
+ };
84
+ exports.writeSchema = writeSchema;
85
+ const writeMapping = async (abi, protocol, contractName, entities) => {
86
+ const events = protocol.hasEvents()
87
+ ? (0, schema_1.abiEvents)(abi)
88
+ .filter(event => !entities.includes(event.get('name')))
89
+ .toJS()
90
+ : [];
91
+ const mapping = prettier_1.default.format((0, mapping_1.generateEventIndexingHandlers)(events, contractName), {
92
+ parser: 'typescript',
93
+ semi: false,
94
+ });
95
+ await fs_extra_1.default.writeFile(`./src/${gluegun_1.strings.kebabCase(contractName)}.ts`, mapping, 'utf-8');
96
+ };
97
+ exports.writeMapping = writeMapping;
98
+ const writeTestsFiles = async (abi, protocol, contractName) => {
99
+ const hasEvents = protocol.hasEvents();
100
+ const events = hasEvents ? (0, schema_1.abiEvents)(abi).toJS() : [];
101
+ if (events.length > 0) {
102
+ // If a contract is added to a subgraph that has no tests folder
103
+ await fs_extra_1.default.ensureDir('./tests/');
104
+ const testsFiles = (0, tests_1.generateTestsFiles)(contractName, events, true);
105
+ for (const [fileName, content] of Object.entries(testsFiles)) {
106
+ await fs_extra_1.default.writeFile(`./tests/${fileName}`, content, 'utf-8');
107
+ }
108
+ }
109
+ };
110
+ exports.writeTestsFiles = writeTestsFiles;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.withSpinner = exports.step = void 0;
27
+ const toolbox = __importStar(require("gluegun"));
28
+ const step = (spinner, subject, text) => {
29
+ if (text) {
30
+ spinner.stopAndPersist({
31
+ text: toolbox.print.colors.muted(`${subject} ${text}`),
32
+ });
33
+ }
34
+ else {
35
+ spinner.stopAndPersist({ text: toolbox.print.colors.muted(subject) });
36
+ }
37
+ spinner.start();
38
+ return spinner;
39
+ };
40
+ exports.step = step;
41
+ // Executes the function `f` in a command-line spinner, using the
42
+ // provided captions for in-progress, error and failed messages.
43
+ //
44
+ // If `f` throws an error, the spinner stops with the failure message
45
+ // and rethrows the error.
46
+ // If `f` returns an object with a `warning` and a `result` key, the
47
+ // spinner stops with the warning message and returns the `result` value.
48
+ // Otherwise the spinner prints the in-progress message with a check mark
49
+ // and simply returns the value returned by `f`.
50
+ const withSpinner = async (text, errorText, warningText, f) => {
51
+ const spinner = toolbox.print.spin(text);
52
+ try {
53
+ const result = await f(spinner);
54
+ if (typeof result === 'object') {
55
+ const hasError = Object.keys(result).includes('error');
56
+ const hasWarning = Object.keys(result).includes('warning');
57
+ const hasResult = Object.keys(result).includes('result');
58
+ if (hasError) {
59
+ spinner.fail(`${errorText}: ${result.error}`);
60
+ return hasResult ? result.result : result;
61
+ }
62
+ if (hasWarning && hasResult) {
63
+ if (result.warning !== null) {
64
+ spinner.warn(`${warningText}: ${result.warning}`);
65
+ }
66
+ spinner.succeed(text);
67
+ return result.result;
68
+ }
69
+ spinner.succeed(text);
70
+ return result;
71
+ }
72
+ spinner.succeed(text);
73
+ return result;
74
+ }
75
+ catch (e) {
76
+ spinner.fail(`${errorText}: ${e.message}`);
77
+ throw e;
78
+ }
79
+ };
80
+ exports.withSpinner = withSpinner;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateStudioNetwork = exports.allowedStudioNetworks = void 0;
4
+ exports.allowedStudioNetworks = ['mainnet', 'rinkeby', 'goerli', 'gnosis'];
5
+ const validateStudioNetwork = ({ studio, product, network, }) => {
6
+ const isStudio = studio || product === 'subgraph-studio';
7
+ const isAllowedNetwork = exports.allowedStudioNetworks.includes(
8
+ // @ts-expect-error we're checking if the network is allowed
9
+ network);
10
+ if (isStudio && !isAllowedNetwork) {
11
+ throw new Error(`The Subgraph Studio only allows subgraphs for these networks: ${exports.allowedStudioNetworks.join(', ')}`);
12
+ }
13
+ };
14
+ exports.validateStudioNetwork = validateStudioNetwork;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const studio_1 = require("./studio");
4
+ describe('Version Command Helpers', () => {
5
+ describe('validateStudioNetwork', () => {
6
+ describe("When it's studio", () => {
7
+ test("And it's Ethereum mainnet", () => {
8
+ expect(() => (0, studio_1.validateStudioNetwork)({
9
+ studio: true,
10
+ network: 'mainnet',
11
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
12
+ });
13
+ test("And it's Gnosis chain", () => {
14
+ expect(() => (0, studio_1.validateStudioNetwork)({
15
+ studio: true,
16
+ network: 'gnosis',
17
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
18
+ });
19
+ test("And it's NOT an allowed network", () => {
20
+ expect(() => (0, studio_1.validateStudioNetwork)({
21
+ product: 'subgraph-studio',
22
+ network: 'celo',
23
+ })).toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
24
+ });
25
+ });
26
+ describe("When it's NOT studio", () => {
27
+ test("And it's Rinkeby", () => {
28
+ expect(() => (0, studio_1.validateStudioNetwork)({
29
+ studio: false,
30
+ network: 'rinkeby',
31
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
32
+ });
33
+ test("And it's NOT an allowed network", () => {
34
+ expect(() => (0, studio_1.validateStudioNetwork)({
35
+ product: 'hosted-service',
36
+ network: 'celo',
37
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
38
+ });
39
+ });
40
+ });
41
+ });
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSubgraphBasename = exports.validateSubgraphName = void 0;
4
+ const validateSubgraphName = (name, { allowSimpleName }) => {
5
+ if (allowSimpleName) {
6
+ return name;
7
+ }
8
+ if (name.split('/').length !== 2) {
9
+ throw new Error(`Subgraph name "${name}" needs to have the format "<PREFIX>/${name}".
10
+
11
+ When using the Hosted Service at https://thegraph.com, <PREFIX> is the
12
+ name of your GitHub user or organization.
13
+
14
+ You can bypass this check with --allow-simple-name.`);
15
+ }
16
+ };
17
+ exports.validateSubgraphName = validateSubgraphName;
18
+ const getSubgraphBasename = (name) => {
19
+ const segments = name.split('/', 2);
20
+ return segments[segments.length - 1];
21
+ };
22
+ exports.getSubgraphBasename = getSubgraphBasename;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.assertGraphTsVersion = exports.assertManifestApiVersion = void 0;
30
+ const semver_1 = __importDefault(require("semver"));
31
+ const manifestUtil = __importStar(require("../migrations/util/load-manifest"));
32
+ const graphTsUtil = __importStar(require("../migrations/util/versions"));
33
+ const assertManifestApiVersion = async (manifestPath, minimumApiVersion) => {
34
+ const manifest = await manifestUtil.loadManifest(manifestPath);
35
+ const lessThanMinimumVersion = (manifestApiVersion) => semver_1.default.lt(manifestApiVersion, minimumApiVersion);
36
+ let isLessThanMinimumVersion = false;
37
+ if (manifest) {
38
+ if (manifest.dataSources && Array.isArray(manifest.dataSources)) {
39
+ isLessThanMinimumVersion = manifest.dataSources.some((dataSource) => dataSource?.mapping?.apiVersion && lessThanMinimumVersion(dataSource.mapping.apiVersion));
40
+ }
41
+ if (manifest.templates && Array.isArray(manifest.templates)) {
42
+ isLessThanMinimumVersion ||= manifest.templates.some((template) => template?.mapping?.apiVersion && lessThanMinimumVersion(template.mapping.apiVersion));
43
+ }
44
+ }
45
+ if (isLessThanMinimumVersion) {
46
+ throw new Error(`The current version of graph-cli can't be used with mappings on apiVersion less than '${minimumApiVersion}'`);
47
+ }
48
+ };
49
+ exports.assertManifestApiVersion = assertManifestApiVersion;
50
+ const assertGraphTsVersion = async (sourceDir, minimumGraphTsVersion) => {
51
+ let graphTsVersion;
52
+ try {
53
+ graphTsVersion = await graphTsUtil.getGraphTsVersion(sourceDir);
54
+ }
55
+ catch (_) {
56
+ // We only do the assertion if `graph-ts` is installed.
57
+ }
58
+ // Coerce needed because we may be dealing with an alpha version
59
+ // and in the `semver` library, this would return true when comparing
60
+ // the same version.
61
+ if (graphTsVersion && semver_1.default.lt(semver_1.default.coerce(graphTsVersion), minimumGraphTsVersion)) {
62
+ throw new Error(`To use this version of the graph-cli you must upgrade the graph-ts dependency to a version greater than or equal to ${minimumGraphTsVersion}
63
+ Also, you'll probably need to take a look at our AssemblyScript migration guide because of language breaking changes: https://thegraph.com/docs/developer/assemblyscript-migration-guide`);
64
+ }
65
+ };
66
+ exports.assertGraphTsVersion = assertGraphTsVersion;