-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #4.
- Loading branch information
Eric Butler
committed
Aug 11, 2019
1 parent
84d3d47
commit 29f7db2
Showing
9 changed files
with
313 additions
and
0 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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 Notarize, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,48 @@ | ||
# QL Compiler | ||
|
||
The QL compiler `qlc` is a super fast and fun codegenerator for GraphQL clients. Specifically, it is capable of | ||
reading `.graphql` queries, mutations, and fragment files and combining this with introspection schema JSON to | ||
produce ad-hoc type defintions for TypeScript. Its similar to tools the [Apollo Tooling CLI](https://github.com/apollographql/apollo-tooling) | ||
and [GraphQL Code Generator](https://github.com/dotansimha/graphql-code-generator), but smaller in scope | ||
(and much faster). | ||
|
||
## Example | ||
|
||
Say you have a query that looks like this: | ||
|
||
```graphql | ||
query MyQuery { | ||
comment { | ||
author { | ||
name | ||
} | ||
content | ||
} | ||
} | ||
``` | ||
|
||
Using qlc would enable you to codegen the following TypeScript file easily: | ||
|
||
```ts | ||
export interface MyQuery_comment_author { | ||
name: string | null; | ||
} | ||
|
||
export interface MyQuery_comment { | ||
author: MyQuery_comment_author; | ||
content: string; | ||
} | ||
|
||
export interface MyQuery { | ||
comment: MyQuery_comment; | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
You can use the binary as is see `--help` for various CLI options (there are not many). Additionally, | ||
since you are likely using this from a node project, there is a convience NPM package available. | ||
|
||
```sh | ||
yarn add @notarize/qlc-cli | ||
``` |
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,2 @@ | ||
*.tar.gz | ||
bin/ |
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,8 @@ | ||
# @notarize/qlc-cli | ||
|
||
This is an npm module for using [qlc](https://github.com/notarize/qlc) (a rust binary) in a Node project. | ||
|
||
### How it works | ||
|
||
- QLC is built in CI and binaries are posted with a GitHub release. | ||
- In this module's postinstall task, it determines which platform it is being installed on and downloads the correct binary. |
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,3 @@ | ||
"use strict"; | ||
|
||
module.exports = require("path").join(__dirname, "../bin/qlc"); |
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,87 @@ | ||
"use strict"; | ||
|
||
const os = require("os"); | ||
const fs = require("fs"); | ||
const https = require("https"); | ||
const path = require("path"); | ||
const util = require("util"); | ||
const childProc = require("child_process"); | ||
|
||
const fsChmod = util.promisify(fs.chmod); | ||
const fsUnlink = util.promisify(fs.unlink); | ||
const fsExists = util.promisify(fs.exists); | ||
const mkdir = util.promisify(fs.mkdir); | ||
|
||
const VERSION = require("../package.json").version; | ||
const BIN_PATH = path.join(__dirname, "../bin"); | ||
|
||
function getTarget() { | ||
switch (os.platform()) { | ||
case "darwin": | ||
return "x86_64-apple-darwin"; | ||
case "linux": | ||
return "x86_64-unknown-linux-musl"; | ||
default: | ||
throw new Error(`Unsupported platform: ${os.platform()}`); | ||
} | ||
} | ||
|
||
function download(url, dest) { | ||
return new Promise((resolve, reject) => { | ||
const outFile = fs.createWriteStream(dest); | ||
const cleanup = (err) => fsUnlink(dest) | ||
.catch(console.error) | ||
.finally(() => { | ||
reject(err); | ||
}); | ||
const headers = { Accept: "application/octet-stream" }; | ||
https.get(url, { headers }, (response) => { | ||
if (response.statusCode === 302) { | ||
return download(response.headers.location, dest) | ||
.then(resolve).catch(reject); | ||
} else if (response.statusCode !== 200) { | ||
return cleanup(new Error(`Download failed with ${response.statusCode}`)); | ||
} | ||
response.pipe(outFile); | ||
outFile.on("finish", () => { | ||
resolve(); | ||
}); | ||
}).on("error", cleanup); | ||
}); | ||
} | ||
|
||
function untar(zipPath, destDir) { | ||
return new Promise((resolve, reject) => { | ||
const unzipProc = childProc.spawn( | ||
"tar", | ||
["xf", zipPath, "-C", destDir], | ||
{ stdio: "inherit" }, | ||
); | ||
unzipProc.on("error", reject); | ||
unzipProc.on("close", code => { | ||
return code === 0 ? resolve() : reject(new Error(`tar exited with ${code}`)); | ||
}); | ||
}); | ||
} | ||
|
||
async function main() { | ||
const fileName = `qlc-${VERSION}-${getTarget()}.tar.gz`; | ||
const url = `https://github.com/notarize/qlc/releases/download/${VERSION}/${fileName}`; | ||
await Promise.all([ | ||
fsExists(BIN_PATH).then(exists => { | ||
return exists ? Promise.resolve() : mkdir(BIN_PATH); | ||
}), | ||
download(url, fileName), | ||
]); | ||
await untar(fileName, BIN_PATH); | ||
await Promise.all([ | ||
fsUnlink(fileName), | ||
fsChmod(path.join(BIN_PATH, "qlc"), "755"), | ||
]); | ||
} | ||
|
||
main() | ||
.catch(err => { | ||
console.error(`Unhandled error: ${err}`); | ||
process.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "@notarize/qlc-cli", | ||
"version": "QLC_VERSION", | ||
"author": "Notarize Inc.", | ||
"license": "MIT", | ||
"description": "A module for using QLC in a Node project", | ||
"main": "lib/index.js", | ||
"bin": { | ||
"qlc": "bin/qlc" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/notarize/qlc.git" | ||
}, | ||
"keywords": [ | ||
"gql", | ||
"generator", | ||
"types", | ||
"interfaces", | ||
"graphql", | ||
"codegen", | ||
"apollo", | ||
"typescript", | ||
"ts", | ||
"typings" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/notarize/qlc/issues" | ||
}, | ||
"homepage": "https://github.com/notarize/qlc", | ||
"scripts": { | ||
"postinstall": "node ./lib/postinstall.js" | ||
} | ||
} |