# prettier-code-insert

> smart insert code and beautify it with prettier

Latest version **1.0.2** (published 2019-02-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install prettier-code-insert
pnpm add prettier-code-insert
yarn add prettier-code-insert
bun add prettier-code-insert
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-02-13 |
| First published | 2019-02-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jacky-fe |
| Maintainers | jacky-fe |
| Keywords | prettier, code, insert |

## Links

- npm: https://www.npmjs.com/package/prettier-code-insert
- Repository: https://github.com/Jacky-fe/prettier-code-insert
- Homepage: https://github.com/Jacky-fe/prettier-code-insert#readme
- Issues: https://github.com/Jacky-fe/prettier-code-insert/issues
- npm.io page: https://npm.io/package/prettier-code-insert

## Dependencies (1)

- [prettier](https://npm.io/package/prettier.md) ^1.10.3

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-02-13
- 1.0.1 — 2019-02-12
- 1.0.0 — 2019-02-12

## README

## 简介
这是一个简单的代码插入工具，你无需将要插入的代码转成AST，也无需通过traverse来查找代码的位置，而只需通过简单的注释即可。它会自动地插入代码并通过prettier美化代码，它可以帮助你降低开发脚手架工具的成本。

## 安装
``` shell
npm i prettier-code-insert -D
```
## 使用方法
例如我们要实现一个“create route”方法，那么我们需要:

1. import这个新生成的路由；
2. 在路由代码里加入这个引入的路由。

这是一个路由的例子：
### router.js
我们声明了里两个slot
1. import 这个slot是为了插入import代码；
2. route 这个slot是为了插入新建的路由。

``` js
import App from 'components/app';
import NotFound from './not-found';
/* @code-insert slot="import" */
export default function createRoutes() {
	const root = {
		path: '/',
		component: App,
		childRoutes: [
			NotFound,
			/* @code-insert slot="route" */
		],
	};
	return root;
}
```

### create-route.js
``` js
import fs from 'fs';
import path from 'path';
import insertCode from 'prettier-code-insert';
export default function create() {
  const filename = path.resolve(__dirname, './the/path/of/your/code');
  const code = fs.readFileSync(filename, { encoding: 'utf-8' });
  return new Promise((resolve, reject) => {
    insertCode(code, [
      {
        code: `import RouteA from "./route-a";`,
        slotName: 'import',
      },
      {
        code: 'RouteA,',
        slotName: 'route',
      },
    ]).then(newCode => {
      fs.writeFileSync(filename, newCode);
      resolve(true);
    });
  });
}
```

执行后的代码
``` js
import App from 'components/app';
import NotFound from './not-found';
/* @code-insert slot="import" */
import RouteA from './route-a'; // 这是新插入的路由
export default function createRoutes() {
	const root = {
		path: '/',
		component: App,
		childRoutes: [
			NotFound,
			/* @code-insert slot="route" */
			RouteA, // 这是新插入的路由
		],
	};
	return root;
}
```

## API
<a name="insertCode"></a>

## insertCode(sourceCode, codeSnaps) ⇒
将代码插入到指定的slot中

**Kind**: global function  
**Returns**: 转换后的代码 Promise&lt;string&gt;

| Param | Type | Description |
| --- | --- | --- |
| sourceCode | <code>string</code> | 被插入代码的源码 |
| codeSnaps | <code>Array&lt;CodeSnap&gt;</code> | 代码片段，格式： { code: string, slotName: string }。会将sourcecode里对应slot的地方插入代码 |

---
_Source: https://npm.io/package/prettier-code-insert · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
