Add TypeScript support
See original GitHub issueHi, First, thanks for a great tool! This together with Ink has made writing cli-tools a joy!
But I was wondering if you would be interested in a PR to enable Typescript-files as entrypoints for commands.
I’ve noticed that parcel is used to bundle the scripts and parcel has built in support for TS. And today I can use TS-files if they’re imported deeper in the tree.
I did some fiddling and found a way to at least get started.
I just changed a few lines in lib/read-commands.js
:
const readCommands = async (dirPath, buildDirPath) => {
const paths = fs.readdirSync(dirPath);
const commands = [];
const promises = paths.map(async path => {
// Since `readdir` returns relative paths, we need to transform them to absolute paths
const fullPath = join(dirPath, path);
const stats = await stat(fullPath);
if (stats.isDirectory()) {
const subCommands = await readCommands(fullPath, join(buildDirPath, path));
const indexCommand = subCommands.find(isIndexCommand);
commands.push({
...indexCommand,
name: path,
subCommands: subCommands.filter(command => !isIndexCommand(command))
});
}
if (stats.isFile()) {
const {description, args} = await parseCommand(fullPath);
commands.push({
path: fullPath,
// -------CHANGES BELOW-------
buildPath: join(buildDirPath, path.replace('.tsx', '.js')), // This should probably be a more safe regular expression based replace
name: basename(basename(fullPath, '.js'), '.tsx'),
// -------CHANGES ABOVE-------
description,
args,
subCommands: []
});
}
});
await Promise.all(promises);
return commands;
};
This change deals with the first problem. Now at least both .js- and .tsx-files gets parsed.
But this solutions gives another problem, of course 🤔. If the entrypoint includes interfaces or other typescript features lib/parse-command.js
wont be able to parse the file. I would need to dive into the world of Babel in order to see if it’s enough to add @babel/plugin-transform-typescript
to make it work, or if a separate typescript based parser is needed.
What do you think? Tell me if you would like me to try it out and see if I could get something working. If you’re not interested, thats fine since I can use Typescript deeper in the tree!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:8 (1 by maintainers)
Shouldn’t be difficult. Babel supports TypeScript.
That’s a must!
propTypes
only exist because of lack of static types in JS.Hello @vadimdemedes Could you publish a new version with the support of typescript ?