|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* eslint-disable quotes */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const program = require('commander'); |
| 8 | + |
| 9 | +const examples = |
| 10 | + ` |
| 11 | + Examples: |
| 12 | +
|
| 13 | + $ fun init |
| 14 | + $ fun init helloworld-nodejs8 |
| 15 | + $ fun init foo/bar |
| 16 | + $ fun init gh:foo/bar |
| 17 | + $ fun init gl:foo/bar |
| 18 | + $ fun init bb:foo/bar |
| 19 | + $ fun init github:foo/bar |
| 20 | + $ fun init gitlab:foo/bar |
| 21 | + $ fun init bitbucket:foo/bar |
| 22 | + $ fun init git+ssh://git@github.com/foo/bar.git |
| 23 | + $ fun init hg+ssh://hg@bitbucket.org/bar/foo |
| 24 | + $ fun init git@github.com:foo/bar.git |
| 25 | + $ fun init https://github.com/foo/bar.git |
| 26 | + $ fun init /path/foo/bar |
| 27 | + $ fun init -n fun-app -V foo=bar /path/foo/bar |
| 28 | + `; |
| 29 | + |
| 30 | +const parseVars = (val, vars) => { |
| 31 | + /* |
| 32 | + * Key-value pairs, separated by equal signs |
| 33 | + * keys can only contain letters, numbers, and underscores |
| 34 | + * values can be any character |
| 35 | + */ |
| 36 | + const group = val.match(/(^[a-zA-Z_][a-zA-Z\d_]*)=(.*)/); |
| 37 | + vars = vars || {}; |
| 38 | + if (group) { |
| 39 | + vars[group[1]] = group[2]; |
| 40 | + } |
| 41 | + return vars; |
| 42 | +}; |
| 43 | + |
| 44 | +program |
| 45 | + .name('fun init') |
| 46 | + .usage('[options] [location]') |
| 47 | + .description('Initializes a new fun project.') |
| 48 | + .option('-o, --output-dir [path]', 'where to output the initialized app into', '.') |
| 49 | + .option('-n, --name [name]', 'name of your project to be generated as a folder', 'fun-app') |
| 50 | + .option('--no-input [noInput]', 'disable prompting and accept default values defined template config') |
| 51 | + .option('-V, --var [vars]', 'template variable', parseVars) |
| 52 | + .on('--help', () => { |
| 53 | + console.log(examples); |
| 54 | + }) |
| 55 | + .parse(process.argv); |
| 56 | + |
| 57 | +const context = { |
| 58 | + name: program.name, |
| 59 | + outputDir: program.outputDir, |
| 60 | + input: program.input, |
| 61 | + vars: program.var || {} |
| 62 | +}; |
| 63 | + |
| 64 | +if (program.args.length > 0) { |
| 65 | + context.location = program.args[0]; |
| 66 | +} |
| 67 | + |
| 68 | +require('../lib/commands/init')(context).catch(require('../lib/exception-handler')); |
0 commit comments