Skip to content

Commit 995c105

Browse files
committed
Add relative directory argument
1 parent 96616fe commit 995c105

5 files changed

Lines changed: 53 additions & 18 deletions

File tree

src/bin.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
#!/usr/bin/env node
22

3-
import { argv } from 'yargs';
3+
import yargs from 'yargs';
44
import { Logger } from './core/Logger';
5-
import { runCLI } from './index';
6-
7-
runCLI(argv._[0] === 'init')
8-
.then(() => process.exit(0))
9-
.catch(err => {
10-
Logger.error(err);
11-
process.exit(1);
12-
});
5+
import { IArgs, runGenerator } from './index';
6+
import { initConfigFile } from './init';
7+
8+
yargs
9+
.command(
10+
'init',
11+
'Run config initialization wizard',
12+
() => {},
13+
() => {
14+
Logger.log('Starting config initialization wizard');
15+
16+
initConfigFile()
17+
.then(() => process.exit(0))
18+
.catch(err => {
19+
Logger.error(err);
20+
process.exit(1);
21+
});
22+
}
23+
)
24+
.command({
25+
command: '*',
26+
handler: (args: IArgs) => {
27+
const { directory } = args;
28+
29+
runGenerator({ directory })
30+
.then(() => process.exit(0))
31+
.catch(err => {
32+
Logger.error(err);
33+
process.exit(1);
34+
});
35+
},
36+
})
37+
.option('directory', {
38+
alias: 'd',
39+
type: 'string',
40+
description: 'Generate files in custom directory relative to root path',
41+
})
42+
.parse();

src/constants/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22

33
import { getAppRoot, getConfig, getStylesExtension } from '../utils';
4-
import { StyleFormats } from '../types/types';
4+
import { StyleFormats } from '../types';
55

66
export const STYLE_FORMATS: StyleFormats[] = ['CSS', 'SCSS', 'SASS', 'Less', 'Stylus'];
77
export const APP_ROOT = getAppRoot();

src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import inquirer from 'inquirer';
22

3-
import { init } from './init';
3+
import path from 'path';
44
import { Logger } from './core/Logger';
55
import * as questionTypes from './questions/questionTypes';
66
import { type as typeQuestion } from './questions/questions';
77
import { FileGenerateManager } from './core/FileGenerateManager';
8+
import { CONFIG } from './constants';
89

9-
export async function runCLI(shouldInit: boolean) {
10-
if (shouldInit) {
11-
await init();
12-
return;
13-
}
10+
export interface IArgs {
11+
directory?: string;
12+
}
1413

14+
export async function runGenerator(args: IArgs) {
15+
const { directory } = args;
1516
const { type } = await inquirer.prompt(typeQuestion as any);
1617

1718
process.stdout.write('\n');
1819

20+
if (directory) {
21+
CONFIG.path = path.resolve(process.cwd(), directory);
22+
}
23+
1924
switch (type) {
2025
case 'component': {
2126
const answers = await inquirer.prompt(questionTypes.getComponentQuestions());

src/init/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { question } from './questions';
88
import { APP_ROOT } from '../constants';
99
import { updatePackage } from './helpers';
1010

11-
export async function init() {
11+
export async function initConfigFile() {
1212
clear(true);
1313
Logger.log(
1414
chalk => `

src/utils/getConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { cosmiconfigSync } from 'cosmiconfig';
44
import defaultConfig from '../config/default.json';
55
import { APP_ROOT, STYLE_FORMATS } from '../constants';
66
import { Logger } from '../core/Logger';
7-
import { IConfig } from '../types/types';
7+
import { IConfig } from '../types';
88

99
export function getConfig() {
1010
let config = defaultConfig as IConfig;

0 commit comments

Comments
 (0)