Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
obrassard committed Oct 3, 2020
2 parents 83e018b + 003b752 commit 2440a77
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 34 deletions.
118 changes: 98 additions & 20 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module.exports = class extends Generator {
super(args, opts);
this.argument("appname", { type: String, required: true });
this.options.appname = this.options.appname.replace(/[^a-z0-9]/gi, '-').toLowerCase();
this._packagesQueue = [];
this._devPackagesQueue = [];
}

async prompting() {
Expand All @@ -23,6 +25,15 @@ module.exports = class extends Generator {
// default: false,
// message: "Add express.js ?"
// },
{
type: "list",
name: "packageManager",
default: "yarn",
message: "Which package manager would you like to use ?",
choices: [
'npm', 'yarn'
]
},
{
type: "confirm",
name: "jest",
Expand All @@ -34,8 +45,16 @@ module.exports = class extends Generator {
name: "docker",
default: true,
message: "Add Docker config ?"
},
{
type: "confirm",
name: "ghActions",
default: true,
message: "Use GitHub Actions for CI ?"
}
]);

this.options.runCmd = this.answers.packageManager == 'npm' ? 'npm run': 'yarn';
}

writing() {
Expand All @@ -46,6 +65,8 @@ module.exports = class extends Generator {
appname: this.options.appname,
nodemon: this.answers.nodemon,
jest: this.answers.jest,
pkg : this.answers.packageManager,
runCmd: this.options.runCmd
}
);

Expand All @@ -56,6 +77,8 @@ module.exports = class extends Generator {
docker: this.answers.docker,
nodemon: this.answers.nodemon,
jest: this.answers.jest,
pkg : this.answers.packageManager,
runCmd: this.options.runCmd
}
);

Expand All @@ -75,14 +98,22 @@ module.exports = class extends Generator {
)

if (this.answers.docker) {
this.fs.copy(
this.templatePath('Dockerfile'),
this.fs.copyTpl(
this.templatePath('Dockerfile.ejs'),
this.destinationPath(`./${this.options.appname}/Dockerfile`),
{
pkg : this.answers.packageManager,
runCmd: this.options.runCmd
}
)

this.fs.copy(
this.templatePath('docker-compose.yml'),
this.fs.copyTpl(
this.templatePath('docker-compose.yml.ejs'),
this.destinationPath(`./${this.options.appname}/docker-compose.yml`),
{
pkg : this.answers.packageManager,
runCmd: this.options.runCmd
}
)
}

Expand All @@ -104,38 +135,85 @@ module.exports = class extends Generator {
this.destinationPath(`./${this.options.appname}/nodemon.json`),
)
}

if (this.answers.ghActions) {
this.fs.copyTpl(
this.templatePath('ga-node-ci.yml.ejs'),
this.destinationPath(`./${this.options.appname}/.github/workflows/node-ci.yml`),
{
pkg : this.answers.packageManager
}
)
}
}

