Skip to content

Commit d605688

Browse files
committed
feat: add ability to pass parameters to the CLI
1 parent e5f7d49 commit d605688

File tree

4 files changed

+242
-162
lines changed

4 files changed

+242
-162
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
"@types/validate-npm-package-name": "^3.0.0",
7070
"@types/yargs": "^15.0.4",
7171
"commitlint": "^8.3.5",
72-
"eslint": "^6.8.0",
73-
"eslint-config-satya164": "^3.1.6",
72+
"eslint": "^7.0.0",
73+
"eslint-config-satya164": "^3.1.7",
7474
"husky": "^4.2.5",
7575
"prettier": "^2.0.5",
7676
"release-it": "^13.5.8",

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import yargs from 'yargs';
55
import inquirer from 'inquirer';
66
import { cosmiconfigSync } from 'cosmiconfig';
77
import isGitDirty from 'is-git-dirty';
8-
import create from './create';
8+
import create, { args as CreateArgs } from './create';
99
import * as logger from './utils/logger';
1010
import buildAAR from './targets/aar';
1111
import buildCommonJS from './targets/commonjs';
@@ -25,7 +25,7 @@ const FLOW_PRGAMA_REGEX = /\*?\s*@(flow)\b/m;
2525

2626
// eslint-disable-next-line babel/no-unused-expressions
2727
yargs
28-
.command('create <name>', 'create a react native library', {}, create)
28+
.command('create <name>', 'create a react native library', CreateArgs, create)
2929
.command('init', 'configure the package to use bob', {}, async () => {
3030
const pak = path.join(root, 'package.json');
3131

@@ -114,7 +114,7 @@ yargs
114114
: undefined;
115115

116116
const entries: { [key: string]: string } = {
117-
main: target
117+
'main': target
118118
? path.join(output, target, 'index.js')
119119
: path.join(source, entryFile),
120120
'react-native': path.join(source, entryFile),

src/create.ts

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,56 @@ const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
1818
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
1919
const OBJC_FILES = path.resolve(__dirname, '../templates/objc-library');
2020

21+
type ArgName =
22+
| 'slug'
23+
| 'description'
24+
| 'author-name'
25+
| 'author-email'
26+
| 'author-url'
27+
| 'repo-url'
28+
| 'type';
29+
30+
type Answers = {
31+
slug: string;
32+
description: string;
33+
authorName: string;
34+
authorEmail: string;
35+
authorUrl: string;
36+
repoUrl: string;
37+
type: 'native' | 'cpp' | 'expo';
38+
};
39+
40+
export const args: Record<ArgName, yargs.Options> = {
41+
'slug': {
42+
description: 'Name of the npm package',
43+
type: 'string',
44+
},
45+
'description': {
46+
description: 'Description of the npm package',
47+
type: 'string',
48+
},
49+
'author-name': {
50+
description: 'Name of the package author',
51+
type: 'string',
52+
},
53+
'author-email': {
54+
description: 'Email address of the package author',
55+
type: 'string',
56+
},
57+
'author-url': {
58+
description: 'URL for the package author',
59+
type: 'string',
60+
},
61+
'repo-url': {
62+
description: 'URL for the repository',
63+
type: 'string',
64+
},
65+
'type': {
66+
description: 'Type package do you want to develop',
67+
choices: ['native', 'cpp', 'expo'],
68+
},
69+
};
70+
2171
export default async function create(argv: yargs.Arguments<any>) {
2272
const folder = path.join(process.cwd(), argv.name);
2373

@@ -49,18 +99,9 @@ export default async function create(argv: yargs.Arguments<any>) {
4999

50100
const basename = path.basename(argv.name);
51101

52-
const {
53-
slug,
54-
description,
55-
authorName,
56-
authorEmail,
57-
authorUrl,
58-
githubUrl: repo,
59-
type,
60-
} = (await inquirer.prompt([
61-
{
102+
const questions: Record<ArgName, inquirer.Question> = {
103+
'slug': {
62104
type: 'input',
63-
name: 'slug',
64105
message: 'What is the name of the npm package?',
65106
default: validateNpmPackage(basename).validForNewPackages
66107
? /^(@|react-native)/.test(basename)
@@ -71,30 +112,26 @@ export default async function create(argv: yargs.Arguments<any>) {
71112
validateNpmPackage(input).validForNewPackages ||
72113
'Must be a valid npm package name',
73114
},
74-
{
115+
'description': {
75116
type: 'input',
76-
name: 'description',
77117
message: 'What is the description for the package?',
78118
validate: (input) => Boolean(input),
79119
},
80-
{
120+
'author-name': {
81121
type: 'input',
82-
name: 'authorName',
83122
message: 'What is the name of package author?',
84123
default: name,
85124
validate: (input) => Boolean(input),
86125
},
87-
{
126+
'author-email': {
88127
type: 'input',
89-
name: 'authorEmail',
90128
message: 'What is the email address for the package author?',
91129
default: email,
92130
validate: (input) =>
93131
/^\S+@\S+$/.test(input) || 'Must be a valid email address',
94132
},
95-
{
133+
'author-url': {
96134
type: 'input',
97-
name: 'authorUrl',
98135
message: 'What is the URL for the package author?',
99136
default: async (answers: any) => {
100137
try {
@@ -109,9 +146,8 @@ export default async function create(argv: yargs.Arguments<any>) {
109146
},
110147
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
111148
},
112-
{
149+
'repo-url': {
113150
type: 'input',
114-
name: 'githubUrl',
115151
message: 'What is the URL for the repository?',
116152
default: (answers: any) => {
117153
if (/^https?:\/\/github.com\/[^/]+/.test(answers.authorUrl)) {
@@ -124,10 +160,10 @@ export default async function create(argv: yargs.Arguments<any>) {
124160
},
125161
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
126162
},
127-
{
163+
'type': {
128164
type: 'list',
129-
name: 'type',
130165
message: 'What type of package do you want to develop?',
166+
// @ts-ignore - seems types are wrong for inquirer
131167
choices: [
132168
{ name: 'Native module in Kotlin and Objective-C', value: 'native' },
133169
{ name: 'Native module with C++ code', value: 'cpp' },
@@ -138,16 +174,34 @@ export default async function create(argv: yargs.Arguments<any>) {
138174
],
139175
default: 'native',
140176
},
141-
])) as {
142-
slug: string;
143-
description: string;
144-
authorName: string;
145-
authorEmail: string;
146-
authorUrl: string;
147-
githubUrl: string;
148-
type: 'native' | 'cpp' | 'expo';
149177
};
150178

179+
const {
180+
slug,
181+
description,
182+
authorName,
183+
authorEmail,
184+
authorUrl,
185+
repoUrl,
186+
type,
187+
} = {
188+
...argv,
189+
...(await inquirer.prompt(
190+
Object.entries(questions).map(([key, value]) => ({
191+
...value,
192+
name: key.replace(/\b-([a-z])/g, (_, char) => char.toUpperCase()),
193+
when: !(argv[key] && value.validate
194+
? value.validate(argv[key]) === true
195+
: Boolean(argv[key])),
196+
default:
197+
typeof value.default === 'function'
198+
? (answers: Partial<Answers>) =>
199+
value.default({ ...argv, ...answers })
200+
: value.default,
201+
}))
202+
)),
203+
} as Answers;
204+
151205
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
152206

153207
const options = {
@@ -172,7 +226,7 @@ export default async function create(argv: yargs.Arguments<any>) {
172226
email: authorEmail,
173227
url: authorUrl,
174228
},
175-
repo,
229+
repo: repoUrl,
176230
};
177231

178232
const copyDir = async (source: string, dest: string) => {

0 commit comments

Comments
 (0)