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: generate ts types #637

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules
/docs
.nyc_output
*.log
.vscode
.vscode
*.d.ts
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ You can run your tests with `nyc` using

```bash
$ npx nyc -s aegir test -t node
# to check the report locally
# to check the report locally
$ npx nyc report --reporter=html && open coverage/index.html
# or just for a text based reporter
$ npx nyc report
Expand Down Expand Up @@ -236,6 +236,34 @@ Pass the `--analyze` option to have Webpack generate a `stats.json` file for the
aegir build --analyze
```

### Generating types

This will generate `.d.ts` files from files in your project. Suitable for use in a `prepublishOnly` [npm script](https://docs.npmjs.com/misc/scripts).

```console
$ aegir generate-types PATTERN
```

It supports a glob pattern as the argument, but to prevent cli expansion you should wrap it in quotes:

```console
$ aegir generate-types 'src/**/*.js'
```

Pass the `--overwrite` option to remove `.d.ts` files that would be generated:

```console
$ aegir generate-types --overwrite 'src/**/*.js'
```

Any flags or config to pass to `tsc` can be specified as forward options:

```console
$ aegir generate-types --overwrite 'src/**/*.js' -- --allowJs --esModuleInterop
Copy link
Contributor

Choose a reason for hiding this comment

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

Please note that tsconfig.json affects a lot what options can be passed or how they collide. E.g. having noEmit compiler option will prevent emitDeclarationOnly passed here.

I don't think documenting every single collision is realistic, but maybe a note or a link to an example setup might be helpful.

```

Since these are generated files you may wish to add `*.d.ts` to your `.gitignore` file if you are generating types from JSDocs as part of a js project release.

### Releasing

1. Run linting
Expand Down Expand Up @@ -282,7 +310,7 @@ If you want no documentation generation you can pass `--no-docs` to the release

#### Scoped Github Token

Performing a release involves creating new commits and tags and then pushing them back to the repository you are releasing from. In order to do this you should create a [GitHub personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) and store it in the environmental variable `AEGIR_GHTOKEN`.
Performing a release involves creating new commits and tags and then pushing them back to the repository you are releasing from. In order to do this you should create a [GitHub personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) and store it in the environmental variable `AEGIR_GHTOKEN`.

The only access scope it needs is `public_repo`.

Expand Down
17 changes: 17 additions & 0 deletions cmds/generate-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

module.exports = {
command: 'generate-types [input...]',
desc: 'Generate .d.ts files for the project',
builder: {
overwrite: {
type: 'boolean',
default: false,
describe: 'Whether to remove all .d.ts files in the project before running',
}
},
handler (argv) {
const build = require('../src/generate-types')
return build(argv)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"git-validate": "^2.2.4",
"globby": "^11.0.1",
"ipfs-utils": "^2.3.0",
"it-all": "^1.0.2",
"it-glob": "~0.0.8",
"json-loader": "~0.5.7",
"karma": "^5.1.0",
Expand Down
55 changes: 55 additions & 0 deletions src/generate-types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const {
exec
} = require('../utils')
const glob = require('it-glob')
const all = require('it-all')
const promisify = require('util').promisify
const rimraf = promisify(require('rimraf'))

async function generateTypes (argv) {
const files = (await Promise.all(
argv.input.map(pattern => all(glob(process.cwd(), pattern, {
absolute: true,
nodir: true
})))
)).reduce((acc, curr) => acc.concat(curr), [])

if (!files.length) {
throw new Error(`Invalid input glob pattern ${argv.input.join()}`)
}

if (argv.overwrite) {
// remove any .td.ts definitions that already exist for the input files
for (const path of files) {
if (!path.endsWith('.js')) {
continue
}

// foo.js -> foo.d.ts
const tsDef = path.substring(0, path.length - 3) + '.d.ts'

// will not error if the file does not exist
await rimraf(tsDef)
}
}

const forwardOptions = argv['--'] ? argv['--'] : []
const args = ['-d', '--emitDeclarationOnly'].concat(forwardOptions).concat(files)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should not we be passing a --declarationDir as well or is it expected to be passed as part of -- ?


const cmd = exec(require.resolve('typescript/bin/tsc'), args, {
cwd: process.cwd()
})

cmd.stdout.on('data', data => {
console.info(data.toString()) // eslint-disable-line no-console
})
cmd.stderr.on('data', data => {
console.error(data.toString()) // eslint-disable-line no-console
})

await cmd
}

module.exports = generateTypes
17 changes: 17 additions & 0 deletions test/generate-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-env mocha */
'use strict'

const generateTypes = require('../src/generate-types')

describe('generate-types', () => {
it('generate types for itself (aegir)', function () {
this.timeout(60 * 1000) // slow ci is slow
return generateTypes({
input: ['src/**/*.js'],
overwrite: true,
'--': [
'--allowJs'
]
})
})
})
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ require('./dependency-check')
require('./utils/echo-server')
require('./utils/get-port')
require('./config/user')
require('./generate-types')