Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 2c4b42a

Browse files
committed
feat: Implemented pipes
Enabled stdin/stdout redirection to use react2dts as unix filter
1 parent c12cd70 commit 2c4b42a

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

Diff for: index.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
#!/usr/bin/env node
12
import * as path from 'path';
23
import * as fs from 'fs';
34
import * as babylon from 'babylon';
5+
const minimist = require('minimist');
46

5-
export function generate(name: string, path: string): string {
6-
const ast = babylon.parse(fs.readFileSync(path).toString(), {
7+
export function generateFromFile(name: string, path: string): string {
8+
return generate(name, fs.readFileSync(path).toString());
9+
}
10+
11+
export function generate(name: string, code: string): string {
12+
const ast = babylon.parse(code, {
713
sourceType: 'module',
814
plugins: [
915
'jsx',
@@ -203,3 +209,20 @@ class Writer {
203209
}
204210

205211
}
212+
213+
const options = minimist(process.argv.slice(2), {
214+
string: 'name'
215+
});
216+
217+
const stdinCode: string[] = [];
218+
process.stdin.resume();
219+
process.stdin.on('data', (data: Buffer) => {
220+
stdinCode.push(data.toString());
221+
});
222+
process.stdin.on('end', () => {
223+
if (!options.name) {
224+
console.error('Failed to specify --name parameter');
225+
process.exit(1);
226+
}
227+
process.stdout.write(generate(options.name, stdinCode.join('')));
228+
});

Diff for: package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "0.2.1",
44
"description": "Create typescript definitions files (d.ts) from react components",
55
"main": "index.js",
6+
"bin": {
7+
"react2dts": "index.js"
8+
},
69
"scripts": {
710
"build": "tsc",
811
"test": "npm run build && node -r babel-register ./node_modules/.bin/tape 'tests/**/*-test.js' |tap-spec",
@@ -30,7 +33,8 @@
3033
"typescript": "1.7.3"
3134
},
3235
"dependencies": {
33-
"babylon": "6.3.15"
36+
"babylon": "6.3.15",
37+
"minimist": "1.2.0"
3438
},
3539
"config": {
3640
"commitizen": {

Diff for: tests/component-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as react2dts from '../index';
66

77
test('create definition from simple component', function(t) {
88
t.equals(
9-
react2dts.generate('simple-component', path.join(__dirname, 'simple-component.jsx')),
9+
react2dts.generateFromFile('simple-component', path.join(__dirname, 'simple-component.jsx')),
1010
fs.readFileSync(path.join(__dirname, 'simple-component.d.ts')).toString());
1111
t.end();
1212
});

Diff for: typings/minimist.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace Minimist {
2+
export function minimist(args: string[], options: any): any;
3+
}
4+
declare module 'minimist' {
5+
export = Minimist.minimist;
6+
}

Diff for: typings/tsd.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/// <reference path="node/node.d.ts" />
22
/// <reference path="babylon.d.ts" />
33
/// <reference path="react/react.d.ts" />
4+
/// <reference path="minimist.d.ts" />

0 commit comments

Comments
 (0)