Skip to content

Commit

Permalink
Housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
wub committed Jul 17, 2017
1 parent a368aca commit 03661fe
Show file tree
Hide file tree
Showing 12 changed files with 1,275 additions and 1,774 deletions.
15 changes: 12 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

[COMMIT_EDITMSG]
max_line_length = 0
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jarrod Mosen <[email protected]>
55 changes: 41 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
# preact-cli-plugin-typescript
# preact-cli-plugin-typescript &middot; [![Travis Status](https://img.shields.io/travis/wub/preact-cli-plugin-typescript/master.svg?label=travis&maxAge=43200)](https://travis-ci.org/wub/preact-cli-plugin-typescript) [![npm version](https://img.shields.io/npm/v/preact-cli-plugin-typescript.svg)](https://www.npmjs.com/package/preact-cli-plugin-typescript) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

Use TypeScript with [Preact CLI](https://github.com/developit/preact-cli).

This will install:

- [typescript](https://github.com/Microsoft/TypeScript)
- [awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader)

In the future, you'll be able to choose between `awesome-typescript-loader`
and `ts-loader`.

If you prefer Flow, check out [preact-cli-plugin-flow](https://github.com/SaraVieira/preact-cli-plugin-flow).

## Getting Started
## Installation

Install via npm or yarn:
Install via npm:

```shell
npm i preact-cli-plugin-typescript --save-dev
```
```shell
yarn add preact-cli-plugin-typescript --dev
```

This will install:

- [typescript](https://github.com/Microsoft/TypeScript)
- [awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader)
After installation, this plugin will create a `tsconfig.json` (TypeScript
configuration file), and `preact.config.js`, if they don't exist already.

After installation, this plugin will create a `tsconfig.json` (TypeScript configuration file).
## Usage

Create or edit `preact.config.js` to add the plugin:
In the root of your project, edit `preact.config.js` to add the plugin:

```javascript
```js
import preactCliTypeScript from 'preact-cli-plugin-typescript';

export default function (config) {
preactCliTypeScript(config);
}
```

TypeScript will now be compiled. Cool.
If you have an existing `tsconfig.json` file, be sure to use the correct
JSX factory:

```json
{
"compilerOptions": {
"jsx": "react",
"jsxFactory": "h"
}
}
```

Now you can simply add `.ts`/`.tsx` files to your project, and they'll
be compiled. Cool.

If you want to incrementally move to TypeScript, be sure to enable `allowJs`
in your `tsconfig.json`:

```json
{
"compilerOptions": {
"allowJs": true
}
}
```
43 changes: 33 additions & 10 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,47 @@
*/

/* global require process */
const exec = require('child_process').exec
const path = require('path')
const fs = require('fs')

const cwd = process.cwd().replace (/\\/g, '/')
const cwd = process.cwd().replace(/\\/g, '/')
const suffix = '/node_modules/preact-cli-plugin-typescript'
const root = cwd.endsWith(suffix) ? cwd.substr(0, cwd.length - suffix.length) : cwd
const root = cwd.endsWith(suffix)
? cwd.substr(0, cwd.length - suffix.length)
: cwd

// Install tsconfig
if (fs.access(path.join(root, 'tsconfig.json'), fs.constants.F_OK, () => {})) {
// TODO: If file exists, get existing config and merge correct values.
console.warn('Detected an existing tsconfig - make sure you have the correct settings.')
}
else {
console.log(
'tsconfig detected - make sure you have the correct settings.'
)
} else {
try {
fs.createReadStream(path.join('src', 'tsconfig.json'))
.pipe(fs.createWriteStream(path.join(root, 'tsconfig.json')));
fs
.createReadStream(path.join('src', 'tsconfig.json'))
.pipe(fs.createWriteStream(path.join(root, 'tsconfig.json')))

console.log('tsconfig.json installed.')
} catch (e) {
console.error('An error occurred while creating the tsconfig', e)
}
catch (e) {
console.error('An error occurred while creating the tsconfig', e);
}

// Install preact.config.js
if (fs.access(path.join(root, 'preact.config.js'), fs.constants.F_OK, () => {})) {
// TODO: If file exists, get existing config and merge correct values.
console.log(
'preact.config.js detected - you can now import the plugin.'
)
} else {
try {
fs
.createReadStream(path.join('src', 'preact.config.js'))
.pipe(fs.createWriteStream(path.join(root, 'preact.config.js')))

console.log('preact.config.js installed.')
} catch (e) {
console.error('An error occurred while creating the tsconfig', e)
}
}
Loading

0 comments on commit 03661fe

Please sign in to comment.