forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-github-repo.ts
92 lines (84 loc) · 2.87 KB
/
create-github-repo.ts
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
import * as denodeify from 'denodeify';
const Task = require('ember-cli/lib/models/task');
const SilentError = require('silent-error');
import { exec } from 'child_process';
import * as https from 'https';
import { oneLine } from 'common-tags';
export default Task.extend({
run: function(commandOptions: any) {
const ui = this.ui;
let promise: Promise<any>;
// declared here so that tests can stub exec
const execPromise = denodeify(exec);
if (/.+/.test(commandOptions.ghToken) && /\w+/.test(commandOptions.ghUsername)) {
promise = Promise.resolve({
ghToken: commandOptions.ghToken,
ghUsername: commandOptions.ghUsername
});
} else {
ui.writeLine();
ui.writeLine(oneLine`
In order to deploy this project via GitHub Pages, we must first create a repository for it.
`);
ui.writeLine(oneLine`
It\'s safer to use a token than to use a password so you will need to create one
`);
ui.writeLine('Go to the following page and click "Generate new token".');
ui.writeLine('https://github.com/settings/tokens\n');
ui.writeLine('Choose "public_repo" as scope and then click "Generate token".\n');
promise = ui.prompt([
{
name: 'ghToken',
type: 'input',
message: oneLine`
Please enter GitHub token you just created
(used only once to create the repo):
`,
validate: function(token: string) {
return /.+/.test(token);
}
}, {
name: 'ghUsername',
type: 'input',
message: 'and your GitHub user name:',
validate: function(userName: string) {
return /\w+/.test(userName);
}
}]);
}
return promise
.then((answers) => {
return new Promise(function(resolve, reject) {
const postData = JSON.stringify({
'name': commandOptions.projectName
});
const req = https.request({
hostname: 'api.github.com',
port: 443,
path: '/user/repos',
method: 'POST',
headers: {
'Authorization': `token ${answers.ghToken}`,
'Content-Type': 'application/json',
'Content-Length': postData.length,
'User-Agent': 'angular-cli-github-pages'
}
});
req.on('response', function(response: any) {
if (response.statusCode === 201) {
resolve(execPromise(oneLine`
git remote add origin
[email protected]:${answers.ghUsername}/${commandOptions.projectName}.git
`));
} else {
reject(new SilentError(oneLine`
Failed to create GitHub repo. Error: ${response.statusCode} ${response.statusMessage}
`));
}
});
req.write(postData);
req.end();
});
});
}
});