Skip to content

Commit a0d530f

Browse files
committed
Merge branch 'stg'
2 parents 0ba6fe4 + cea553b commit a0d530f

24 files changed

+566
-286
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"extends": ["airbnb-base", "plugin:node/recommended"],
33
"rules": {
44
"no-console": off,
5+
"comma-dangle": [error, always-multiline, {functions: never}]
56
}
67
}

CHANGELOG.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Changelog
2+
3+
## 0.1.4
4+
5+
* Handle the Github tokens for multiple accounts/users
6+
* Fix problem with keywords on package.json
7+
* Post install script to setup github auth details
8+
9+
## 0.1.3
10+
11+
* Create the folder for the new project
12+
* Guide you through a questionnarie to setup the project
13+
* Initialize a git repo
14+
* Copy the template files (src, eslintrc, gitignore, readme, etc)
15+
* Can create a Github repository
16+
* Install eslint dependencies
17+
* Install the selected testing dependencies
18+
* Generate package.json with all the project details
19+
* Commit and push the initial commit

LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright 2019 Michelle Torres
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+42-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<img width="75px" height="75px" align="right" alt="Create NodeJS Project Logo" src="https://raw.githubusercontent.com/nmicht/create-nodejs-project/master/assets/create-nodejs-project.png" title="Create NodeJS Project"/>
2+
13
# Node Project Initializer
24

35
[![License][license-image]][license-url] [![version][npm-image]][npm-url]
@@ -15,46 +17,55 @@ An [npm initializer][npm/init] to scaffold a node project and include basic tool
1517

1618
## Usage
1719

