Skip to content

Commit f54c16c

Browse files
committed
add npm init example
1 parent a1ea7fc commit f54c16c

File tree

8 files changed

+98
-9
lines changed

8 files changed

+98
-9
lines changed

.moban.d/docs/source/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ Documentation
2626
.. toctree::
2727

2828
usage
29-
29+
extended_usage
3030

3131
.. _moban: https://github.com/chfw/moban

docs/source/extended_usage.rst

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Custom use case: scaffolding a npm package
2+
================================================================================
3+
4+
As mentioned in previous section, **yehua** would take a yehua.yml from command
5+
line options. This section walk you through creating a npm package, **non-python
6+
package**.
7+
8+
If you are familiar with npm, you can try and compare with yehua::
9+
10+
$ npm init
11+
12+
Evaluation
13+
--------------------------------------------------------------------------------
14+
15+
Please first checkout yehua repository so that you will have the access to
16+
example directory. Then make sure you have **yehua** installed. Let's do these
17+
steps to evaluate it::
18+
19+
$ cd /tmp
20+
$ yehua
21+
22+
yehua.yml for npm package
23+
--------------------------------------------------------------------------------
24+
25+
Let us go through the variant yehua file.
26+
27+
configuration section
28+
********************************************************************************
29+
30+
31+
32+
.. literalinclude:: ../../examples/npm-init/yehua.yml
33+
:lines: 12-15
34+

docs/source/usage.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
How it works
1+
Default use case: scaffolding a python package
22
================================================================================
33

44
**yehua** reads a configuration file named yehua.yml which then instruct **yehua
55
to ask questions to collect project specific variables, and create the file
6-
structures. Its default usage is to create a blank python package but it is not
7-
limited to it. You can provide your own yehua file and do something else.
6+
structures.
87
8+
It takes a yehua file from command line option, or YEHUA_FILE in the environment
9+
varaible. If none is found, it resorts to the default yehua.yml file included
10+
in the package. And the default yehua.yml create a blank python package.
911

10-
Default behaviour
12+
13+
Default yehua.yml file
1114
--------------------------------------------------------------------------------
1215

13-
Let us go through the default yehua file. And then talk about the customization.
16+
Let us go through the default yehua file. And then talk about the customization
17+
in the next chapter
1418

1519
configuration section
1620
********************************************************************************

examples/npm-init/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello world");

examples/npm-init/package.json.jj2

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "{{project_name}}",
3+
"version": "{{version}}",
4+
"description": "{{description}}",
5+
"main": "{{entry_point}}",
6+
"scripts": {
7+
"test": "{{test_command}}",
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "{{git_repo}}"
12+
},
13+
"keywords": [
14+
"{{keywords}}"
15+
],
16+
"author": "{{author}}",
17+
"license": "{{license}}"
18+
}

examples/npm-init/yehua.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
introduction: |
2+
This utility will walk you through creating a package.json file.
3+
It only covers the most common items, and tries to guess sensible defaults.
4+
5+
See `npm help json` for definitive documentation on these fields
6+
and exactly what they do.
7+
8+
Use `npm install <pkg> --save` afterwards to install a package and
9+
save it as a dependency in the package.json file.
10+
11+
Press ^C at any time to quit.
12+
configuration:
13+
template_path: .
14+
static_path: .
15+
questions:
16+
- project_name: "name:"
17+
- version: "version:"
18+
- description: "description:"
19+
- entry_point: "entry point:"
20+
- test_command: "test command:"
21+
- git_repo: "git repository:"
22+
- keywords: "keywords:"
23+
- author: "author:"
24+
- license: "license:"
25+
templates:
26+
- package.json: package.json.jj2
27+
static:
28+
- index.js: index.js

yehua/project.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ def __init__(self, yehua_file):
2121
self._template_yehua_file()
2222

2323
def create_all_directories(self):
24-
temp = {self.answers['project_name']: self.directives['layout']}
25-
utils.make_directories(None, temp)
24+
folder_tree = {
25+
self.answers['project_name']: self.directives.get('layout', None)
26+
}
27+
utils.make_directories(None, folder_tree)
2628

2729
def templating(self):
2830
for template in self.directives['templates']:
@@ -99,7 +101,7 @@ def raise_complex_question(question):
99101
suggested_answers = sorted(subq.keys())
100102
long_question = [subquestion] + suggested_answers
101103
choice = '(%s): ' % (
102-
','.join([str(x) for x in xrange(1, len(long_question))]))
104+
','.join([str(x) for x in range(1, len(long_question))]))
103105
long_question.append(choice)
104106
a = utils.yehua_input('\n'.join(long_question))
105107
for key in suggested_answers:

yehua/utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def make_directories(parent, node_dictionary):
3737
else:
3838
the_parent = key
3939
mkdir(the_parent)
40+
if value is None:
41+
continue
4042
for item in value:
4143
if isinstance(item, dict):
4244
make_directories(the_parent, item)

0 commit comments

Comments
 (0)