forked from subquery/subql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add validator package * add cli validate command * add skip in the report * update readme * update tsconfig reference * change await code style
- Loading branch information
Showing
27 changed files
with
550 additions
and
4 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,47 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {Command, flags} from '@oclif/command'; | ||
import {commonRules, Validator} from '@subql/validator'; | ||
import chalk from 'chalk'; | ||
|
||
export default class Validate extends Command { | ||
static description = 'check a folder or github repo is a validate subquery project'; | ||
|
||
static flags = { | ||
location: flags.string({char: 'l', description: 'local folder or github repo url'}), | ||
silent: flags.boolean(), | ||
}; | ||
|
||
async run(): Promise<void> { | ||
const {flags} = this.parse(Validate); | ||
const v = new Validator(flags.location ?? process.cwd()); | ||
v.addRule(...commonRules); | ||
|
||
const reports = await v.validate(); | ||
const passed = reports.filter((r) => r.valid).length; | ||
const skipped = reports.filter((r) => r.skipped).length; | ||
const failed = reports.length - passed - skipped; | ||
|
||
if (!flags.silent) { | ||
for (const r of reports) { | ||
if (r.valid) { | ||
this.log(`${chalk.bgGreen.bold(' PASS ')} ${r.name}`); | ||
} else if (r.skipped) { | ||
this.log(`${chalk.yellow.bold(' SKIP ')} ${r.name}`); | ||
} else { | ||
this.log(`${chalk.bgRed.bold(' FAIL ')} ${r.name}`); | ||
this.log(` ${chalk.redBright(r.description)}`); | ||
} | ||
} | ||
|
||
this.log(''); | ||
this.log(`Result: ${passed} passed, ${failed} failed, ${skipped} skipped`); | ||
this.log(''); | ||
} | ||
|
||
if (failed > 0) { | ||
this.exit(1); | ||
} | ||
} | ||
} |
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 @@ | ||
# @subql/validator |
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,28 @@ | ||
{ | ||
"name": "subquery-starter", | ||
"version": "0.0.3", | ||
"description": "This project can be use as a starting point for developing your SubQuery project", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc -b", | ||
"prepack": "rm -rf dist && npm build", | ||
"test": "jest", | ||
"codegen": "./node_modules/.bin/subql codegen" | ||
}, | ||
|
||
"homepage": "https://github.com/subquery/subql-starter", | ||
"repository": "github:subquery/subql-starter", | ||
"files": [ | ||
"dist", | ||
"schema.graphql", | ||
"project.yaml" | ||
], | ||
"author": "Ian He & Jay Ji", | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"@polkadot/api": "^3", | ||
"@subql/types": "^0.6.0", | ||
"typescript": "^4.1.3", | ||
"@subql/cli": "^0.7.3" | ||
} | ||
} |
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,24 @@ | ||
specVersion: "0.0.1" | ||
description: "" | ||
repository: "https://github.com/subquery/subql-starter" | ||
|
||
schema: "./schema.graphql" | ||
|
||
network: | ||
endpoint: "wss://polkadot.api.onfinality.io/public-ws" | ||
|
||
dataSources: | ||
- name: main | ||
kind: substrate/Runtime | ||
startBlock: 1 | ||
mapping: | ||
handlers: | ||
- handler: handleBlock | ||
kind: substrate/BlockHandler | ||
- handler: handleEvent | ||
kind: substrate/EventHandler | ||
filter: #Filter is optional | ||
module: balances | ||
method: Deposit | ||
- handler: handleCall | ||
kind: substrate/CallHandler |
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,26 @@ | ||
{ | ||
"name": "@subql/validator", | ||
"version": "0.1.0", | ||
"description": "to validate subquery project", | ||
"homepage": "https://github.com/subquery/subql", | ||
"repository": "github:subquery/subql", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "rm -rf dist && tsc -b" | ||
}, | ||
"author": "ZheX", | ||
"main": "dist/index.js", | ||
"license": "Apache-2.0", | ||
"files": [ | ||
"src/global.d.ts", | ||
"/dist" | ||
], | ||
"dependencies": { | ||
"axios": "^0.21.1", | ||
"js-yaml": "^4.0.0", | ||
"package-json-type": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@types/js-yaml": "^4.0.0" | ||
} | ||
} |
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,15 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {IPackageJson} from 'package-json-type'; | ||
|
||
export interface ContextData { | ||
projectPath: string; | ||
pkg: IPackageJson; | ||
schema?: unknown; | ||
} | ||
|
||
export interface Context { | ||
data: ContextData; | ||
logger: Console; | ||
} |
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,6 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './validator'; | ||
export * from './readers'; | ||
export * from './rules'; |
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,24 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {GithubReader} from './github-reader'; | ||
import {Reader} from './reader'; | ||
|
||
describe('GithubReader', () => { | ||
let reader: Reader; | ||
|
||
beforeAll(() => { | ||
const key = 'subquery/subql-starter'; | ||
reader = new GithubReader(key); | ||
}); | ||
|
||
it('should return the package json object', async () => { | ||
const data = await reader.getPkg(); | ||
expect(data.name).toBe('subquery-starter'); | ||
}); | ||
|
||
it('should return the project schema object', async () => { | ||
const data: any = await reader.getProjectSchema(); | ||
expect(data.repository).toBe('https://github.com/subquery/subql-starter'); | ||
}); | ||
}); |
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,47 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import axios, {AxiosInstance} from 'axios'; | ||
import yaml from 'js-yaml'; | ||
import {IPackageJson} from 'package-json-type'; | ||
import {Reader} from './reader'; | ||
|
||
export class GithubReader implements Reader { | ||
private readonly api: AxiosInstance; | ||
private defaultBranch: string; | ||
|
||
constructor(private readonly key: string) { | ||
this.api = axios.create({ | ||
baseURL: `https://raw.githubusercontent.com/${key}`, | ||
}); | ||
} | ||
|
||
async getPkg(): Promise<IPackageJson | undefined> { | ||
try { | ||
const branch = await this.getDefaultBranch(); | ||
const {data} = await this.api.get(`${branch}/package.json`); | ||
return data; | ||
} catch (err) { | ||
return undefined; | ||
} | ||
} | ||
|
||
async getProjectSchema(): Promise<unknown | undefined> { | ||
try { | ||
const branch = await this.getDefaultBranch(); | ||
const {data} = await this.api.get(`${branch}/project.yaml`); | ||
return yaml.load(data); | ||
} catch (err) { | ||
return undefined; | ||
} | ||
} | ||
|
||
private async getDefaultBranch(): Promise<string> { | ||
if (this.defaultBranch) { | ||
return this.defaultBranch; | ||
} | ||
const {data} = await axios.get(`https://api.github.com/repos/${this.key}`); | ||
this.defaultBranch = data.default_branch; | ||
return this.defaultBranch; | ||
} | ||
} |
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,6 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './reader'; | ||
export * from './github-reader'; | ||
export * from './local-reader'; |
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,25 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import * as path from 'path'; | ||
import {LocalReader} from './local-reader'; | ||
import {Reader} from './reader'; | ||
|
||
describe('LocalReader', () => { | ||
let reader: Reader; | ||
|
||
beforeAll(() => { | ||
const loc = path.join(__dirname, '../../fixtures'); | ||
reader = new LocalReader(loc); | ||
}); | ||
|
||
it('should return the package json object', async () => { | ||
const data = await reader.getPkg(); | ||
expect(data.name).toBe('subquery-starter'); | ||
}); | ||
|
||
it('should return the project schema object', async () => { | ||
const data: any = await reader.getProjectSchema(); | ||
expect(data.repository).toBe('https://github.com/subquery/subql-starter'); | ||
}); | ||
}); |
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,40 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import yaml from 'js-yaml'; | ||
import {IPackageJson} from 'package-json-type'; | ||
import {Reader} from './reader'; | ||
|
||
export class LocalReader implements Reader { | ||
constructor(private readonly projectPath: string) {} | ||
|
||
async getPkg(): Promise<IPackageJson | undefined> { | ||
const file = path.join(this.projectPath, 'package.json'); | ||
if (!fs.existsSync(file)) { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
try { | ||
const data = JSON.parse(fs.readFileSync(file).toString()); | ||
return Promise.resolve(data); | ||
} catch (err) { | ||
return Promise.resolve(undefined); | ||
} | ||
} | ||
|
||
async getProjectSchema(): Promise<unknown | undefined> { | ||
const file = path.join(this.projectPath, 'project.yaml'); | ||
if (!fs.existsSync(file)) { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
try { | ||
const data = yaml.load(fs.readFileSync(file).toString()); | ||
return Promise.resolve(data); | ||
} catch (err) { | ||
return Promise.resolve(undefined); | ||
} | ||
} | ||
} |
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,21 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import path from 'path'; | ||
import {GithubReader} from './github-reader'; | ||
import {LocalReader} from './local-reader'; | ||
import {ReaderFactory} from './reader'; | ||
|
||
describe('ReaderFactory', () => { | ||
it('should return the Github Reader', () => { | ||
const url = 'https://github.com/subquery/subql-starter'; | ||
const reader = ReaderFactory.create(url); | ||
expect(reader instanceof GithubReader).toBeTruthy(); | ||
}); | ||
|
||
it('should return the Local Reader', () => { | ||
const loc = path.join(__dirname, '../../fixtures'); | ||
const reader = ReaderFactory.create(loc); | ||
expect(reader instanceof LocalReader).toBeTruthy(); | ||
}); | ||
}); |
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,29 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import * as fs from 'fs'; | ||
import {IPackageJson} from 'package-json-type'; | ||
import {GithubReader} from './github-reader'; | ||
import {LocalReader} from './local-reader'; | ||
|
||
export interface Reader { | ||
getProjectSchema(): Promise<unknown | undefined>; | ||
getPkg(): Promise<IPackageJson | undefined>; | ||
} | ||
|
||
export class ReaderFactory { | ||
static create(location: string): Reader { | ||
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec | ||
const githubMatch = location.match(/https:\/\/github.com\/([\w-_]+\/[\w-_]+)/i); | ||
if (githubMatch) { | ||
return new GithubReader(githubMatch[1]); | ||
} | ||
|
||
const stats = fs.statSync(location); | ||
if (stats.isDirectory()) { | ||
return new LocalReader(location); | ||
} | ||
|
||
throw new Error(`unknown location: ${location}`); | ||
} | ||
} |
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,7 @@ | ||
// Copyright 2020-2021 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './rule'; | ||
export * from './require-build-script'; | ||
export * from './require-codegen-script'; | ||
export * from './require-cli-dep'; |
Oops, something went wrong.