Skip to content

Commit

Permalink
Fix browser bug, refactor, add debug option (#90)
Browse files Browse the repository at this point in the history
* Add debug option

* Fix package.json when browser is selected

* Refactored git code to `createGitCommands.js`

* Refactored tempalte commands.

* Improve the default template values

* Create rc file for test.
  • Loading branch information
unional committed Jun 4, 2016
1 parent 0bae629 commit 2f3d889
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 244 deletions.
102 changes: 102 additions & 0 deletions generators/beta/createGitCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict'
const fs = require('fs');
const path = require('path');
const url = require('url');
const simpleGit = require('simple-git');

module.exports = function creatingGitCommands(generator, repositoryPath) {
repositoryPath = path.resolve(repositoryPath);
let repoExists = fs.existsSync(path.join(repositoryPath, '.git'));

let git = new simpleGit(repositoryPath);

function readGitConfig(name) {
return new Promise((resolve, reject) => {
let result;
const child = generator.spawnCommand('git', ['config', name], { stdio: [0, 'pipe'] });
child.on('close', (code) => {
resolve(result);
});

child.stdout.on('data', (data) => {
result = data.toString().trim();
});
});
}

function loadConfig() {
return Promise.all([
readGitConfig('user.username'),
readGitConfig('user.name'),
readGitConfig('user.email')
]).then((results) => {
return {
username: results[0],
name: results[1],
email: results[2]
};
});
}

function loadRepoInfo() {
return new Promise((resolve) => {
let result = {
repositoryName: path.basename(repositoryPath),
repositoryOrganization: path.basename(path.join(repositoryPath, '..'))
};

if (repoExists) {
git.getRemotes(true, (err, out) => {
const origins = out.filter((entry) => {
return entry.name === 'origin';
});

if (origins.length > 0) {
result.repositoryRemoteUrl = origins[0].refs.fetch;
const u = url.parse(result.repositoryRemoteUrl);

const parts = u.pathname.substring(1).split('/', 2);
let repoName = parts[1];
result.repositoryName = repoName.indexOf('.git') === repoName.length - 4 ? repoName.slice(0, -4) : repoName;
result.repositoryOrganization = parts[0];
}

resolve(result);
});
}
else {
resolve(result);
}
});
}
return {
loadConfig,
loadRepoInfo,
repoExists,
clone: (remoteUrl) => {
return new Promise((resolve, reject) => {
git.clone(remoteUrl, repositoryPath, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
},
addSubmodule: (remoteUrl, localPath) => {
return new Promise((resolve, reject) => {
git.submoduleAdd(remoteUrl, localPath, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
};
};

11 changes: 11 additions & 0 deletions generators/beta/createGithubCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// const GitHubApi = require('github');

// const github = new GitHubApi({
// version: "3.0.0",
// protocol: "https",
// host: "api.github.com",
// timeout: 5000,
// header: {
// "user-agent": "generator-typings"
// }
// });
136 changes: 136 additions & 0 deletions generators/beta/createTemplateCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict'
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const rc = require('rc');

const TEMPLATEVERSION = 0;
const globalConfigPath = path.join(process.env.HOME, '.generator-typingsrc');


module.exports = function createTemplateCommmands(generator) {

let configTemplate;

function loadOrCreate() {
// Missing `version` indicates it is the default config.
const defaultConfigTemplate = {
username: generator.props.username,
repositoryNamePrefix: 'typed-',
repositoryOrganization: generator.props.username,
testFramework: 'blue-tape',
browserTestHarness: 'tape-run+jspm',
license: 'MIT',
licenseSignature: generator.props.username,
};
return configTemplate = rc('generator-typings', defaultConfigTemplate);
}

function update() {
const questions = [
{
type: 'input',
name: 'username',
message: `Your username on ${chalk.green('GitHub')}`,
default: configTemplate.username,
},
{
type: 'input',
name: 'repositoryOrganization',
message: (props) => `https://github.com/${chalk.green('<organization>')}/${configTemplate.repositoryNamePrefix}*`,
default: (props) => configTemplate.repositoryOrganization || props.username,
},
{
type: 'input',
name: 'repositoryNamePrefix',
message: (props) => {
return `https://github.com/${props.repositoryOrganization}/${chalk.green(configTemplate.repositoryNamePrefix)}*`
},
default: configTemplate.repositoryNamePrefix,
},
{
type: 'list',
name: 'testFramework',
message: `Your ${chalk.green('test framework')} of choice`,
choices: ['blue-tape'],
default: configTemplate.testFramework,
},
{
type: 'list',
name: 'browserTestHarness',
message: `Your ${chalk.cyan('browser')} ${chalk.green('test harness')}`,
choices: (props) => {
switch (props.testFramework) {
case 'blue-tape':
default:
return [
// tsify not working with TS 1.9 yet
// { name: 'tape-run + browserify', value: 'tape-run+browserify' },
{ name: 'tape-run + jspm', value: 'tape-run+jspm' },
];
}
},
default: configTemplate.browserTestHarness,
},
{
type: 'list',
name: 'license',
message: `Which ${chalk.green('license')} do you want to use?`,
choices: [
{ name: 'Apache 2.0', value: 'Apache-2.0' },
{ name: 'MIT', value: 'MIT' },
{ name: 'Unlicense', value: 'unlicense' },
{ name: 'FreeBSD', value: 'BSD-2-Clause-FreeBSD' },
{ name: 'NewBSD', value: 'BSD-3-Clause' },
{ name: 'Internet Systems Consortium (ISC)', value: 'ISC' },
{ name: 'No License (Copyrighted)', value: 'nolicense' }
],
default: configTemplate.license,
},
{
type: 'input',
name: 'licenseSignature',
message: `Your signature in the license: Copyright (c) ${new Date().getFullYear()} ${chalk.green('<signature>')}`,
default: (props) => configTemplate.licenseSignature || props.username,
},
];

return generator.prompt(questions).then((props) => {
props.version = TEMPLATEVERSION;
configTemplate = props;

if (!generator.options.skipCache) {
fs.writeFileSync(globalConfigPath, JSON.stringify(configTemplate));
}

return configTemplate;
});
};
function generateProps() {
const repoName = generator.typingsName || generator.props.repositoryName || configTemplate.repositoryNamePrefix + generator.props.sourceDeliveryPackageName
let props = {
username: configTemplate.username,
repositoryName: repoName,
repositoryOrganization: generator.props.repositoryOrganization || configTemplate.repositoryOrganization,
license: configTemplate.license,
licenseSignature: configTemplate.licenseSignature,
};

if (~generator.props.sourcePlatforms.indexOf('node')) {
props.testFramework = configTemplate.testFramework;
}

if (~generator.props.sourcePlatforms.indexOf('browser')) {
props.testFramework = configTemplate.testFramework;
props.browserTestHarness = configTemplate.browserTestHarness;
}

return props;
};

return {
loadOrCreate,
update,
generateProps
}
}
Loading

0 comments on commit 2f3d889

Please sign in to comment.