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

Feat/npx sdef to dfs #8

Merged
merged 6 commits into from
Sep 6, 2018
Merged

Conversation

havardh
Copy link
Contributor

@havardh havardh commented Sep 6, 2018

It turns out that the sdef command can be used to generate .d.ts files from App bundles. I've made the input parameter to the command the path to the app bundle to allow bundles outside of the /Applications folder to be converted too.

Fixes #7

Adds a converter as the default bin which will convert a '.app'
application container into a '.d.ts' file.
Copy link
Member

@azu azu left a comment

Choose a reason for hiding this comment

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

Thanks for PR.
I've write some comments.

console.log();
}

if (process.argv.length !== 3 && process.argv.length !== 4) {
Copy link
Member

Choose a reason for hiding this comment

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

I like to use command parser library like meow prefer than hand written.

meow includes following feature

  • Show usage(--help by autoHelp option)
  • Correct exit status
  • Parse flags like --out [outdir]

Copy link
Member

Choose a reason for hiding this comment

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

Usage: npx @jxa/sdef-to-dts <input> --output <output>

In meow

const cli = meow(`
	Usage
	  $ npx @jxa/sdef-to-dts <input> --output <output>
          
          <input> - path to an Application.app to read
	Options
	  --output, -o  path to an Application.d.ts or a directory to write to

	Examples
	  $ npx @jxa/sdef-to-dts /Applications/Safari.app --output ./safari.d.ts
`, {
	flags: {
		output: {
			type: 'string',
			alias: 'o'
		}
	},
        autoVersion: true, // <= --version
        autoHelp: true // <= --help 
});

// ... 
  • cli.input[0] is inFile
  • cli.flags.output is [outDir or outPath]


function readSdef(path) {
try {
return child_process.execSync("sdef " + path, {stdio: ["ignore"]}).toString();
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

Probably, we should use execa instead of child_process directly.
Because, child_process.execSync's default buffer is 200kb.

fs.writeFileSync(outPath, content, "utf-8");
}).catch(err => {
console.error(err.toString());
usage();
Copy link
Member

Choose a reason for hiding this comment

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

call cli.showHelp()(meow) instead of it.

@azu azu merged commit 50f34b2 into JXA-userland:master Sep 6, 2018
@azu
Copy link
Member

azu commented Sep 6, 2018

Great!

I've published https://github.com/JXA-userland/JXA/releases/tag/v1.3.0

@JMichaelTX
Copy link

Can someone please explain how to use this, and what the benefits are?
I'm trying to use this extension with Visual Studio Code, and I'm having trouble.
I know very little about bash scripting, so please provide detailed instructions.
Thanks.

@havardh
Copy link
Contributor Author

havardh commented Sep 6, 2018

@JMichaelTX: This tool will generate TypeScript definitions for any application bundle which sdef can derive a .sdef file for. My own intention is to use it to generate types for applications which are not found in the types package. You can follow #5 to see discussion that lead to this feature.

Building from the example in this repo, these are the instructions to use this command.

  1. move into the example folder
    cd example
  2. install the @jxa/types package
    yarn add @jxa/types
  3. Make a directory to store types
    mkdir types
  4. Generate types for Google Chrome (assuming it is installed at /Applications/Google Chrome.app
    npx @jxa/sdef-to-dts /Applications/Google\ Chrome.app --output types
  5. Add the type imports to src/example.ts
    import { GoogleChrome } from "../types/GoogleChrome.t.ds
    import { Application } from "@jxa/types";
  6. Add the following code snippet to the src/example.ts file
run(() => {
    Application<GoogleChrome>("GoogleChrome").
});

Visual Studio Code should provide autocomplete suggestions for the Google Chrome application after the ..

Running @jxa/sdef-to-dts requires that you have xcode or Command Line Tools set up so that sdef runs correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants