-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Polymer/polykart
New polytool init template
- Loading branch information
Showing
9 changed files
with
315 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
declare module "github" { | ||
interface NodeCallback<T> { (err: any, res: T): any; } | ||
interface Options { | ||
version: string; | ||
protocol: string; | ||
} | ||
interface CreatePullRequestOpts { | ||
user: string; | ||
repo: string; | ||
title: string; | ||
head: string; | ||
base: string; | ||
body?: string; | ||
} | ||
interface IssuesEditOpts { | ||
headers?: Object; | ||
user: string; | ||
repo: string; | ||
number: number; | ||
title?: string; | ||
body?: string; | ||
assignee?: string; | ||
milestone?: number; | ||
labels?: string[]; | ||
state?: string; | ||
} | ||
interface GetFromOrgOpts { | ||
org: string; | ||
per_page?: number; | ||
page?: number; | ||
} | ||
interface GetReleaseMessage { | ||
headers?: {string: string}; | ||
owner: string; | ||
repo: string; | ||
id: number; | ||
} | ||
interface ListReleasesMessage { | ||
headers?: {string: string}; | ||
owner: string; | ||
repo: string; | ||
page?: number; | ||
per_page?: number; | ||
} | ||
class GitHubApi { | ||
constructor(options: Options); | ||
repos: { | ||
getFromOrg(msg: GetFromOrgOpts, cb: NodeCallback<GitHubApi.Repo[]>): void; | ||
get(msg: {user: string, repo: string}, | ||
cb: NodeCallback<GitHubApi.Repo>): void; | ||
} | ||
pullRequests: { | ||
create( | ||
msg: CreatePullRequestOpts, cb: NodeCallback<GitHubApi.Issue>): void; | ||
} | ||
issues: { | ||
edit(msg: IssuesEditOpts, cb: NodeCallback<GitHubApi.Issue>): void; | ||
} | ||
releases: { | ||
getRelease(msg: GetReleaseMessage, cb: NodeCallback<GitHubApi.Release>): void; | ||
listReleases(msg: ListReleasesMessage, cb: NodeCallback<GitHubApi.Release[]>): void; | ||
} | ||
authenticate(credentials: {type: string, token: string}): void; | ||
user: { get(msg: {}, cb: NodeCallback<GitHubApi.User>): void; }; | ||
} | ||
namespace GitHubApi { | ||
class Repo { | ||
owner: User; | ||
name: string; | ||
clone_url: string; | ||
} | ||
interface User { | ||
login: string; | ||
} | ||
interface Issue { | ||
number: number; | ||
title: string; | ||
body: string; | ||
assignee: User; | ||
milestone: Milestone; | ||
state: string; | ||
labels: { name: string, color: string, url: string }[]; | ||
user: User; | ||
} | ||
interface PullRequest extends Issue {} | ||
interface Milestone {} | ||
interface Release { | ||
url: string, | ||
name: string, | ||
tag_name: string, | ||
id: number, | ||
tarball_url: string, | ||
} | ||
} | ||
export = GitHubApi; | ||
} |
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,134 @@ | ||
/* | ||
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | ||
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
* Code distributed by Google as part of the polymer project is also | ||
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as GitHubApi from 'github'; | ||
import * as http from 'http'; | ||
import {Base} from 'yeoman-generator'; | ||
import * as path from 'path'; | ||
import {Stream} from 'stream'; | ||
|
||
const gunzip = require('gunzip-maybe'); | ||
const request = require('request'); | ||
const tar = require('tar-fs'); | ||
|
||
export const PolykartGenerator = getGenerator(); | ||
|
||
export function getGenerator(options?) { | ||
let realGitHubApi = new GitHubApi({ | ||
version: '3.0.0', | ||
protocol: 'https', | ||
}); | ||
let requestApi = options && options.requestApi || request; | ||
let githubApi = options && options.githubApi || realGitHubApi; | ||
let githubToken = options && options.githubToken; | ||
// let outDir = options && options.outDir || process.cwd(); | ||
|
||
return class PolykartGenerator extends Base { | ||
|
||
_github: GitHubApi; | ||
_githubToken: string; | ||
|
||
constructor(args: string | string[], options: any) { | ||
super(args, options); | ||
this._githubToken = this._getGitHubToken(); | ||
this._github = githubApi; | ||
this._github.authenticate({ | ||
type: 'oauth', | ||
token: this._githubToken, | ||
}); | ||
} | ||
|
||
writing() { | ||
let done = this.async(); | ||
console.log('Finding latest release of polykart...'); | ||
return this._getTemplate() | ||
.then((release) => { | ||
let tarballUrl = release.tarball_url; | ||
console.log('Latest release URL:', tarballUrl); | ||
requestApi({ | ||
url: tarballUrl, | ||
headers: { | ||
'User-Agent': 'request', | ||
'Authorization': `token ${this._githubToken}`, | ||
} | ||
}) | ||
.on('response', (response) => { | ||
if (response.statusCode == 200) { | ||
console.log('Downloading template'); | ||
} | ||
}) | ||
.on('end', () => { | ||
console.log('complete'); | ||
done(); | ||
}) | ||
.pipe(gunzip()) | ||
.pipe(tar.extract(this.destinationRoot(), { | ||
ignore: (_, header) => { | ||
let splitPath = header.name.split(path.sep); | ||
// ignore the top directory in the tarfile to unpack directly to | ||
// the cwd | ||
return splitPath.length < 2 || splitPath[1] === ''; | ||
}, | ||
map: (header) => { | ||
let unprefixed = header.name.split(path.sep).slice(1).join(path.sep).trim(); | ||
// the ./ is needed to unpack top-level files in the tar, otherwise | ||
// they're just not written | ||
header.name = './' + unprefixed; | ||
return header; | ||
} | ||
})); | ||
}) | ||
.catch((error) => { | ||
console.error('Could not load polykart template'); | ||
console.error(error); | ||
done(); | ||
}); | ||
} | ||
|
||
install() { | ||
(<any>this).bowerInstall(); | ||
} | ||
|
||
_getGitHubToken() { | ||
if (githubToken) { | ||
return githubToken; | ||
} | ||
try { | ||
return fs.readFileSync('token', 'utf8').trim(); | ||
} catch (e) { | ||
console.error(` | ||
You need to create a github token and place it in a file named 'token'. | ||
The token only needs the 'public repos' permission. | ||
Generate a token here: https://github.com/settings/tokens | ||
This restriction will be removed soon! | ||
`); | ||
} | ||
} | ||
|
||
_getTemplate(): Promise<GitHubApi.Release> { | ||
return new Promise((resolve, reject) => { | ||
this._github.releases.listReleases({ | ||
owner: 'PolymerLabs', | ||
repo: 'polykart', | ||
}, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
if (result.length === 0) { | ||
reject('no releases'); | ||
} else { | ||
resolve(result[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,59 @@ | ||
/* | ||
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | ||
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
* Code distributed by Google as part of the polymer project is also | ||
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const assert = require('chai').assert; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const streamLib = require('stream'); | ||
const yoAssert = require('yeoman-assert'); | ||
const helpers = require('yeoman-test'); | ||
|
||
const polykart = require('../../lib/templates/polykart.js'); | ||
|
||
suite('templates/polykart', () => { | ||
|
||
test('untars a release', (done) => { | ||
let mockRequestApi = (options) => { | ||
assert.equal(options.url, 'http://foo.com/bar.tar'); | ||
return fs.createReadStream(path.join(__dirname, 'test_tarball.tgz')); | ||
}; | ||
|
||
let mockGithubApi = { | ||
authenticate(options) { | ||
assert.equal(options.type, 'oauth'); | ||
assert.equal(options.token, 'token-token'); | ||
}, | ||
|
||
releases: { | ||
listReleases(options, cb) { | ||
assert.equal(options.owner, 'PolymerLabs'); | ||
assert.equal(options.repo, 'polykart'); | ||
cb(null, [{ | ||
tarball_url: 'http://foo.com/bar.tar', | ||
}]); | ||
}, | ||
}, | ||
}; | ||
|
||
let generator = new polykart.getGenerator({ | ||
requestApi: mockRequestApi, | ||
githubApi: mockGithubApi, | ||
githubToken: 'token-token', | ||
}); | ||
|
||
helpers.run(generator) | ||
.on('end', (a) => { | ||
yoAssert.file(['file1.txt']); | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |
Binary file not shown.
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