question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add TypeScript support

See original GitHub issue

Hi, 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:open
  • Created 4 years ago
  • Reactions:8
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
sindresorhuscommented, May 20, 2019

Yeah lib/parse-command.js would need to be modified to parse TS correctly.

Shouldn’t be difficult. Babel supports TypeScript.

Would be even more amazing if it parsed TS types, not just propTypes 😃

That’s a must! propTypes only exist because of lack of static types in JS.

1reaction
ChibiBlasphemcommented, Nov 15, 2019

Hello @vadimdemedes Could you publish a new version with the support of typescript ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set up TypeScript
You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do...
Read more >
How to Add TypeScript to a JavaScript Project - freeCodeCamp
How to convert a JavaScript application to support TypeScript. · Install typescript · Typescript config file · Create your first .TS file in...
Read more >
Add TypeScript support to an existing codebase | by Billel
Add TypeScript support to an existing codebase · Install typescript npm package and node type definitions. Just run npm i --save-dev typescript @types/node ......
Read more >
Adding TypeScript - Create React App
Global installs of create-react-app are no longer supported. To add TypeScript to an existing Create React App project, first install it:.
Read more >
Adding TypeScript-support to your Node.js project
If your application already has build tooling set up via webpack, the easiest way to add TypeScript support is by using the ts-loader...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found