Skip to content

Commit

Permalink
Add NPM Release
Browse files Browse the repository at this point in the history
Closes #4.
  • Loading branch information
Eric Butler committed Aug 11, 2019
1 parent 84d3d47 commit 29f7db2
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 0 deletions.
106 changes: 106 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ executors:
environment:
CARGO_HOME: /root/notarize/qlc/.cargo

cross_compiling_host:
docker:
- image: notarize/cross-rust:1.36.0-stretch
working_directory: /root/notarize/qlc
environment:
CARGO_HOME: /root/notarize/qlc/.cargo

github_api_client:
docker:
- image: notarize/github-api-client:ghr-0.12.2
working_directory: /root/notarize/qlc

node_env:
docker:
- image: notarize/node:10.14.0
working_directory: /root/notarize/qlc

jobs:
cargo_init:
executor: default_rust_compiler
Expand Down Expand Up @@ -70,6 +87,72 @@ jobs:
name: Testing
command: cargo test -- --test-threads=7

build_release_bin:
executor: cross_compiling_host
steps:
- attach_made_workspace
- run:
name: Building OS X
command: cargo build --release --target=x86_64-apple-darwin
environment:
CC: o64-clang
CXX: o64-clang++
LIBZ_SYS_STATIC: 1
- run:
name: Building Linux Musl
command: cargo build --release --target=x86_64-unknown-linux-musl
- persist_to_workspace:
root: /root/notarize
paths:
- qlc/target/x86_64-apple-darwin/release
- qlc/target/x86_64-unknown-linux-musl/release

create_github_release:
executor: github_api_client
steps:
- attach_made_workspace
- run:
name: Making GitHub Release
command: |
mkdir -p archives
LINUX_MUSL="x86_64-unknown-linux-musl"
cd "$CIRCLE_WORKING_DIRECTORY/target/$LINUX_MUSL/release"
VERSION=$(./qlc --version | sed 's/QL Compiler //g')
tar czf "qlc-$VERSION-$LINUX_MUSL.tar.gz" qlc
mv "qlc-$VERSION-$LINUX_MUSL.tar.gz" "$CIRCLE_WORKING_DIRECTORY/archives/."
MAC_OSX="x86_64-apple-darwin"
cd "$CIRCLE_WORKING_DIRECTORY/target/$MAC_OSX/release"
tar czf "qlc-$VERSION-$MAC_OSX.tar.gz" qlc
mv "qlc-$VERSION-$MAC_OSX.tar.gz" "$CIRCLE_WORKING_DIRECTORY/archives/."
cd "$CIRCLE_WORKING_DIRECTORY"
ls -l archives
ghr \
-t "$GITHUB_TOKEN" \
-n "v$VERSION" \
-c "$CIRCLE_SHA1" \
-delete \
"$VERSION" archives
create_npm_release:
executor: node_env
steps:
- attach_made_workspace
- run:
name: Configuring NPM Token
command: |
echo "registry=https://registry.npmjs.org/" > ~/.npmrc
echo "//registry.npmjs.org/:_authToken=${NOTARBOT_NPM_TOKEN}" >> ~/.npmrc
- run:
name: Publishing Package
command: |
VERSION=$(./target/x86_64-unknown-linux-musl/release/qlc --version | sed 's/QL Compiler //g')
cd pkg/npm
sed -i "s/QLC_VERSION/$VERSION/g" package.json
yarn publish --new-version "$VERSION" --access public
workflows:
version: 2
Everything:
Expand Down Expand Up @@ -99,3 +182,26 @@ workflows:
filters:
branches:
ignore: master

- build_release_bin:
requires:
- test
- lint
- fmt
filters:
branches:
only: production

- create_github_release:
requires:
- build_release_bin
filters:
branches:
only: production

- create_npm_release:
requires:
- create_github_release
filters:
branches:
only: production
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "qlc"
version = "0.1.0"
authors = ["Eric Butler <eric.butler@notarize.com>"]
edition = "2018"
license = "MIT"
autotests = false

[[test]]
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
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.
48 changes: 48 additions & 0 deletions README.md
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
```
2 changes: 2 additions & 0 deletions pkg/npm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tar.gz
bin/
8 changes: 8 additions & 0 deletions pkg/npm/README.md
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.
3 changes: 3 additions & 0 deletions pkg/npm/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("path").join(__dirname, "../bin/qlc");
87 changes: 87 additions & 0 deletions pkg/npm/lib/postinstall.js
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);
});
37 changes: 37 additions & 0 deletions pkg/npm/package.json
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"
}
}

0 comments on commit 29f7db2

Please sign in to comment.