UNPKG

2.45 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/* eslint-disable quotes */
4
5'use strict';
6
7const program = require('commander');
8const getVisitor = require('../lib/visitor').getVisitor;
9const notifier = require('../lib/update-notifier');
10
11const { parsePairs } = require('../lib/build/parser');
12
13const examples =
14 `
15 Examples:
16
17 $ fun init
18 $ fun init event-nodejs8
19 $ fun init foo/bar
20 $ fun init gh:foo/bar
21 $ fun init gl:foo/bar
22 $ fun init bb:foo/bar
23 $ fun init github:foo/bar
24 $ fun init gitlab:foo/bar
25 $ fun init bitbucket:foo/bar
26 $ fun init git+ssh://git@github.com/foo/bar.git
27 $ fun init hg+ssh://hg@bitbucket.org/bar/foo
28 $ fun init git@github.com:foo/bar.git
29 $ fun init https://github.com/foo/bar.git
30 $ fun init /path/foo/bar
31 $ fun init -n fun-app -V foo=bar /path/foo/bar
32 `;
33
34program
35 .name('fun init')
36 .usage('[options] [template]')
37 .description('Initialize a new project based on a template. A template can be a folder containing template metadata and boilerplate files, a name of a pre-built template, or a url that resolves to a template. You can find more information about template at https://yq.aliyun.com/articles/674364.')
38 .option('-o, --output-dir [path]', 'Where to output the initialized app into', '.')
39 .option('-n, --name [name]', 'The name of your project to be generated as a folder', '')
40 .option('-m, --merge [merge]', 'Merge into the template.[yml|yaml] file if it already exist', false)
41 .option('--no-input', 'Disable prompting and accept default values defined template config')
42 .option('-V, --var [vars]', 'Template variable', parsePairs)
43 .on('--help', () => {
44 console.log(examples);
45 })
46 .parse(process.argv);
47
48const context = {
49 name: program.name,
50 outputDir: program.outputDir,
51 merge: program.merge,
52 input: program.input,
53 vars: program.var || {}
54};
55
56if (program.args.length > 0) {
57 context.location = program.args[0];
58}
59
60notifier.notify();
61
62getVisitor().then(visitor => {
63 visitor.pageview('/fun/init').send();
64
65 require('../lib/commands/init')(context)
66 .then(() => {
67 visitor.event({
68 ec: 'init',
69 ea: `init ${context.location}`,
70 el: 'success',
71 dp: '/fun/init'
72 }).send();
73 })
74 .catch(error => {
75 visitor.event({
76 ec: 'init',
77 ea: `init ${context.location}`,
78 el: 'error',
79 dp: '/fun/init'
80 }).send();
81
82 require('../lib/exception-handler')(error);
83 });
84});
\No newline at end of file