Skip to content

Commit

Permalink
feat: copy .d.ts. to dist (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored Feb 10, 2021
1 parent 9f67cb9 commit 4002b50
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
10 changes: 10 additions & 0 deletions cmds/ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ module.exports = {
type: 'array',
describe: 'Values are merged into the local TS config include property.',
default: userConfig.ts.include
},
copyFrom: {
type: 'string',
describe: 'Copy .d.ts files from',
default: userConfig.ts.copyFrom
},
copyTo: {
type: 'string',
describe: 'Copy .d.ts files to',
default: userConfig.ts.copyTo
}
})
},
Expand Down
8 changes: 4 additions & 4 deletions md/ts-jsdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- [Rules for optimal type declarations and documentation](#rules-for-optimal-type-declarations-and-documentation)
- [1. Commonjs default exports](#1-commonjs-default-exports)
- [2. Commons js named exports](#2-commons-js-named-exports)
- [3. Use a `types.ts` file](#3-use-a-typests-file)
- [3. Use a `types.d.ts` file](#3-use-a-typesdts-file)
- [4. JSDoc comments bad parsing](#4-jsdoc-comments-bad-parsing)
- [5. Always put your `@typedef` at the top of file](#5-always-put-your-typedef-at-the-top-of-file)
- [Must read references](#must-read-references)
Expand Down Expand Up @@ -172,11 +172,11 @@ exports.hash = hash() {}
exports.IPFS = IPFS
```

### 3. Use a `types.ts` file
When writing types JSDoc can sometimes be cumbersome, impossible, it can output weird type declarations or even broken documentation. Most of these problems can be solved by defining some complex types in typescript in a `types.ts` file.
### 3. Use a `types.d.ts` file
When writing types JSDoc can sometimes be cumbersome, impossible, it can output weird type declarations or even broken documentation. Most of these problems can be solved by defining some complex types in typescript in a `types.d.ts` file.

```ts
// types.ts
// types.d.ts
export type IntersectionType = Type1 & Type2
```
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"chalk": "^4.1.0",
"conventional-changelog": "^3.1.24",
"conventional-github-releaser": "^3.1.5",
"copyfiles": "^2.4.1",
"cors": "^2.8.5",
"dirty-chai": "^2.0.1",
"electron-mocha": "^10.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ module.exports = async (argv) => {
await tsCmd({
...argv,
preset: 'types',
include: userConfig.ts.include
include: userConfig.ts.include,
copyTo: userConfig.ts.copyTo,
copyFrom: userConfig.ts.copyFrom
})
}
}
4 changes: 3 additions & 1 deletion src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ const config = (searchFrom) => {
},
ts: {
preset: undefined,
include: []
include: [],
copyFrom: 'src/**/*.d.ts',
copyTo: 'dist'
}
},
userConfig,
Expand Down
16 changes: 15 additions & 1 deletion src/ts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const hasConfig = hasFile('tsconfig.json')
* @property {string[]} forwardOptions - Extra options to forward to the backend
* @property {string[]} extraInclude - Extra include files for the TS Config
* @property {boolean} tsRepo - Typescript repo support.
* @property {string} copyFrom - copy .d.ts from
* @property {string} copyTo - copy .d.ts to
*/

/**
Expand All @@ -32,7 +34,9 @@ module.exports = async (argv) => {
forwardOptions: argv['--'] ? argv['--'] : [],
extraInclude: (argv.include && argv.include.length > 0) ? await globby(argv.include) : [],
preset: argv.preset,
tsRepo: argv.tsRepo
tsRepo: argv.tsRepo,
copyFrom: argv.copyFrom,
copyTo: argv.copyTo
}

if (argv.preset === 'config') {
Expand Down Expand Up @@ -144,6 +148,16 @@ const types = async (userTSConfig, opts) => {
if (fs.existsSync(typesPath)) {
fs.copySync(typesPath, fromRoot('dist', path.basename(typesPath)))
}

await execa('copyfiles', [
opts.copyFrom,
opts.copyTo
],
{
localDir: path.join(__dirname, '../..'),
preferLocal: true,
stdio: 'inherit'
})
} finally {
fs.removeSync(configPath)
fs.removeSync(fromRoot('dist', 'tsconfig-types.aegir.tsbuildinfo'))
Expand Down
11 changes: 8 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
*
*/
interface Options extends GlobalOptions {

/**
* Options for the `build` command
*/
build: BuildOptions

/**
* Options for the `ts` command
*/
ts: TSOptions

/**
* Options for the `docs` command
*/
Expand Down Expand Up @@ -79,6 +76,14 @@ interface TSOptions {
* Values are merged into the local TS config include property.
*/
include: string[]
/**
* Copy .d.ts files from
*/
copyFrom: string
/**
* Copy .d.ts files to
*/
copyTo: string
}

interface DocsOptions {
Expand Down

0 comments on commit 4002b50

Please sign in to comment.