GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — polymer-cli/src/github/github.ts at master · Polymer/polymer-cli

This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Expand file tree

/

github.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
180 lines (166 loc) · 5.68 KB
/

github.ts

Copy path

File metadata and controls

180 lines (166 loc) · 5.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @license
* 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 path from 'path';
import * as logging from 'plylog';
import * as semver from 'semver';
import request = require('request');
import rimraf = require('rimraf');
import GitHubApi = require('github');
const gunzip = require('gunzip-maybe');
const tar = require('tar-fs');
const fileFilterList = [
'.gitattributes',
'.github',
'.travis.yml',
];
const logger = logging.getLogger('cli.github');
export type RequestAPI = request.RequestAPI<
request.Request,
request.CoreOptions,
request.RequiredUriUrl>;
export class GithubResponseError extends Error {
name = 'GithubResponseError';
statusCode?: number;
statusMessage?: string;
constructor(statusCode?: number, statusMessage?: string) {
super('unexpected response: ' + statusCode + ' ' + statusMessage);
this.statusCode = statusCode;
this.statusMessage = statusMessage;
}
}
export interface GithubOpts {
owner: string;
repo: string;
githubToken?: string;
githubApi?: GitHubApi;
requestApi?: request
.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
}
export class Github {
private _token: string|null;
private _github: GitHubApi;
private _request: request
.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
private _owner: string;
private _repo: string;
static tokenFromFile(filename: string): string|null {
try {
return fs.readFileSync(filename, 'utf8').trim();
} catch (error) {
return null;
}
}
constructor(opts: GithubOpts) {
this._token = opts.githubToken || Github.tokenFromFile('token');
this._owner = opts.owner;
this._repo = opts.repo;
this._github = opts.githubApi || new GitHubApi({
protocol: 'https',
});
if (this._token != null) {
this._github.authenticate({
type: 'oauth',
token: this._token,
});
}
this._request = opts.requestApi || request;
}
/**
* Given a Github tarball URL, download and extract it into the outDir
* directory.
*/
async extractReleaseTarball(tarballUrl: string, outDir: string):
Promise<void> {
const tarPipe = tar.extract(outDir, {
ignore: (_: any, header: any) => {
const splitPath = path.normalize(header.name).split(path.sep);
// ignore the top directory in the tarfile to unpack directly to
// the cwd
return splitPath.length < 1 || splitPath[1] === '';
},
map: (header: any) => {
const splitPath = path.normalize(header.name).split(path.sep);
const unprefixed = splitPath.slice(1).join(path.sep).trim();
// A ./ prefix is needed to unpack top-level files in the tar,
// otherwise they're just not written
header.name = path.join('.', unprefixed);
return header;
},
});
return new Promise<void>((resolve, reject) => {
this._request({
url: tarballUrl,
headers: {
'User-Agent': 'request',
'Authorization': (this._token) ? `token ${this._token}` :
undefined,
}
})
.on('response',
function(response) {
if (response.statusCode !== 200) {
reject(new GithubResponseError(
response.statusCode, response.statusMessage));
return;
}
logger.info('Unpacking template files');
})
.on('error', reject)
.pipe(gunzip())
.on('error', reject)
.pipe(tarPipe)
.on('error', reject)
.on('finish', () => {
logger.info('Finished writing template files');
resolve();
});
});
}
/**
* Given an extracted or cloned github directory, unlink files that are not
* intended to exist in the template and serve other purposes in the gith
* repository.
*/
removeUnwantedFiles(outDir: string) {
fileFilterList.forEach((filename) => {
rimraf.sync(path.join(outDir, filename));
});
}
/**
* Get all Github releases and match their tag names against the given semver
* range. Return the release with the latest possible match.
*/
async getSemverRelease(semverRange: string): Promise<GitHubApi.Release> {
// Note that we only see the 100 most recent releases. If we ever release
// enough versions that this becomes a concern, we'll need to improve this
// call to request multiple pages of results.
const releases: GitHubApi.Release[] = await this._github.repos.getReleases({
owner: this._owner,
repo: this._repo,
per_page: 100,
});
const validReleaseVersions =
releases.filter((r) => semver.valid(r.tag_name)).map((r) => r.tag_name);
const maxSatisfyingReleaseVersion =
semver.maxSatisfying(validReleaseVersions, semverRange);
const maxSatisfyingRelease =
releases.find((r) => r.tag_name === maxSatisfyingReleaseVersion);
if (!maxSatisfyingRelease) {
throw new Error(`${this._owner}/${this._repo} has no releases matching ${
semverRange}.`);
}
return maxSatisfyingRelease;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.