Skip to content

Commit

Permalink
refactor(sub-generators): refactor to sub-generators
Browse files Browse the repository at this point in the history
  • Loading branch information
sharvit committed Dec 29, 2018
1 parent b0a74f3 commit a4bc113
Show file tree
Hide file tree
Showing 32 changed files with 467 additions and 365 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/app/templates
**/templates/**
./app/
coverage/
package-lock.json
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Source
src
scripts

# Logs
logs
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"node": ">=6"
},
"scripts": {
"build": "yarn build:babel && yarn build:templates",
"build": "yarn build:clean && yarn build:babel && yarn build:templates",
"build:clean": "rimraf ./app",
"build:babel": "BABEL_ENV=production babel src --out-dir .",
"build:templates": "ncp ./src/app/templates ./app/templates",
"build:templates": "./scripts/build-templates.sh",
"test": "jest --coverage",
"test:watch": "jest --watch",
"lint": "eslint ./src",
Expand Down Expand Up @@ -64,7 +65,6 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^23.6.0",
"ncp": "^2.0.0",
"prettier": "^1.15.3",
"rimraf": "^2.6.2",
"semantic-release": "^15.13.1",
Expand Down
5 changes: 5 additions & 0 deletions scripts/build-templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cp -r ./src/app/generators/project/templates ./app/generators/project/templates
cp -r ./src/app/generators/github/templates ./app/generators/github/templates
cp -r ./src/app/generators/travis/templates ./app/generators/travis/templates
13 changes: 13 additions & 0 deletions src/app/generators/base-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Generator = require('yeoman-generator');

const options = require('../options');

module.exports = class extends Generator {
constructor(...args) {
super(...args);

for (const [name, value] of Object.entries(options)) {
this.option(name, value);
}
}
};
37 changes: 37 additions & 0 deletions src/app/generators/github/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const BaseGenerator = require('../base-generator');

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

module.exports = class extends BaseGenerator {
writing() {
if (this.options.githubTemplates) {
const templatesToCopy = [
{ templatePath: '_github', destinationPath: '.github' },
{ templatePath: 'other', destinationPath: 'other' },
{ templatePath: 'contributing.md', destinationPath: 'contributing.md' },
];

templatesToCopy.forEach(({ templatePath, destinationPath }) =>
this.fs.copyTpl(
this.templatePath(templatePath),
this.destinationPath(destinationPath),
this.options
)
);
}
}

async end() {
if (this.options.createGithubRepository) {
const {
data: { html_url: url },
} = await github.createRepository({
name: this.options.projectName,
description: this.options.description,
});

this.log('\n\n');
this.log(`Repository created: ${url}`);
}
}
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
101 changes: 101 additions & 0 deletions src/app/generators/project/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const path = require('path');
const chalk = require('chalk');
const commandExists = require('command-exists');
const findUp = require('find-up');
const makeDir = require('make-dir');

const BaseGenerator = require('../base-generator');

module.exports = class extends BaseGenerator {
/*
Run Loop
*/

configuring() {
if (path.basename(this.destinationRoot()) !== this.options.projectName) {
return makeDir(this.options.projectName).then(path => {
this.destinationRoot(path);
this.log(`\nGenerating a new project in ${chalk.green(path)}\n`);
});
}
}

writing() {
const templatesToCopy = [
{ templatePath: '_babelrc', destinationPath: '.babelrc' },
{ templatePath: '_editorconfig', destinationPath: '.editorconfig' },
{ templatePath: '_eslintignore', destinationPath: '.eslintignore' },
{ templatePath: '_eslintrc', destinationPath: '.eslintrc' },
{ templatePath: '_gitattributes', destinationPath: '.gitattributes' },
{ templatePath: '_gitignore', destinationPath: '.gitignore' },
{ templatePath: '_npmignore', destinationPath: '.npmignore' },
{ templatePath: '_package.json', destinationPath: 'package.json' },
{ templatePath: 'license', destinationPath: 'license' },
{ templatePath: 'readme.md', destinationPath: 'readme.md' },
{ templatePath: 'src', destinationPath: 'src' },
];

templatesToCopy.forEach(({ templatePath, destinationPath }) =>
this.fs.copyTpl(
this.templatePath(templatePath),
this.destinationPath(destinationPath),
this.options
)
);
}

async install() {
this._installNpmDeps();
}

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

this._createGit();
}

/*
Private Methods
*/

_installNpmDeps() {
const hasYarn = commandExists.sync('yarn');

this.installDependencies({
bower: false,
npm: !hasYarn,
yarn: hasYarn,
});
}

_createGit() {
const { repository } = this.options;

this.spawnCommandSync('git', ['init', '--quiet']);
this.spawnCommandSync('git', [
'remote',
'add',
'origin',
`git@github.com:${repository}.git`,
]);
this.spawnCommandSync('git', ['add', '.']);
this.spawnCommandSync('git', [
'commit',
'-m',
'Generated by generator-node-mdl 🔥',
'--quiet',
]);
}

async _removeYoRc() {
const yoRC = await findUp('.yo-rc.json');

if (yoRC) {
this.fs.delete(yoRC);

await new Promise((resolve, reject) => {
this.fs.commit(() => resolve());
});
}
}
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
},
"scripts": {
"prebuild": "rimraf dist",
"build": "babel src --out-dir dist --copy-files --ignore **/*.test.js",
"postbuild": "rimraf dist/**/*.test.js",
"build": "babel src --out-dir dist --ignore **/*.test.js",
"test:watch": "jest --watch",
"test": "jest --coverage",<% if (coveralls) { %>
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",<% } %>
Expand All @@ -33,8 +32,8 @@
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-plugin-add-module-exports": "^1.0.0",
"coveralls": "^3.0.2",
"babel-plugin-add-module-exports": "^1.0.0",<% if (coveralls) { %>
"coveralls": "^3.0.2",<% } %>
"eslint": "^5.10.0",
"eslint-config-prettier": "^3.3.0",
"eslint-config-standard": "^12.0.0",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions src/app/generators/travis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const BaseGenerator = require('../base-generator');

module.exports = class extends BaseGenerator {
writing() {
this.fs.copyTpl(
this.templatePath('_travis.yml'),
this.destinationPath('.travis.yml'),
this.options
);
}

async install() {
if (process.env.NODE_ENV === 'test') return;

const { repository, npmDeploy, npmToken } = this.props;

this.spawnCommandSync('gem', [
'install',
'travis',
'--no-ri',
'--no-rdoc',
'--quiet',
]);
this.spawnCommandSync('travis', ['login', '--auto']);
this.spawnCommandSync('travis', ['enable', '-r', repository]);

if (npmDeploy) {
this.spawnCommandSync('travis', [
'env',
'set',
'NPM_TOKEN',
npmToken,
'-r',
repository,
]);
}
}
};
File renamed without changes.
Loading

0 comments on commit a4bc113

Please sign in to comment.