# @jamen/new-common

> Base functions for project generators.

Latest version **1.0.0** (published 2018-07-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jamen/new-common
pnpm add @jamen/new-common
yarn add @jamen/new-common
bun add @jamen/new-common
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-07-13 |
| First published | 2018-06-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 13 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Jamen Marz |
| Maintainers | jamen |

## Links

- npm: https://www.npmjs.com/package/@jamen/new-common
- npm.io page: https://npm.io/package/@jamen/new-common

## Dependencies (5)

- [tre](https://npm.io/package/tre.md) 1.0.5
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [prompts](https://npm.io/package/prompts.md) ^0.1.8
- [license-me](https://npm.io/package/license-me.md) 0.0.2
- [lodash.template](https://npm.io/package/lodash.template.md) 4.4.0

## Recent versions

- 1.0.0 (latest) — 2018-07-13
- 0.1.0 — 2018-06-28

## README

# @jamen/new-common

Base functions for project generators.

## Install

```sh
npm i @jamen/new-common
```

## Usage

### `create(output, questions, templates)`

Prompts for the `questions` then writes to the `output` using `templates`.

- `output` is the directory where templates created.
- `questions` is a function of `questions(output) -> Promise<Array<question>>` (see [propmts][1])
- `templates` is a function of `templates(answers) -> Promise<Array<template>>` (see [Templates][2])

```js
create(
    process.cwd(),
    questions,
    templates
)
```

### `preview(output, questions, templates)`

Like `create()`, but doesn't create any files. Just runs the prompts and displays a file tree at the end.  This is helpful, because answers can often affect the structure of the output.

### Templates

Each template is:

```js
templates : {
    input: String,
    create: Function<answers> -> Promise<Array<template>>
}

template : {
    from: String,
    to: String,
    use: Function<from, to, answers>
}
```

The function in `use` is called a "directive" or simply a "use", and its job is to copy or create the source (if there is one) to the destination, using prompt answers to add variance to the output.

Note that `from` can be absent, since the files wont always have static contents (i.e. empty contents, or completely dynamic contents).

### Uses

The package has common uses for templating, merging, and creating files.

- `createTemplateNormal`: Templates with [lodash.template][3]
- `createTemplateJson`: Templates and then merges into destination (e.g. a `package.json`)
- `createTemplateUniq`: Templates and enforces unique lines (e.g. a `.gitignore`)
- `createLicense`: Creates a `license` file given a license type answer.
- `createFile`: Creates any file.

An example of how two variations of these functions would look:

```js
const templates = answers => [
    {
        from: 'package.json',
        to: 'package.json',
        use: createTemplateJson
    },
    {
        from: 'lib/project.js',
        to: `lib/${basename(answers.name)}.js`,
        use: createTemplateNormal
    },
    {
        to: 'license',
        use: createLicense
    },
]
```

### Utils

The package has other utility functions for common tasks

- `exactTemplates(input, templates)` is used for composing templates from a different generator.
- `installNpmPackages(dir, packages, dev)` is to install dependencies (with `yarn`, `pnpm`, or `npm`).
- `templateFile(from, answers)` is used for reading and templating a file without writing it yet.
- `message` are functions to create any message logged by the core functions.
- `prompts` is [prompts][1].

Other utils are functions from Node core that have been promisified.

[1]: https://npmjs.com/prompts
[2]: #templates
[3]: https://npmjs.com/lodash.template

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