-
Notifications
You must be signed in to change notification settings - Fork 59
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ node_modules | |
/docs | ||
.nyc_output | ||
*.log | ||
.vscode | ||
.vscode | ||
*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not we be passing a |
||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. havingnoEmit
compiler option will preventemitDeclarationOnly
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.