@graphprotocol/graph-cli 0.30.2 → 0.30.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.30.2",
3
+ "version": "0.30.3",
4
4
  "license": "(Apache-2.0 OR MIT)",
5
5
  "description": "CLI for building for and deploying to The Graph",
6
6
  "dependencies": {
@@ -79,12 +79,12 @@ const writeScaffold = async (scaffold, directory, spinner) => {
79
79
  await writeScaffoldDirectory(scaffold, directory, spinner)
80
80
  }
81
81
 
82
- const writeABI = async (abi, contractName, abiPath = `./abis/${contractName}.json`) => {
82
+ const writeABI = async (abi, contractName) => {
83
83
  let data = prettier.format(JSON.stringify(abi.data), {
84
84
  parser: 'json',
85
85
  })
86
86
 
87
- await fs.writeFile(abiPath, data, { encoding: 'utf-8' })
87
+ await fs.writeFile(`./abis/${contractName}.json`, data, { encoding: 'utf-8' })
88
88
  }
89
89
 
90
90
  const writeSchema = async (abi, protocol, schemaPath, entities) => {
@@ -95,7 +95,7 @@ module.exports = {
95
95
  let { collisionEntities, onlyCollisions, abiData } = updateEventNamesOnCollision(ethabi, entities, contractName, mergeEntities)
96
96
  ethabi.data = abiData
97
97
 
98
- await writeABI(ethabi, contractName, abi)
98
+ await writeABI(ethabi, contractName)
99
99
  await writeSchema(ethabi, protocol, result.getIn(['schema', 'file']), collisionEntities)
100
100
  await writeMapping(ethabi, protocol, contractName, collisionEntities)
101
101
 
@@ -13,12 +13,12 @@ const HELP = `
13
13
  ${chalk.bold('graph test')} ${chalk.dim('[options]')} ${chalk.bold('<datasource>')}
14
14
 
15
15
  ${chalk.dim('Options:')}
16
- -c, --coverage Run the tests in coverage mode. Works with v0.2.1 and above
16
+ -c, --coverage Run the tests in coverage mode
17
17
  -d, --docker Run the tests in a docker container(Note: Please execute from the root folder of the subgraph)
18
18
  -f --force Binary - overwrites folder + file when downloading. Docker - rebuilds the docker image
19
19
  -h, --help Show usage information
20
20
  -l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
21
- -r, --recompile Force-recompile tests (Not available in 0.2.2 and earlier versions)
21
+ -r, --recompile Force-recompile tests
22
22
  -v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
23
23
  `
24
24
 
@@ -43,16 +43,16 @@ module.exports = {
43
43
  version,
44
44
  } = toolbox.parameters.options
45
45
 
46
- let opts = new Map()
46
+ let opts = {}
47
47
  // Support both long and short option variants
48
- opts.set("coverage", coverage || c)
49
- opts.set("docker", docker || d)
50
- opts.set("force", force || f)
51
- opts.set("help", help || h)
52
- opts.set("logs", logs || l)
53
- opts.set("recompile", recompile || r)
54
- opts.set("version", version || v)
55
-
48
+ opts.coverage = coverage || c
49
+ opts.docker = docker || d
50
+ opts.force = force || f
51
+ opts.help = help || h
52
+ opts.logs = logs || l
53
+ opts.recompile = recompile || r
54
+ opts.version = version || v
55
+ opts.testsFolder = './tests'
56
56
  // Fix if a boolean flag (e.g -c, --coverage) has an argument
57
57
  try {
58
58
  fixParameters(toolbox.parameters, {
@@ -76,18 +76,49 @@ module.exports = {
76
76
  }
77
77
 
78
78
  let datasource = toolbox.parameters.first || toolbox.parameters.array[0]
79
-
80
79
  // Show help text if requested
81
- if (opts.get("help")) {
80
+ if (opts.help) {
82
81
  print.info(HELP)
83
82
  return
84
83
  }
85
84
 
86
- let result = await fetch('https://api.github.com/repos/LimeChain/matchstick/releases/latest')
87
- let json = await result.json()
88
- opts.set("latestVersion", json.tag_name)
85
+ // Check if matchstick.yaml config exists
86
+ if(filesystem.exists('matchstick.yaml')) {
87
+ try {
88
+ // Load the config
89
+ let config = await yaml.load(filesystem.read('matchstick.yaml', 'utf8'))
90
+
91
+ // Check if matchstick.yaml and testsFolder not null
92
+ if(config && config.testsFolder) {
93
+ // assign test folder from matchstick.yaml if present
94
+ opts.testsFolder = config.testsFolder
95
+ }
96
+ } catch (error) {
97
+ print.info('A problem occurred while reading matchstick.yaml. Please attend to the errors below:')
98
+ print.error(error.message)
99
+ process.exit(1)
100
+ }
101
+ }
102
+
103
+ opts.cachePath = path.join(opts.testsFolder, '.latest.json')
104
+ setVersionFromCache(opts)
105
+
106
+ // Fetch the latest version tag if version is not specified with -v/--version or if the version is not cached
107
+ if (opts.force || (!opts.version && !opts.latestVersion)) {
108
+ print.info("Fetching latest version tag")
109
+ let result = await fetch('https://api.github.com/repos/LimeChain/matchstick/releases/latest')
110
+ let json = await result.json()
111
+ opts.latestVersion = json.tag_name
112
+
113
+ filesystem.file(opts.cachePath, {
114
+ content: {
115
+ version: json.tag_name,
116
+ timestamp: Date.now()
117
+ }
118
+ })
119
+ }
89
120
 
90
- if(opts.get("docker")) {
121
+ if(opts.docker) {
91
122
  runDocker(datasource, opts)
92
123
  } else {
93
124
  runBinary(datasource, opts)
@@ -95,13 +126,25 @@ module.exports = {
95
126
  }
96
127
  }
97
128
 
129
+ async function setVersionFromCache(opts) {
130
+ if(filesystem.exists(opts.cachePath) == 'file') {
131
+ let cached = filesystem.read(opts.cachePath, 'json')
132
+ // Get the cache age in days
133
+ let cacheAge = (Date.now() - cached.timestamp) / (1000 * 60 * 60 * 24)
134
+ // If cache age is less than 1 day, use the cached version
135
+ if (cacheAge < 1) {
136
+ opts.latestVersion = cached.version
137
+ }
138
+ }
139
+ }
140
+
98
141
  async function runBinary(datasource, opts) {
99
- let coverageOpt = opts.get("coverage")
100
- let forceOpt = opts.get("force")
101
- let logsOpt = opts.get("logs")
102
- let versionOpt = opts.get("version")
103
- let latestVersion = opts.get("latestVersion")
104
- let recompileOpt = opts.get("recompile")
142
+ let coverageOpt = opts.coverage
143
+ let forceOpt = opts.force
144
+ let logsOpt = opts.logs
145
+ let versionOpt = opts.version
146
+ let latestVersion = opts.latestVersion
147
+ let recompileOpt = opts.recompile
105
148
 
106
149
  const platform = await getPlatform(logsOpt)
107
150
 
@@ -126,10 +169,10 @@ async function getPlatform(logsOpt) {
126
169
  const arch = os.arch()
127
170
  const cpuCore = os.cpus()[0]
128
171
  const isM1 = (arch === 'arm64' && /Apple (M1|processor)/.test(cpuCore.model))
129
- const linuxInfo = type === 'Linux' ? await getLinuxInfo() : new Map()
130
- const linuxDistro = linuxInfo.get('name')
131
- const release = linuxInfo.get('version') || os.release()
132
- const majorVersion = parseInt(linuxInfo.get('version'), 10) || semver.major(release)
172
+ const linuxInfo = type === 'Linux' ? await getLinuxInfo() : {}
173
+ const linuxDistro = linuxInfo.name
174
+ const release = linuxInfo.version || os.release()
175
+ const majorVersion = parseInt(linuxInfo.version, 10) || semver.major(release)
133
176
 
134
177
  if (logsOpt) {
135
178
  print.info(`OS type: ${linuxDistro || type}\nOS arch: ${arch}\nOS release: ${release}\nOS major version: ${majorVersion}\nCPU model: ${cpuCore.model}`)
@@ -148,6 +191,8 @@ async function getPlatform(logsOpt) {
148
191
  } else if (type === 'Linux') {
149
192
  if (majorVersion === 18) {
150
193
  return 'binary-linux-18'
194
+ } if (majorVersion === 22) {
195
+ return 'binary-linux-22'
151
196
  } else {
152
197
  return 'binary-linux-20'
153
198
  }
@@ -163,13 +208,13 @@ async function getLinuxInfo() {
163
208
  try {
164
209
  let result = await system.run("cat /etc/*-release | grep -E '(^VERSION|^NAME)='", {trim: true})
165
210
  let infoArray = result.replace(/['"]+/g, '').split('\n').map(p => p.split('='))
166
- let infoMap = new Map();
211
+ let linuxInfo = {}
167
212
 
168
213
  infoArray.forEach((val) => {
169
- infoMap.set(val[0].toLowerCase(), val[1])
214
+ linuxInfo[val[0].toLowerCase()] = val[1]
170
215
  });
171
216
 
172
- return infoMap
217
+ return linuxInfo
173
218
  } catch (error) {
174
219
  print.error(`Error fetching the Linux version:\n ${error}`)
175
220
  process.exit(1)
@@ -177,39 +222,21 @@ async function getLinuxInfo() {
177
222
  }
178
223
 
179
224
  async function runDocker(datasource, opts) {
180
- let coverageOpt = opts.get("coverage")
181
- let forceOpt = opts.get("force")
182
- let versionOpt = opts.get("version")
183
- let latestVersion = opts.get("latestVersion")
184
- let recompileOpt = opts.get("recompile")
225
+ let coverageOpt = opts.coverage
226
+ let forceOpt = opts.force
227
+ let versionOpt = opts.version
228
+ let latestVersion = opts.latestVersion
229
+ let recompileOpt = opts.recompile
185
230
 
186
231
  // Remove binary-install-raw binaries, because docker has permission issues
187
232
  // when building the docker images
188
- await filesystem.remove("./node_modules/binary-install-raw/bin")
233
+ await filesystem.remove('./node_modules/binary-install-raw/bin')
189
234
 
190
235
  // Get current working directory
191
236
  let current_folder = await filesystem.cwd()
192
237
 
193
238
  // Declate dockerfilePath with default location
194
- let dockerfilePath = "./tests/.docker/Dockerfile"
195
-
196
- // Check if matchstick.yaml config exists
197
- if(filesystem.exists('matchstick.yaml')) {
198
- try {
199
- // Load the config
200
- let config = await yaml.load(filesystem.read('matchstick.yaml', 'utf8'))
201
-
202
- // Check if matchstick.yaml is not empty
203
- if(config != null) {
204
- // If a custom tests folder is declared update dockerfilePath
205
- dockerfilePath = path.join(config.testsFolder || 'tests', '.docker/Dockerfile')
206
- }
207
- } catch (error) {
208
- print.info('A problem occurred while reading matchstick.yaml. Please attend to the errors below:')
209
- print.error(error.message)
210
- process.exit(1)
211
- }
212
- }
239
+ let dockerfilePath = path.join(opts.testsFolder || 'tests', '.docker/Dockerfile')
213
240
 
214
241
  // Check if the Dockerfil already exists
215
242
  let dockerfileExists = filesystem.exists(dockerfilePath)