18-
`npm init node-project path/to/project`
20+
1. Install the package as global
21+
```
22+
npm install -g create-nodejs-project
23+
```
24+
25+
2. You will be prompted for your Github information
26+
If you do not have the information at the moment, you can keep it empty.
27+
In order to create projects with Github integration, you will need to add the authentication information later. See [Github Auth](#configure-Github-authentication)
28+
29+
3. Create your project
30+
```
31+
npm init nodejs-project path/to/new/project
32+
```
1933

2034
## What it does
2135

2236
1. Create the folder for the new project
2337
1. Guide you through a questionnarie to setup the project
24-
2. Initialize a git repo
38+
2. Initialize a git repository
2539
3. Copy the template files (src, eslintrc, gitignore, readme, etc)
2640
4. Can create a Github repository
27-
5. Install eslint
41+
5. Handle the Github tokens for multiple accounts/users
42+
5. Install eslint dependencies
2843
5. Install the selected testing dependencies
2944
6. Generate package.json with all the project details
3045
7. Commit and push the initial commit
3146

3247
## About this package
3348

34-
So, this started as a dry thing.
35-
I'm not expert with NodeJS, but every time that I start a new project, I hate to go to other project, copy files like eslintrc, editorconfig, install the same dependencies, create folder structure, etc.
36-
So, the idea is to have a create package to use it in the form of:
37-
`npm init node-project path/to/project`
38-
and with this have a new folder my-new-project with everything ready to work.
49+
The motivation started as a **DRY** thing.
50+
51+
I'm not expert with NodeJS, but every time that I start a new project, I hate to go to other project, copy files like eslintrc, editorconfig, install the same dependencies, create folder structure, etc.
52+
53+
So, the idea is to have an automated way to initialize new NodeJS projects and with this have a new folder with everything ready to work in what really matters.
3954

40-
I know there are a lot of similar packages out there, but the idea is to learn more about nodejs api, handling files, packages, etc.
4155

4256
## Future features
4357

44-
1. Fix the structure of modules, classes and etc
45-
4. Add unit testing
46-
7. Add options to create the project with params instead of questionnaire
47-
10. Add a good error handler
58+
1. Unit testing
59+
7. Options to create the project with params instead of questionnaire
60+
10. A good error handler
4861
11. Color for the console messages
49-
12. Modify template structure (the one that is generated in the new project) to include unit test
62+
12. Improve the template structure (the one that is generated in the new project) to include unit test
5063
13. Include license files to the template copy/update process
51-
14. A logger ? (just for learning)
52-
17. Ability to handle auth for different github accounts
5364
18. Option to questionnaire with all the default values
5465

55-
## Github Auth
66+
## Configure Github Authentication
5667

57-
If you are planning to allow this script to create your github repositories, is required to generate a Github Token.
68+
If you are planning to allow this script to create your Github repositories, is required to generate a Github Token.
5869

5970
1. Visit https://github.com/settings/tokens.
6071
2. Click Generate new token.
@@ -70,24 +81,31 @@ If you are planning to allow this script to create your github repositories, is
7081
```
7182
3. Click Generate token.
7283
4. Copy the generated string to a safe place, such as a password safe.
73-
5. Open Terminal and add the github token. Note: The file may be empty, you can use `auth-example.json` to copy and paste.
84+
5. Open Terminal and add the Github token.
7485

7586
```
7687
# nano ~/auth.json
7788
7889
{
79-
"github-oauth": {
80-
"github.com": "YOUR_TOKEN"
81-
}
90+
"Github": [
91+
{
92+
"user": "YOUR_USER",
93+
"token": "YOUR_TOKEN"
94+
},
95+
{
96+
"user": "OTHER_USER",
97+
"token": "OTHER_TOKEN"
98+
}
99+
]
82100
}
83101
```
84102

85103

86104

87105
[license-url]: LICENSE
88-
[license-image]: https://img.shields.io/github/license/ahmadnassri/node-create.svg?style=for-the-badge&logo=circleci
106+
[license-image]: https://img.shields.io/github/license/nmicht/create-nodejs-project.svg?style=for-the-badge&logo=appveyor
89107

90108
[npm-url]: https://www.npmjs.com/package/create-nodejs-project
91-
[npm-image]: https://img.shields.io/npm/v/@nmicht/create.svg?style=for-the-badge&logo=npm
109+
[npm-image]: https://img.shields.io/npm/v/create-nodejs-project.svg?style=for-the-badge&logo=npm
92110

93111
[npm/init]: https://docs.npmjs.com/cli/init#description

assets/create-nodejs-project.png

8.66 KB
Loading

auth-example.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
2-
"github": {
3-
"token": "YOUR_TOKEN"
4-
}
2+
"github": [
3+
{
4+
"user": "YOUR_USER",
5+
"token": "YOUR_TOKEN"
6+
},
7+
{
8+
"user": "OTHER_USER",
9+
"token": "OTHER_TOKEN"
10+
}
11+
]
512
}

install/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const fs = require('fs');
2+
const inquirer = require('inquirer');
3+
4+
const utils = require('../src/utils');
5+
6+
async function install() {
7+
const authPath = utils.fs.resolvePath('~/auth.json');
8+
9+
const answers = await inquirer.prompt([
10+
{
11+
type: 'input',
12+
name: 'user',
13+
message: 'What is your github user?',
14+
},
15+
16+
{
17+
type: 'input',
18+
name: 'token',
19+
message: 'What is your GitHub token?',
20+
},
21+
]);
22+
23+
// Write file
24+
const data = {
25+
github: [
26+
{
27+
user: answers.user,
28+
token: answers.token,
29+
},
30+
],
31+
};
32+
33+
fs.writeFileSync(authPath, JSON.stringify(data));
34+
console.log(`File ${authPath} created with your github details`);
35+
}
36+
37+
install();

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-nodejs-project",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "An npm initializer to scaffold a node project and include basic tools like lint, testing, etc.",
55
"main": "src/index.js",
66
"bin": "src/index.js",
@@ -18,9 +18,9 @@
1818
"license": "MIT",
1919
"private": false,
2020
"scripts": {
21+
"postinstall": "node install/index.js",
2122
"start": "node src/index.js",
22-
"cleanup": "node test/cleanup.js a-demo-project",
23-
"test": "node src/index.js ../a-demo-project"
23+
"cleanup": "node test/cleanup.js"
2424
},
2525
"engines": {
2626
"node": ">=10.0.0"

src/auth/index.js

+79-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,103 @@
11
const fs = require('fs');
2-
const path = require('path');
32

43
const settings = require('../settings');
4+
const utils = require('../utils');
55

66
/**
77
* Get the Github token from the auth file
8-
* @param {String} [path=''] The path for the auth.json file
9-
* @return {String} The github token
8+
* @param {String} [user=undefined] The user owner of the token
9+
* @param {String} [path=''] The path for the auth.json file
10+
* @return {String} The github token
1011
*/
11-
async function getToken(jsonPath = '') {
12+
function getToken(user = undefined, jsonPath = '') {
1213
let auth = {};
14+
let ghData;
1315
const authPath = jsonPath || settings.authPath;
14-
const authFile = path.resolve(authPath);
16+
const authFile = utils.fs.resolvePath(authPath);
1517

1618
try {
1719
auth = JSON.parse(fs.readFileSync(authFile, 'utf8'));
1820
} catch (error) {
1921
throw error;
2022
}
2123

22-
if (!auth.github.token) {
24+
if (user) {
25+
[ghData] = auth.github.filter(obj => obj.user === user);
26+
} else {
27+
[ghData] = auth.github;
28+
}
29+
30+
const { token } = ghData;
31+
32+
if (!token) {
2333
throw new Error('Token missing');
2434
}
2535

26-
return auth.github.token;
36+
return token;
37+
}
38+
39+
/**
40+
* Update the Token
41+
* @param {String} [user=undefined] The user owner of the token
42+
* @param {String} token The token
43+
* @param {String} [jsonPath=''] The path for the auth.json file
44+
* @return {Boolean} True in case the file gets updated
45+
*/
46+
function updateToken(user = undefined, token, jsonPath = '') {
47+
let auth = {};
48+
let currentToken = '';
49+
let userIndex = 0;
50+
const authPath = jsonPath || settings.authPath;
51+
const authFile = utils.fs.resolvePath(authPath);
52+
53+
try {
54+
auth = JSON.parse(fs.readFileSync(authFile, 'utf8'));
55+
} catch (error) {
56+
throw error;
57+
}
58+
59+
currentToken = auth.github[0].token;
60+
61+
if (user) {
62+
for (let k = 0; k < auth.github.length; k += 1) {
63+
if (auth.github[k].user === user) {
64+
userIndex = k;
65+
currentToken = auth.github[k].token;
66+
break;
67+
}
68+
}
69+
}
70+
71+
if (currentToken !== token) {
72+
auth.github[userIndex].token = token;
73+
fs.writeFileSync(authFile, JSON.stringify(auth));
74+
return true;
75+
}
76+
77+
return false;
78+
}
79+
80+
/**
81+
* Get the first user from the auth file
82+
* @param {String} [jsonPath=''] The auth.json file path
83+
* @return {String} The user
84+
*/
85+
function getFirstUser(jsonPath = '') {
86+
let auth = {};
87+
const authPath = jsonPath || settings.authPath;
88+
const authFile = utils.fs.resolvePath(authPath);
89+
90+
try {
91+
auth = JSON.parse(fs.readFileSync(authFile, 'utf8'));
92+
} catch (error) {
93+
throw error;
94+
}
95+
96+
return auth.github[0].user;
2797
}
2898

2999
module.exports = {
30100
getToken,
101+
updateToken,
102+
getFirstUser,
31103
};

src/gitHandler/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function userValue(prop) {
99
let data = '';
1010

1111
try {
12-
data = await utils.execp(`git config user.${prop}`);
12+
data = await utils.process.execp(`git config user.${prop}`);
1313
} catch (error) {
1414
console.error(error);
1515
}
@@ -26,7 +26,7 @@ async function init(path = '.') {
2626
let resp = '';
2727

2828
try {
29-
resp = await utils.execp('git init', path);
29+
resp = await utils.process.execp('git init', path);
3030
} catch (error) {
3131
console.error(error);
3232
}
@@ -44,7 +44,7 @@ async function commit(path = '.', msg = 'Initial commit') {
4444
let resp = '';
4545

4646
try {
47-
resp = await utils.execp(`git add . && git commit -m'${msg}'`, path);
47+
resp = await utils.process.execp(`git add . && git commit -m'${msg}'`, path);
4848
} catch (error) {
4949
console.error(error);
5050
}
@@ -63,7 +63,7 @@ async function addRemote(path = '.', url, remote = 'origin') {
6363
let resp = '';
6464

6565
try {
66-
resp = await utils.execp(`git remote add ${remote} ${url}`, path);
66+
resp = await utils.process.execp(`git remote add ${remote} ${url}`, path);
6767
} catch (error) {
6868
console.error(error);
6969
}
@@ -82,7 +82,7 @@ async function push(path = '.', remote = 'origin', branch = 'master') {
8282
let resp = '';
8383

8484
try {
85-
resp = await utils.execp(`git push ${remote} ${branch}`, path);
85+
resp = await utils.process.execp(`git push ${remote} ${branch}`, path);
8686
} catch (error) {
8787
console.error(error);
8888
}

0 commit comments

Comments
 (0)