Skip to content

Commit

Permalink
feat(github): add option to create a github repository
Browse files Browse the repository at this point in the history
  • Loading branch information
sharvit committed Dec 22, 2018
1 parent 82b46ad commit c414400
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 6 deletions.
65 changes: 63 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const commandExists = require('command-exists');
const findUp = require('find-up');
const makeDir = require('make-dir');

const github = require('../lib/github');

module.exports = class extends Generator {
prompting() {
return this.prompt([
Expand Down Expand Up @@ -44,6 +46,18 @@ module.exports = class extends Generator {
message: 'GitHub username',
store: true,
},
{
name: 'createGithubRepository',
message: 'Create a github repository?',
type: 'confirm',
default: true,
},
{
name: 'githubPassword',
message: 'Enter you github password:',
type: 'password',
when: ({ createGithubRepository }) => createGithubRepository,
},
{
name: 'githubTemplates',
message: 'Would you like to make it Github friendly for contributions?',
Expand Down Expand Up @@ -126,6 +140,8 @@ module.exports = class extends Generator {
email: answers.email,
website: answers.website,
githubUsername: answers.githubUsername,
createGithubRepository: answers.createGithubRepository,
githubPassword: answers.githubPassword,
githubTemplates: answers.githubTemplates,
travisCI: answers.travisCI,
coveralls: answers.coveralls,
Expand Down Expand Up @@ -199,13 +215,58 @@ module.exports = class extends Generator {
});
}

end() {
async end() {
await this._removeYoRc();

this._createGit();

if (this.props.createGithubRepository) {
const repository = await this._createGithubRepository();

this._addGithubRemote(repository.ssh_url);

this.log('\n\n');
this.log(`Repository created: ${repository.html_url}`);
}
}

_createGit() {
this.spawnCommandSync('git', ['init', '--quiet']);
this.spawnCommandSync('git', ['add', '.']);
this.spawnCommandSync('git', [
'commit',
'-m',
'Generated by generator-node-mdl 🔥',
'--quiet',
]);
}

findUp('.yo-rc.json').then(res => {
async _removeYoRc() {
return findUp('.yo-rc.json').then(res => {
if (res) {
this.fs.delete(res);
return new Promise((resolve, reject) => {
this.fs.commit(() => resolve());
});
}
});
}

async _createGithubRepository() {
github.login({
username: this.props.githubUsername,
password: this.props.githubPassword,
});

const { data } = await github.createRepository({
name: this.props.projectName,
description: this.props.description,
});

return data;
}

_addGithubRemote(remoteUrl) {
this.spawnCommandSync('git', ['remote', 'add', 'origin', remoteUrl]);
}
};
48 changes: 46 additions & 2 deletions app/index.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';

const path = require('path');
const assert = require('yeoman-assert');
const helpers = require('yeoman-test');

const github = require('../lib/github');

jest.mock('../lib/github');

const runAppGenerator = () => helpers.run(path.join(__dirname, './index.js'));

beforeEach(() => jest.clearAllMocks());

test('destinationRoot', () => {
return runAppGenerator()
.withPrompts({ projectName: 'temp' })
Expand Down Expand Up @@ -143,6 +147,46 @@ describe('prompts', () => {
});
});

test('createGithubRepository', () => {
const name = 'some-project-name';
const description = 'some-description';
const username = 'some-username';
const password = 'some-password';

return runAppGenerator()
.withPrompts({
createGithubRepository: true,
projectName: name,
description,
githubUsername: username,
githubPassword: password,
})
.then(() => {
assert.fileContent('.git/config', '[remote "origin"]');
assert.fileContent('.git/config', 'url = some-ssh_url');

expect(github.login).toBeCalledWith({ username, password });
expect(github.createRepository).toBeCalledWith({ name, description });
});
});

test('no createGithubRepository', () => {
return runAppGenerator()
.withPrompts({
createGithubRepository: false,
githubUsername: 'some-username',
githubPassword: 'some-password',
projectName: 'some-project-name',
})
.then(() => {
assert.noFileContent('.git/config', '[remote "origin"]');
assert.noFileContent('.git/config', 'url = some-ssh_url');

expect(github.login).not.toBeCalled();
expect(github.createRepository).not.toBeCalled();
});
});

test('githubTemplates', () => {
return runAppGenerator()
.withPrompts({
Expand Down
18 changes: 18 additions & 0 deletions lib/__mocks__/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const login = jest.fn(({ username, password }) => true);

const authenticate = jest.fn(token => ({ token }));

const createRepository = jest.fn(async ({ name, description }) => ({
data: {
name,
description,
ssh_url: 'some-ssh_url',
html_url: 'some-html_url',
},
}));

module.exports = {
login,
authenticate,
createRepository,
};
23 changes: 23 additions & 0 deletions lib/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const github = require('@octokit/rest')();

const login = ({ username, password }) =>
github.authenticate({
type: 'basic',
username,
password,
});

const authenticate = token =>
github.authenticate({
type: 'token',
token,
});

const createRepository = ({ name, description }) =>
github.repos.createForAuthenticatedUser({ name, description });

module.exports = {
login,
authenticate,
createRepository,
};
1 change: 0 additions & 1 deletion other/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ We haven‘t filled this out yet though. Care to help? See [`contributing.md`](.

## Want to do

- Add option to automatically create a github repository
- Add option to automatically install travis on github
- Add option to install [semantic-release](https://github.com/semantic-release/semantic-release)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"app"
],
"dependencies": {
"@octokit/rest": "^16.3.0",
"chalk": "^2.3.0",
"command-exists": "^1.2.4",
"find-up": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
node-fetch "^2.3.0"
universal-user-agent "^2.0.1"

"@octokit/rest@^16.0.1":
"@octokit/rest@^16.0.1", "@octokit/rest@^16.3.0":
version "16.3.0"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.3.0.tgz#98a24a3334312a87fff8a2a54c1dca4d0900d54d"
integrity sha512-u0HkROLB0nOSfJhkF5FKMg6I12m6cN5S3S73Lwtfgrs9u4LhgUCZN2hC2KDyIaT7nhvNe9Kx0PgxhhD6li6QsA==
Expand Down

0 comments on commit c414400

Please sign in to comment.