# @creuna/codegen

> This package has utilities for generating javascript files.

Latest version **0.3.0** (published 2020-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @creuna/codegen
pnpm add @creuna/codegen
yarn add @creuna/codegen
bun add @creuna/codegen
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2020-02-17 |
| First published | 2018-12-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Asbjørn Hegdahl |
| Maintainers | atlekri, dalfalk, mariuswatz, mw.olsen, setalid, simon-garden-beach |

## Links

- npm: https://www.npmjs.com/package/@creuna/codegen
- Repository: https://github.com/Creuna-Oslo/codegen
- Homepage: https://github.com/Creuna-Oslo/codegen#readme
- Issues: https://github.com/Creuna-Oslo/codegen/issues
- npm.io page: https://npm.io/package/@creuna/codegen

## Dependencies (5)

- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.12.1
- [prettier](https://npm.io/package/prettier.md) ^1.16.3
- [klaw-sync](https://npm.io/package/klaw-sync.md) ^6.0.0
- [@creuna/utils](https://npm.io/package/@creuna/utils.md) ^1.1.0

## Recent versions

- 0.3.0 (latest) — 2020-02-17
- 0.2.3 — 2019-02-23
- 0.2.2 — 2019-01-31
- 0.2.1 — 2019-01-03
- 0.2.0 — 2018-12-20
- 0.1.0 — 2018-12-19

## README

# codegen

This package has utilities for generating javascript files.

## API

- **createComponentsFile(_options_)**
- **createPagesFile(_options_)**
- **createPathsFile(_options_)**

These functions all take the same options object.

The functions read a provided directory and look for react components. All found components that have an `index.js` file and use the same name for the `.jsx` file and the containing folder name will be included in the generated files. The content of the generated files depends on what function is used.

### Example file tree

```
/
  components
    component
      component.jsx
      index.js
    other-component
      blabla.jsx
      index.js
    another-component
      another-component.jsx
```

In this example, only `component` will be included. `blabla.jsx` does not match `other-component`, and `another-component` lacks an `index.js` file.

### Options

- **fileHeader**: `String` = `"// NOTE: Do not edit this file. It is automatically generated."`
  (String prepended to the generated file)

- **fileName**: `String`
  (Name of the file to write)

- **outputPath**: `String`
  (Full path to the folder in which to write the file)

- **prettierOptions**: `Object`
  (Prettier options object, used to format the output file)

- **searchPath**: `String`
  (The path in which to look for components)

- **fileExtension**: `String`
  (The file extension you are working with. It will change the import style of the components. Written in the form .[fileExtension]. It defaults to '.jsx')
  (For example: '.tsx'.)

### createComponentsFile

```js
const { createComponentsFile } = require("@creuna/codegen");
const path = require("path");

createComponentsFile({
  searchPath: path.join(__dirname, "components"),
  fileName: "app.components.js",
  outputPath: __dirname,
  fileExtension: '.tsx'
});
```

**app.components.js**

```js
// NOTE: Do not edit this file. It is automatically generated.
import Component from "./components/component";

export { Component };
```

### createPagesFile

This function is inteded for use with static site generation. All valid components will be imported, parsed for metadata and exported from the resulting javascript file. The function supports passing metadata in comments using `yaml` syntax.

**Example page component**

```jsx
/*
group: Eksempelgruppe
name: Eksempelside
path: /eksempelside
*/
import React from "react";
const ExamplePage = () => <div />;
export default ExamplePage;
```

**createPagesFile**

```js
const { createPagesFile } = require("@creuna/codegen");
const path = require("path");

createPagesFile({
  fileName: "pages.js",
  searchPath: path.join(__dirname, "pages"),
  outputPath: __dirname,
  fileExtension: '.jsx'
});
```

**pages.js**

```js
// NOTE: Do not edit this file. It is automatically generated.
import ExamplePage from "./example-page";

export default [
  {
    component: ExamplePage,
    group: "Eksempelgruppe",
    name: "Eksempelside",
    path: "/eksempelside"
  }
];
```

### createPathsFile

This function is inteded for use with `react-router`. The generated file exports an array of paths (using commonJs syntax) that can be fed to `react-router`. `yaml` comments are respected.

**Example page component**

```jsx
/*
path: /some-path
*/
import React from "react";
const ExamplePage = () => <div />;
export default ExamplePage;
```

**createPathsFile**

```js
const { createPathsFile } = require("@creuna/codegen");
const path = require("path");

createPagesFile({
  fileName: "paths.js",
  searchPath: path.join(__dirname, "pages"),
  outputPath: __dirname
});
```

**paths.js**

```js
// NOTE: Do not edit this file. It is automatically generated.
module.exports = ["/some-path"];
```

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