Skip to content

Commit 46a3126

Browse files
committed
export cli functionality via API
1 parent 9ac59f9 commit 46a3126

File tree

3 files changed

+78
-49
lines changed

3 files changed

+78
-49
lines changed

src/cli.ts

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
11
#!/usr/bin/env node
22

3-
import * as path from 'path';
4-
import * as chokidar from 'chokidar';
5-
import _glob from 'glob';
63
import * as yargs from 'yargs';
7-
import chalk from 'chalk';
8-
import {DtsCreator} from './dts-creator';
9-
import {DtsContent} from "./dts-content";
10-
import * as util from "util";
4+
import {run} from "./run";
115

12-
const glob = util.promisify(_glob);
136

14-
const yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] <input directory>')
7+
const yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] <search directory>')
158
.example('$0 src/styles', '')
169
.example('$0 src -o dist', '')
1710
.example('$0 -p styles/**/*.icss -w', '')
1811
.detectLocale(false)
1912
.demand(['_'])
20-
.alias('c', 'camelCase').describe('c', 'Convert CSS class tokens to camelcase')
21-
.alias('o', 'outDir').describe('o', 'Output directory')
2213
.alias('p', 'pattern').describe('p', 'Glob pattern with css files')
14+
.alias('o', 'outDir').describe('o', 'Output directory')
2315
.alias('w', 'watch').describe('w', 'Watch input directory\'s css files or pattern').boolean('w')
16+
.alias('c', 'camelCase').describe('c', 'Convert CSS class tokens to camelcase')
2417
.alias('d', 'dropExtension').describe('d', 'Drop the input files extension').boolean('d')
2518
.alias('s', 'silent').describe('s', 'Silent output. Do not show "files written" messages').boolean('s')
2619
.alias('h', 'help').help('h')
2720
.version(() => require('../package.json').version);
28-
const argv = yarg.argv;
29-
let creator: DtsCreator;
3021

31-
async function writeFile(f: string): Promise<void> {
32-
try {
33-
const content: DtsContent = await creator.create(f, undefined, !!argv.w);
34-
await content.writeFile();
3522

36-
if (!argv.s) {
37-
console.log('Wrote ' + chalk.green(content.outputFilePath));
38-
}
39-
}
40-
catch (error) {
41-
console.error(chalk.red('[Error] ' + error));
42-
}
43-
};
23+
main();
24+
25+
async function main(): Promise<void> {
26+
const argv = yarg.argv;
4427

45-
async function main() {
46-
let rootDir: string;
47-
let searchDir: string;
4828
if(argv.h) {
4929
yarg.showHelp();
5030
return;
5131
}
5232

33+
let searchDir: string;
5334
if(argv._ && argv._[0]) {
5435
searchDir = argv._[0];
5536
}else if(argv.p) {
@@ -58,26 +39,13 @@ async function main() {
5839
yarg.showHelp();
5940
return;
6041
}
61-
const filesPattern = path.join(searchDir, argv.p || '**/*.css');
62-
rootDir = process.cwd();
63-
creator = new DtsCreator({
64-
rootDir,
65-
searchDir,
42+
43+
await run(searchDir, {
44+
pattern: argv.p,
6645
outDir: argv.o,
46+
watch: argv.w,
6747
camelCase: argv.c,
68-
dropExtension: argv.d
48+
dropExtension: argv.d,
49+
silent: argv.s
6950
});
70-
71-
if(!argv.w) {
72-
const files = await glob(filesPattern);
73-
files.forEach(writeFile);
74-
} else {
75-
console.log('Watch ' + filesPattern + '...');
76-
77-
const watcher = chokidar.watch([filesPattern.replace(/\\/g, "/")]);
78-
watcher.on('add', writeFile);
79-
watcher.on('change', writeFile);
80-
}
8151
};
82-
83-
main();

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { DtsCreator } from './dts-creator';
2-
export = DtsCreator;
1+
export {DtsCreator as default} from './dts-creator';
2+
export {run} from './run';

src/run.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as path from "path";
2+
import * as util from "util";
3+
import chalk from "chalk";
4+
import * as chokidar from "chokidar";
5+
import _glob from 'glob';
6+
import {DtsCreator} from "./dts-creator";
7+
import {DtsContent} from "./dts-content";
8+
9+
const glob = util.promisify(_glob);
10+
11+
12+
interface RunOptions {
13+
pattern?: string;
14+
outDir?: string;
15+
watch?: boolean;
16+
camelCase?: boolean;
17+
dropExtension?: boolean;
18+
silent?: boolean;
19+
}
20+
21+
export async function run(searchDir: string, options: RunOptions = {}): Promise<void> {
22+
const filesPattern = path.join(searchDir, options.pattern || '**/*.css');
23+
24+
const creator = new DtsCreator({
25+
rootDir: process.cwd(),
26+
searchDir,
27+
outDir: options.outDir,
28+
camelCase: options.camelCase,
29+
dropExtension: options.dropExtension,
30+
});
31+
32+
const writeFile = async (f: string): Promise<void> => {
33+
try {
34+
const content: DtsContent = await creator.create(f, undefined, !!options.watch);
35+
await content.writeFile();
36+
37+
if (!options.silent) {
38+
console.log('Wrote ' + chalk.green(content.outputFilePath));
39+
}
40+
}
41+
catch (error) {
42+
console.error(chalk.red('[Error] ' + error));
43+
}
44+
};
45+
46+
if(!options.watch) {
47+
const files = await glob(filesPattern);
48+
await Promise.all(files.map(writeFile));
49+
} else {
50+
console.log('Watch ' + filesPattern + '...');
51+
52+
const watcher = chokidar.watch([filesPattern.replace(/\\/g, "/")]);
53+
watcher.on('add', writeFile);
54+
watcher.on('change', writeFile);
55+
await waitForever();
56+
}
57+
}
58+
59+
async function waitForever(): Promise<void> {
60+
return new Promise<void>(() => {});
61+
}

0 commit comments

Comments
 (0)