UNPKG

2.52 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');
10const { autoExit } = require('../lib/unref-timeout');
11
12const { parsePairs } = require('../lib/build/parser');
13
14const examples =
15 `
16 Examples:
17
18 $ fun init
19 $ fun init event-nodejs8
20 $ fun init foo/bar
21 $ fun init gh:foo/bar
22 $ fun init gl:foo/bar
23 $ fun init bb:foo/bar
24 $ fun init github:foo/bar
25 $ fun init gitlab:foo/bar
26 $ fun init bitbucket:foo/bar
27 $ fun init git+ssh://git@github.com/foo/bar.git
28 $ fun init hg+ssh://hg@bitbucket.org/bar/foo
29 $ fun init git@github.com:foo/bar.git
30 $ fun init https://github.com/foo/bar.git
31 $ fun init /path/foo/bar
32 $ fun init -n fun-app -V foo=bar /path/foo/bar
33 `;
34
35program
36 .name('fun init')
37 .usage('[options] [template]')
38 .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.')
39 .option('-o, --output-dir [path]', 'Where to output the initialized app into', '.')
40 .option('-n, --name [name]', 'The name of your project to be generated as a folder', '')
41 .option('-m, --merge [merge]', 'Merge into the template.[yml|yaml] file if it already exist', false)
42 .option('--no-input', 'Disable prompting and accept default values defined template config')
43 .option('-V, --var [vars]', 'Template variable', parsePairs)
44 .on('--help', () => {
45 console.log(examples);
46 })
47 .parse(process.argv);
48
49const context = {
50 name: program.name,
51 outputDir: program.outputDir,
52 merge: program.merge,
53 input: program.input,
54 vars: program.var || {}
55};
56
57if (program.args.length > 0) {
58 context.location = program.args[0];
59}
60
61notifier.notify();
62
63getVisitor().then(visitor => {
64 visitor.pageview('/fun/init').send();
65
66 require('../lib/commands/init')(context)
67 .then(() => {
68 visitor.event({
69 ec: 'init',
70 ea: `init ${context.location}`,
71 el: 'success',
72 dp: '/fun/init'
73 }).send();
74
75 autoExit();
76 })
77 .catch(error => {
78 visitor.event({
79 ec: 'init',
80 ea: `init ${context.location}`,
81 el: 'error',
82 dp: '/fun/init'
83 }).send();
84
85 require('../lib/exception-handler')(error);
86 });
87});
\No newline at end of file