install() {
this.npmInstall(['typescript'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['@types/node'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['ts-node'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['rimraf'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['dotenv'], null, {'cwd': this.options.appname});
this.log('Installing packages ...')
this._addToPackagesQueue('typescript', true);
this._addToPackagesQueue('@types/node', true);
this._addToPackagesQueue('ts-node', true);
this._addToPackagesQueue('rimraf', true);
this._addToPackagesQueue('dotenv');

if (this.answers.nodemon) {
this.npmInstall(['nodemon'], { 'save-dev': true }, { 'cwd': this.options.appname });
this._addToPackagesQueue('nodemon', true);
}

if (this.answers.express) {
this.npmInstall(['express', null, {'cwd': this.options.appname}]);
this.npmInstall(['@types/expres'], { 'save-dev': true }, { 'cwd': this.options.appname });
this._addToPackagesQueue('express');
this._addToPackagesQueue('@types/expres', true);
}

if (this.answers.jest) {
this.npmInstall(['jest'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['jest-extended'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['ts-jest'], { 'save-dev': true }, { 'cwd': this.options.appname });
this.npmInstall(['@types/jest'], { 'save-dev': true }, { 'cwd': this.options.appname });
// this.npmInstall(['supertest'], { 'save-dev': true }, { 'cwd': this.options.appname });
this._addToPackagesQueue('jest', true);
this._addToPackagesQueue('jest-extended', true);
this._addToPackagesQueue('ts-jest', true);
this._addToPackagesQueue('@types/jest', true);
this._addToPackagesQueue('supertest', true);
}

this._installPackages();
}


end() {
this.log('Intializing git')
this._intializeGit();
this.log('All done ! Congrats ✨')
}

_intializeGit() {
this.log('Intializing Git ...')
this.spawnCommandSync('git', ['init', this.options.appname]);
this.spawnCommandSync('git', ['add', '*'], { 'cwd': this.options.appname });
this.spawnCommandSync('git', ['add', '.*'], { 'cwd': this.options.appname });
this.spawnCommandSync('git', ['commit', '-m', 'Initial commit'], { 'cwd': this.options.appname });
}

this.log('All done ! Congrats ✨')
_addToPackagesQueue(name, dev = false) {
if (dev) {
this._devPackagesQueue.push(name)
} else {
this._packagesQueue.push(name)
}
}

_installPackages() {

// Install required dependencies
if (this._packagesQueue.length) {
if (this.answers.packageManager == 'npm' ) {
this.npmInstall(this._packagesQueue, { 'save-dev': false }, { 'cwd': this.options.appname });
} else if (this.answers.packageManager == 'yarn') {
this.yarnInstall(this._packagesQueue, { 'dev': false }, { 'cwd': this.options.appname });
}
}

// Install required dev-dependencies
if (this._devPackagesQueue.length) {
if (this.answers.packageManager == 'npm' ) {
this.npmInstall(this._devPackagesQueue, { 'save-dev': true }, { 'cwd': this.options.appname });
} else if (this.answers.packageManager == 'yarn') {
this.yarnInstall(this._devPackagesQueue, { 'dev': true }, { 'cwd': this.options.appname });
}
}
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

FROM node:12-alpine
ARG APP_CODE_PATH_CONTAINER=/app
COPY ./ ${APP_CODE_PATH_CONTAINER}
WORKDIR ${APP_CODE_PATH_CONTAINER}
RUN npm install
RUN npm run build
CMD npm start
RUN <%= pkg %> install
RUN <%= runCmd %> build
CMD <%= runCmd %> start
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ services:
volumes:
- .:/app
working_dir: /app
command: npm run watch
command: <%= runCmd %> watch
14 changes: 14 additions & 0 deletions generators/app/templates/ga-node-ci.yml.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Node.js CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: '14'
- run: <%= pkg %> install
- run: npm run build --if-present
- run: npm run test --if-present
7 changes: 5 additions & 2 deletions generators/app/templates/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
"name": "<%= appname %>",
"version": "1.0.0",
"description": "",
"license": "UNLICENSED",
"repository": "",
"main": "main.ts",
"scripts": {
"start": "node build/main.js",
"dev": "ts-node --files ./src/main.ts",
"build": "npm run clean && tsc",
"build": "tsc",
<% if (nodemon) { %>"watch": "nodemon",<% }%>
<% if (jest) { %>"test": "jest --colors",<% } %>
"clean": "rimraf dist"
"clean": "rimraf build",
"prebuild": "rimraf build"
}
}
13 changes: 6 additions & 7 deletions generators/app/templates/readme.md.ejs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

<img src="https://i.imgur.com/fAmJYWo.png" alt="TypeScript Logo" width="100"/>

# TypeScript Starter Project

### Available tasks

**npm run build** - Transpile TS files
**npm run dev** - Run project with TS-Node
**npm start** - Start `node` on transpiled files (in `./build`)
**npm run clean** - Remove `./build` directory
<% if (nodemon) { %>**npm run watch** - Run project with autoreload on changes<% }%>
<% if (jest) { %>**npm test** - Run Jest unit tests<% }%>
- **<%= runCmd %> build** - Transpile TS files
- **<%= runCmd %> dev** - Run project with TS-Node
- **<%= runCmd %> start** - Start `node` on transpiled files (in `./build`)
- **<%= runCmd %> clean** - Remove `./build` directory
<% if (nodemon) { %>- **<%= runCmd %> watch** - Run project with autoreload on changes<% }%>
<% if (jest) { %>- **<%= runCmd %> test** - Run Jest unit tests<% }%>

<% if (docker) { %>
### Using docker ?
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-tsproj",
"version": "1.0.1",
"version": "1.0.2",
"description": "A light yeoman generator for TS projets scaffolding",
"repository": "https://github.com/obrassard/generator-ts.git",
"main": "",
Expand Down

0 comments on commit 2440a77

Please sign in to comment.