Skip to content

Commit ebea4bf

Browse files
authored
Merge pull request #22 from nmicht/feature/settings
Feature/settings
2 parents 574c4ae + 529204c commit ebea4bf

File tree

13 files changed

+105
-46
lines changed

13 files changed

+105
-46
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "10"

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Node.js Project Initializer
44

5-
[![License][license-image]][license-url] [![version][npm-image]][npm-url]
5+
[![License][license-image]][license-url] [![version][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url]
66

77
An [npm initializer][npm/init] to scaffold a node project and include basic tools like lint, testing, etc.
88

@@ -34,11 +34,10 @@ npm init nodejs-project path/to/new/project
3434
## What it does
3535

3636
1. Create the folder for the new project
37-
1. Guide you through a questionnarie to setup the project
37+
1. Guide you through a questionnaire to setup the project
3838
2. Initialize a git repository
3939
3. Copy the template files (src, eslintrc, gitignore, readme, etc)
40-
4. Can create a Github repository
41-
5. Handle the Github tokens for multiple accounts/users
40+
4. Create a Github repository
4241
5. Install eslint dependencies
4342
5. Install the selected testing dependencies
4443
6. Generate package.json with all the project details
@@ -47,22 +46,22 @@ npm init nodejs-project path/to/new/project
4746

4847
## About this package
4948

50-
The motivation started as a **DRY** thing.
49+
Every time that I start a new project in Node.js, I hate to go to other project folder, copy files like eslintrc, editorconfig; install the same dependencies, create folder structure, etc.
5150

52-
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.
51+
With this in mind, the motivation to build this package started as a **DRY** (Do not repeat yourself) thing.
5352

54-
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.
53+
This package is intended to automated the initialization of new Node.js projects and with this have a new folder with everything ready to work, basically an scaffolding tool.
5554

5655

5756
## Future features
5857

5958
1. Unit testing
60-
7. Options to create the project with params instead of questionnaire
59+
7. Options to create the project with parameters instead of questionnaire
6160
10. A good error handler
6261
11. Color for the console messages
6362
12. Improve the template structure (the one that is generated in the new project) to include unit test
6463
18. Option to questionnaire with all the default values
65-
2. Logic to handle multiple auth files and multiple github accounts
64+
2. Logic to handle multiple authentication files and multiple Github accounts
6665

6766
## Configure Github Authentication
6867

@@ -105,3 +104,6 @@ If you are planning to allow this script to create your Github repositories, is
105104
[npm-image]: https://img.shields.io/npm/v/create-nodejs-project.svg?style=for-the-badge&logo=npm
106105

107106
[npm/init]: https://docs.npmjs.com/cli/init#description
107+
108+
[downloads-url]: https://www.npmjs.com/package/create-nodejs-project
109+
[downloads-image]: https://img.shields.io/npm/dt/create-nodejs-project.svg?style=for-the-badge

create-nodejs-settings-example.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
"license": "GNU GPLv3",
3232
"version": "0.1.0"
3333
},
34+
"templates": [
35+
"nodejs-project",
36+
],
3437
"settingsPath": "/YOUR_PATH/create-nodejs-project/create-nodejs-settings.json",
3538
"templatesPath": "/YOUR_PATH/create-nodejs-project/templates",
36-
"nodejsTemplatePath": "/YOUR_PATH/create-nodejs-project/templates/nodejs-project",
3739
"licensesPath": "/YOUR_PATH/create-nodejs-project/templates/licenses"
3840
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"private": false,
2020
"scripts": {
2121
"postinstall": "node --no-warnings scripts/install.js",
22-
"start": "node --no-warnings src/index.js",
22+
"setup": "node --no-warnings scripts/setup.js",
2323
"cleanup": "node --no-warnings scripts/cleanup.js",
24+
"start": "node --no-warnings src/index.js",
2425
"test": "mocha"
2526
},
2627
"engines": {

scripts/install.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
1-
const questions = require('../src/questionnaire/questions');
1+
const fs = require('fs').promises;
2+
23
const settings = require('../src/settings');
34

45
/**
5-
* Install function for the package, it set up the github auth details
6-
* TODO Consider the case for a previous auth file with different tokens
6+
* Install function for the package, it set up all the settings details
77
*/
88
(async () => {
9-
let user;
10-
await settings.load();
11-
12-
const authFileAnswers = await questions.promptSettingsFile(settings.settingsPath);
13-
settings.settingsPath = authFileAnswers.settingsPath;
14-
159
try {
16-
user = await settings.firstUser();
10+
await fs.access(settings.settingsPath);
11+
await settings.load();
1712
} catch (e) {
18-
// console.log('fixme');
13+
settings.update();
1914
}
20-
21-
22-
const authUser = await questions.promptGithubUser(user.user || '');
23-
const authToken = await questions.promptAuthToken(user.user || '', user.token || '');
24-
25-
settings.githubAuth = {
26-
user: authUser.github.user,
27-
token: authToken.github.token,
28-
};
29-
30-
await settings.update();
31-
console.log(`Your settings file was updated on ${settings.settingsPath}`);
3215
})();

scripts/setup.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs').promises;
2+
3+
const questions = require('../src/questionnaire/questions');
4+
const settings = require('../src/settings');
5+
6+
/**
7+
* Install function for the package, it set up the github auth details
8+
* TODO Consider the case for a previous auth file with different tokens
9+
*/
10+
(async () => {
11+
let user;
12+
try {
13+
await fs.access(settings.settingsPath);
14+
await settings.load();
15+
} catch (e) {
16+
settings.update();
17+
}
18+
19+
const authFileAnswers = await questions.promptSettingsFile(settings.settingsPath);
20+
settings.settingsPath = authFileAnswers.settingsPath;
21+
22+
try {
23+
user = await settings.firstUser();
24+
} catch (e) {
25+
// console.log('fixme');
26+
}
27+
28+
29+
const authUser = await questions.promptGithubUser(user.user || '');
30+
const authToken = await questions.promptAuthToken(user.user || '', user.token || '');
31+
32+
settings.githubAuth = {
33+
user: authUser.github.user,
34+
token: authToken.github.token,
35+
};
36+
37+
await settings.update();
38+
console.log(`Your settings file was updated on ${settings.settingsPath}`);
39+
})();

src/project/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs').promises;
2+
const path = require('path');
23

34
const questionnaire = require('../questionnaire');
45
const gitHandler = require('../gitHandler');
@@ -40,6 +41,7 @@ class Project {
4041
* @param {String} [path=''] The full path for the project folder
4142
* @param {String} [year=''] The year to be used on the license
4243
* @param {Array} [testPackages=[]] The list of the test packages selected
44+
* @param {String} [template=''] The list of the test packages selected
4345
*/
4446
constructor({
4547
name = '',
@@ -66,9 +68,10 @@ class Project {
6668
},
6769
issueTracker = '',
6870
isPrivate = false,
69-
path = '',
71+
thePath = '',
7072
year = '',
7173
testPackages = [],
74+
theTemplate = '',
7275
}) {
7376
this.name = name;
7477
this.description = description;
@@ -94,9 +97,10 @@ class Project {
9497
};
9598
this.issueTracker = issueTracker;
9699
this.isPrivate = isPrivate;
97-
this.path = path;
100+
this.path = thePath;
98101
this.year = year || (new Date()).getFullYear();
99102
this.testPackages = testPackages;
103+
this.template = theTemplate;
100104
}
101105

102106
/**
@@ -207,7 +211,10 @@ class Project {
207211
* @return {Promise}
208212
*/
209213
async generateTemplateFiles() {
210-
await template.copyTemplate(Project.settings.nodejsTemplatePath, this.path);
214+
await template.copyTemplate(
215+
path.join(Project.settings.templatesPath, this.template),
216+
this.path
217+
);
211218

212219
return Promise.all([
213220
template.updateTemplateFiles(this.dictionary, this.path),

src/questionnaire/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ async function run(name, settings) {
2020
let currentAuthUser;
2121
let currentToken;
2222

23-
const resp = await questions.promptProjectDetails(name, settings.licenses, settings.testingPkgs);
23+
const resp = await questions.promptProjectDetails(
24+
name,
25+
settings.licenses,
26+
settings.testingPkgs,
27+
settings.templates
28+
);
2429

2530
if (!resp.useGithub) {
2631
remoteAnswers = await questions.promptGitRemoteDetails();

src/questionnaire/questions.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ const utils = require('../utils');
1212
* @param {String} defaults.license The default license for the project
1313
* @param {String} defaults.gitUserName The git username setup for the project
1414
* @param {String} defaults.gitUserEmail The git username setup for the project
15+
* @param {String} defaults.template The name for the default template
1516
* @param {Array} licenses The list of options for licenses
1617
* @param {Array} testingPkgs The list of options for testingPkgs
18+
* @param {Array} templates The list of options for templates
1719
* @return {Promise}
1820
*/
19-
async function promptProjectDetails(defaults, licenses, testingPkgs) {
21+
async function promptProjectDetails(defaults, licenses, testingPkgs, templates) {
2022
return inquirer.prompt([
2123
{
2224
type: 'input',
@@ -25,6 +27,14 @@ async function promptProjectDetails(defaults, licenses, testingPkgs) {
2527
default: defaults.projectName,
2628
},
2729

30+
{
31+
type: 'list',
32+
name: 'template',
33+
message: 'What kind of project are you creating?',
34+
choices: templates,
35+
default: defaults.template,
36+
},
37+
2838
{
2939
type: 'input',
3040
name: 'description',

src/settings/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const SETTINGS_PATH = path.resolve(path.join(__dirname, '..', '..', 'create-node
1010
* @class Settings
1111
*/
1212
class Settings {
13-
1413
constructor({
1514
lintPkgs = [
1615
'eslint',
@@ -36,13 +35,16 @@ class Settings {
3635
'MIT License',
3736
'ISC License',
3837
],
38+
templates = [
39+
'nodejs-project',
40+
],
3941
settingsPath = SETTINGS_PATH,
4042
templatesPath = path.resolve(path.join(__dirname, '..', '..', 'templates')),
41-
nodejsTemplatePath = path.resolve(path.join(__dirname, '..', '..', 'templates', 'nodejs-project')),
4243
licensesPath = path.resolve(path.join(__dirname, '..', '..', 'templates', 'licenses')),
4344
defaults = {
4445
license: 'GNU GPLv3',
4546
version: '0.1.0',
47+
template: 'nodejs-project',
4648
},
4749
githubAuth = {
4850
user: 'YOUR_USER',
@@ -59,10 +61,11 @@ class Settings {
5961
this.defaults = {
6062
license: defaults.license,
6163
version: defaults.version,
64+
template: defaults.template,
6265
};
66+
this.templates = templates;
6367
this.settingsPath = settingsPath;
6468
this.templatesPath = templatesPath;
65-
this.nodejsTemplatePath = nodejsTemplatePath;
6669
this.licensesPath = licensesPath;
6770
}
6871

@@ -98,7 +101,7 @@ class Settings {
98101
this.licenses = json.licenses;
99102
this.settingsPath = json.settingsPath;
100103
this.templatesPath = json.templatesPath;
101-
this.nodejsTemplatePath = json.nodejsTemplatePath;
104+
this.templates = json.templates;
102105
this.licensesPath = json.licensesPath;
103106
}
104107

0 commit comments

Comments
 (0)