Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pipe from cli #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,62 @@ function getArgs() {
'$0 -c AwesomeComponent awesome.htm',
'Creates React component "AwesomeComponent" based on awesome.htm'
)
.example(
'cat file.htm | $0 -c AwesomeComponent',
'Creates React component "AwesomeComponent" based on data piped in'
)
.strict();

var files = args.argv._;
if (!files || files.length === 0) {
console.error('Please provide a file name');
args.showHelp();
process.exit(1);

// print error if called directly (not piped) and no files are specified
if (process.stdin.isTTY && (!files || files.length === 0)) {
console.error('Please provide a file name');
args.showHelp();
process.exit(1);
}
return args.argv;
}

function main() {
var argv = getArgs();
fs.readFile(argv._[0], 'utf-8', function(err, input) {
function buildConverterCb(createClass, outputClassName) {
var converter = new HTMLtoJSX({
createClass: createClass,
outputClassName: outputClassName
});

return function(err, input) {
if (err) {
console.error(err.stack);
process.exit(2);
}
var converter = new HTMLtoJSX({
createClass: !!argv.className,
outputClassName: argv.className
});

var output = converter.convert(input);
console.log(output);
process.exit();
}
}

function readFromPipe(cb) {
var data = '';
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function(chunk) {
data += chunk;
});
process.stdin.on('end', function() {
return cb(null, data);
});
}


function main() {
var argv = getArgs();
var convertInputCb = buildConverterCb(!!argv.className, argv.className);

if (process.stdin.isTTY) {
return fs.readFile(argv._[0], 'utf-8', convertInputCb);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is argv._[0]?

}
return readFromPipe(convertInputCb);
}

main();