@@ -5,38 +5,80 @@ const fs = require('fs-extra');
5
5
const meow = require ( 'meow' ) ;
6
6
const puppeteer = require ( 'puppeteer' ) ;
7
7
const liveServer = require ( 'live-server' ) ;
8
+ const chalk = require ( 'chalk' ) ;
9
+ const ora = require ( 'ora' ) ;
8
10
9
11
const cli = meow ( {
10
12
description : false ,
11
13
help : `
12
- NAME
13
- onepunch -- create presentation with web technology
14
+ ${ chalk . bold ( 'NAME' ) }
14
15
15
- SYNOPSIS
16
- onepunch init [-n directory_name]
16
+ ${ chalk . bold ( 'onepunch' ) } -- create presentations with web technology
17
+
18
+ ${ chalk . bold ( 'DESCRIPTION' ) }
19
+
20
+ ${ chalk . bold ( 'onepunch' ) } is a command-line interface useful to create presentations with web technology.
21
+ Moreover, thanks to puppeteer, ${ chalk . bold ( 'onepunch' ) } can print the presentation in a PDF file.
22
+
23
+ ${ chalk . bold ( 'onepunch' ) } is designed for designers, it does not provide any default style.
24
+ Designers can define custom styles by writing CSS files and linking them in the ${ chalk . underline ( 'index.html' ) } .
25
+
26
+ ${ chalk . bold ( 'onepunch' ) } is open source software licensed under the MIT License,
27
+ please visit https://github.com/nextbitlabs/onepunch for further details.
28
+
29
+ ${ chalk . bold ( 'SYNOPSIS' ) }
30
+
31
+ ${ chalk . bold ( 'onepunch init' ) } [${ chalk . bold ( '-n' ) } ${ chalk . italic ( 'directory_name' ) } ]
17
32
Initialize a presentation.
18
33
19
- onepunch serve
34
+ ${ chalk . bold ( ' onepunch serve' ) }
20
35
Open the presentation in the browser.
21
36
22
- onepunch print [-i htmlfile] [-o pdffile]
37
+ ${ chalk . bold ( ' onepunch print' ) } [ ${ chalk . bold ( '-i' ) } ${ chalk . italic ( ' htmlfile' ) } ] [${ chalk . bold ( '-o' ) } ${ chalk . italic ( ' pdffile' ) } ]
23
38
Print the presentation in a PDF file.
24
39
25
- OPTIONS
26
- -n or --name directory_name
27
- Specify the name of the directory where the project is initialized. Defaults to onepunch-presentation.
28
-
29
- -i or --input htmlfile
30
- Specify the HTML file to print, defaults to "index.html".
31
-
32
- -o or --output pdffile
33
- Specify the name of the PDF file in output, defaults to "index.pdf".
34
-
35
- --version
40
+ ${ chalk . bold ( 'onepunch update' ) }
41
+ Update files in the ${ chalk . underline ( 'src' ) } directory according to the latest release.
42
+ Please note that any custom change inside directory ${ chalk . underline ( 'src' ) } will be overwritten.
43
+
44
+ ${ chalk . bold ( 'OPTIONS' ) }
45
+
46
+ ${ chalk . bold ( '-n' ) } or ${ chalk . bold ( '--name' ) } ${ chalk . italic ( 'directory_name' ) }
47
+ Specify the name of the directory where the project is initialized. Defaults to ${ chalk . underline ( 'onepunch-presentation' ) } .
48
+
49
+ ${ chalk . bold ( '-i' ) } or ${ chalk . bold ( '--input' ) } ${ chalk . italic ( 'htmlfile' ) }
50
+ Specify the HTML file to print, defaults to ${ chalk . underline ( 'index.html' ) } .
51
+
52
+ ${ chalk . bold ( '-o' ) } or ${ chalk . bold ( '--output' ) } ${ chalk . italic ( 'pdffile' ) }
53
+ Specify the name of the PDF file in output, defaults to ${ chalk . underline ( 'index.pdf' ) } .
54
+
55
+ ${ chalk . bold ( '--version' ) }
36
56
Display the version number.
37
57
38
- --help
58
+ ${ chalk . bold ( ' --help' ) }
39
59
Display the documentation.
60
+
61
+ ${ chalk . bold ( 'LICENSE' ) }
62
+
63
+ Copyright 2020 Nextbit <[email protected] > (https://nextbit.it/)
64
+
65
+ Permission is hereby granted, free of charge, to any person obtaining a copy
66
+ of this software and associated documentation files (the "Software"), to deal
67
+ in the Software without restriction, including without limitation the rights
68
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
69
+ copies of the Software, and to permit persons to whom the Software is
70
+ furnished to do so, subject to the following conditions:
71
+
72
+ The above copyright notice and this permission notice shall be included in
73
+ all copies or substantial portions of the Software.
74
+
75
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
76
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
77
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
78
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
79
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
80
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
81
+ THE SOFTWARE.
40
82
` ,
41
83
flags : {
42
84
name : {
@@ -67,6 +109,9 @@ switch (cli.input[0]) {
67
109
case 'print' :
68
110
print ( cli . flags ) ;
69
111
break ;
112
+ case 'update' :
113
+ update ( ) ;
114
+ break ;
70
115
default :
71
116
cli . showHelp ( ) ;
72
117
break ;
@@ -82,19 +127,37 @@ function serve() {
82
127
}
83
128
84
129
function init ( flags ) {
130
+ const spinner = ora ( 'Initializing ...' ) . start ( ) ;
85
131
const { name} = flags ;
86
132
const presentationPath = path . resolve ( name ) ;
87
- const presentationName = path . basename ( presentationPath ) ;
88
133
89
134
if ( fs . existsSync ( presentationPath ) ) {
90
- abort ( `The directory ' ${ presentationName } ' is already existing.` ) ;
135
+ abort ( `The directory ${ chalk . underline ( name ) } is already existing.` , spinner ) ;
91
136
}
92
137
93
138
fs . mkdirSync ( presentationPath ) ;
94
139
fs . copySync ( path . resolve ( __dirname , 'template' ) , presentationPath ) ;
140
+ spinner . succeed ( `Directory ${ chalk . underline ( name ) } has been initialized.` ) ;
141
+ }
142
+
143
+ function update ( ) {
144
+ const spinner = ora ( 'Updating ...' ) . start ( ) ;
145
+ const file = 'onepunch.json' ;
146
+
147
+ if ( ! fs . existsSync ( file ) ) {
148
+ abort ( `File ${ chalk . underline ( 'onepunch.json' ) } is not present. Are you sure this is the right directory?` , spinner ) ;
149
+ }
150
+
151
+ fs . copySync (
152
+ path . resolve ( __dirname , 'template/src' ) ,
153
+ 'src' ,
154
+ { overwrite : true } ,
155
+ ) ;
156
+ spinner . succeed ( `Directory ${ chalk . underline ( 'src' ) } has been updated to release ${ cli . pkg . version } .` ) ;
95
157
}
96
158
97
159
function print ( flags ) {
160
+ const spinner = ora ( 'Printing ...' ) . start ( ) ;
98
161
const { input, output} = flags ;
99
162
100
163
liveServer . start ( {
@@ -120,11 +183,17 @@ function print(flags) {
120
183
} ) ;
121
184
await browser . close ( ) ;
122
185
liveServer . shutdown ( ) ;
186
+ spinner . succeed ( `File ${ chalk . underline ( output ) } has been successfully created.` ) ;
123
187
} ) ( ) ;
124
188
}
125
189
126
- function abort ( message ) {
127
- console . error ( message ) ;
190
+ function abort ( message , spinner = null ) {
191
+ if ( spinner ) {
192
+ spinner . fail ( message ) ;
193
+ } else {
194
+ console . error ( message ) ;
195
+ }
196
+
128
197
console . error ( 'Aborting.' ) ;
129
198
process . exit ( 1 ) ;
130
199
}
0 commit comments