Skip to content

Commit

Permalink
feat: add license and readme support
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackfaded committed Jul 7, 2020
1 parent ecc3070 commit 22084bb
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 30 deletions.
51 changes: 39 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
"sonarqube:report": "sh scripts/report_sonarqube.sh -k"
},
"dependencies": {
"axios": "^0.19.2",
"validate-npm-package-name": "^3.0.0",
"yeoman-generator": "^4.11.0"
},
"devDependencies": {
"@types/jest": "^26.0.3",
"@types/node": "^14.0.14",
"@types/validate-npm-package-name": "^3.0.0",
"@types/yeoman-assert": "^3.1.1",
"@types/yeoman-generator": "^3.1.4",
"@types/yeoman-test": "^2.0.4",
Expand Down
57 changes: 43 additions & 14 deletions src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import isValidPkgJsonName from 'validate-npm-package-name';
import Generator from 'yeoman-generator';

import { Features } from '../lib/enums/features';
import { Filenames } from '../lib/enums/filenames';
import { Messages } from '../lib/enums/messages';
import { Names } from '../lib/enums/names';
import { withFeature } from '../lib/helpers';
import { withFeature, getLicenses } from '../lib/helpers';
import rootPkg from '../lib/helpers/package';

interface Answers {
[Names.PROJECT_NAME]: string;
[Names.AUTHOR]: string;
[Names.FEATURES]: Features[];
[Names.SONARQUBE]: boolean;
[Names.LINT_STAGED]: boolean;
[Names.SONARQUBE_TOKEN]: string | null;
[Names.WITH_LICENSE]: boolean;
[Names.LICENSE]: string;
}

const defaultAnswers: Answers = {
Expand All @@ -23,6 +25,8 @@ const defaultAnswers: Answers = {
[Names.SONARQUBE]: false,
[Names.LINT_STAGED]: false,
[Names.SONARQUBE_TOKEN]: null,
[Names.WITH_LICENSE]: true,
[Names.LICENSE]: 'mit',
};

export default class extends Generator {
Expand All @@ -34,6 +38,7 @@ export default class extends Generator {
type: 'input',
name: Names.PROJECT_NAME,
message: Messages.PROJECT_NAME,
validate: (answer) => isValidPkgJsonName(answer).validForNewPackages,
default: defaultAnswers.projectname,
},
{
Expand Down Expand Up @@ -73,18 +78,44 @@ export default class extends Generator {
type: 'input',
name: Names.SONARQUBE_TOKEN,
message: Messages.SONARQUBE_TOKEN,
default: defaultAnswers['sonarqube-token'],
default: defaultAnswers[Names.SONARQUBE_TOKEN],
when: (answers) => answers[Names.SONARQUBE],
},
{
type: 'confirm',
name: Names.LINT_STAGED,
message: Messages.LINT_STAGED,
default: defaultAnswers['lint-staged'],
default: defaultAnswers[Names.LINT_STAGED],
when: (answers) => withFeature(answers, Features.ESLINT),
},
{
type: 'confirm',
name: Names.WITH_LICENSE,
message: Messages.WITH_LICENSE,
default: true,
},
]);
this.answers = answers;
this.answers = { ...defaultAnswers, ...answers };
if (answers[Names.WITH_LICENSE]) {
try {
const licenses = await getLicenses();
const licenseAnswer = await this.prompt([
{
type: 'list',
name: Names.LICENSE,
message: Messages.LICENSE,
default: defaultAnswers[Names.LICENSE],
choices: licenses.map((l: any) => ({
name: l.name,
value: l.key,
})),
},
]);
this.answers[Names.LICENSE] = licenseAnswer[Names.LICENSE];
} catch (error) {
console.log('Error while fetching licenses, skipping...');
}
}
}

writing(): void {
Expand All @@ -93,19 +124,11 @@ export default class extends Generator {
Filenames.NODEMON_CONFIG,
Filenames.GIT_IGNORE,
Filenames.SRC_FOLDER,
Filenames.README,
].forEach((fileName: Filenames) => {
this.fs.copy(this.templatePath(fileName), this.destinationPath(fileName));
});

// this.fs.copyTpl(
// this.templatePath(Filenames.PACKAGE_JSON),
// this.destinationPath(Filenames.PACKAGE_JSON),
// {
// title: this.answers.projectname,
// author: this.answers.author,
// }
// );

this.fs.writeJSON(this.destinationPath(Filenames.PACKAGE_JSON), {
name: this.answers.projectname,
version: '0.1.0',
Expand Down Expand Up @@ -149,6 +172,12 @@ export default class extends Generator {
withPrettier: withFeature(this.answers, Features.PRETTIER),
});
}

if (this.answers[Names.WITH_LICENSE]) {
this.composeWith(require.resolve('../license'), {
license: this.answers.license,
});
}
}

install(): void {
Expand Down
53 changes: 53 additions & 0 deletions src/app/templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Node Starter

## Getting Started

Run your app in development mode
```bash
npm run start:dev
```

Build your app
```bash
npm run build
```

### Jest

Jest enables you to unittest your code and generate coverage files

To test your files run
```bash
npm run test
npm run test:cov # also creates coverage files to report to sonarqube
```

### Husky and Lint-Staged
[husky](https://www.npmjs.com/package/husky) and [lint-staged](https://www.npmjs.com/package/lint-staged) will lint and format your files while committing. It will abort if there are remaining linting errors. This keeps your git clean from malformed code.



### Sonarqube
With [sonarqube](https://www.sonarqube.org/) you get some metrics about your code.
For example the code coverage, the technical debt, best practises and many more

To start sonarqube run
```bash
npm run sonarqube:start
```
and visit [the local server](http://localhost:9000/).
The default credentials are `admin:admin`.

To stop sonarqube run
```bash
npm run sonarqube:stop
```

To report your testcoverage just run
```bash
npm run sonarqube:report YOUR_KEY
```
or
```bash
sh ./scripts/report_sonarqube.sh -k YOUR_KEY
```
1 change: 0 additions & 1 deletion src/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = class extends Generator {
token: string | null;
constructor(args: string | string[], config: any) {
super(args, config);
console.log({ config });
this.token = config.token;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/enums/filenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export enum Filenames {
SONARQUBE_REPORT_SCRIPT = 'report_sonarqube.sh',
DOCKER_COMPOSE = 'docker-compose.yml',
GIT_IGNORE = '.gitignore',
LICENSE = 'LICENSE.md',
README = 'README.md',

SRC_FOLDER = 'src',
SRIPTS_FOLDER = 'scripts',
Expand Down
4 changes: 3 additions & 1 deletion src/lib/enums/messages.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export enum Messages {
PROJECT_NAME = 'Your project name',
PROJECT_NAME = 'Your project name (only lowercase)',
AUTHOR = 'Your name',
FEATURES = 'Which features you want to enable?',
SONARQUBE = 'Would you like to enable sonarqube?',
SONARQUBE_TOKEN = 'Enter your sonarqube token here (leave empty to configurate later).',
LINT_STAGED = 'Would you like to lint your staged files before commiting?',
WITH_LICENSE = "Do you want to generate a license (chose one in the next step)?",
LICENSE = "Which license?"
}
2 changes: 2 additions & 0 deletions src/lib/enums/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export enum Names {
SONARQUBE = 'sonarqube',
SONARQUBE_TOKEN = 'sonarqube-token',
LINT_STAGED = 'lint-staged',
WITH_LICENSE = 'with-license',
LICENSE = 'license',
}
12 changes: 11 additions & 1 deletion src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import axios from 'axios';

import { Features } from '../enums/features';

export const withFeature = (answers: any, feature: Features): boolean => {
return answers.features.some((_feature: Features) => _feature === feature);
};

export const getLicenses = async () => {
const { data } = await axios.get('https://api.github.com/licenses');
return data;
};

export const getLicense = async (key: string) => {
const { data } = await axios.get(`https://api.github.com/licenses/${key}`);
return data.body;
};
2 changes: 1 addition & 1 deletion src/lib/helpers/package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const rootPkg = require('../../package.json');
const rootPkg = require('../../../package.json');

export default rootPkg;
21 changes: 21 additions & 0 deletions src/license/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Generator from 'yeoman-generator';

import { Filenames } from '../lib/enums/filenames';
import { getLicense } from '../lib/helpers';

module.exports = class extends Generator {
license: string;
constructor(args: string | string[], config: any) {
super(args, config);
this.license = config.license;
}

async writing() {
const licenseContent = await getLicense(this.license);
this.fs.copyTpl(
this.templatePath(Filenames.LICENSE),
this.destinationPath(Filenames.LICENSE),
{ license: licenseContent }
);
}
};
1 change: 1 addition & 0 deletions src/license/templates/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- license %>

0 comments on commit 22084bb

Please sign in to comment.