Skip to content

Commit c6435a3

Browse files
Merge pull request #6 from nextbitlabs/add-update-command
Add update command
2 parents fb4fa3e + c58c272 commit c6435a3

File tree

5 files changed

+362
-51
lines changed

5 files changed

+362
-51
lines changed

index.js

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,80 @@ const fs = require('fs-extra');
55
const meow = require('meow');
66
const puppeteer = require('puppeteer');
77
const liveServer = require('live-server');
8+
const chalk = require('chalk');
9+
const ora = require('ora');
810

911
const cli = meow({
1012
description: false,
1113
help: `
12-
NAME
13-
onepunch -- create presentation with web technology
14+
${chalk.bold('NAME')}
1415
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')}]
1732
Initialize a presentation.
1833
19-
onepunch serve
34+
${chalk.bold('onepunch serve')}
2035
Open the presentation in the browser.
2136
22-
onepunch print [-i htmlfile] [-o pdffile]
37+
${chalk.bold('onepunch print')} [${chalk.bold('-i')} ${chalk.italic('htmlfile')}] [${chalk.bold('-o')} ${chalk.italic('pdffile')}]
2338
Print the presentation in a PDF file.
2439
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')}
3656
Display the version number.
3757
38-
--help
58+
${chalk.bold('--help')}
3959
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.
4082
`,
4183
flags: {
4284
name: {
@@ -67,6 +109,9 @@ switch (cli.input[0]) {
67109
case 'print':
68110
print(cli.flags);
69111
break;
112+
case 'update':
113+
update();
114+
break;
70115
default:
71116
cli.showHelp();
72117
break;
@@ -82,19 +127,37 @@ function serve() {
82127
}
83128

84129
function init(flags) {
130+
const spinner = ora('Initializing ...').start();
85131
const {name} = flags;
86132
const presentationPath = path.resolve(name);
87-
const presentationName = path.basename(presentationPath);
88133

89134
if (fs.existsSync(presentationPath)) {
90-
abort(`The directory '${presentationName}' is already existing.`);
135+
abort(`The directory ${chalk.underline(name)} is already existing.`, spinner);
91136
}
92137

93138
fs.mkdirSync(presentationPath);
94139
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}.`);
95157
}
96158

97159
function print(flags) {
160+
const spinner = ora('Printing ...').start();
98161
const {input, output} = flags;
99162

100163
liveServer.start({
@@ -120,11 +183,17 @@ function print(flags) {
120183
});
121184
await browser.close();
122185
liveServer.shutdown();
186+
spinner.succeed(`File ${chalk.underline(output)} has been successfully created.`);
123187
})();
124188
}
125189

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+
128197
console.error('Aborting.');
129198
process.exit(1);
130199
}

0 commit comments

Comments
 (0)