0.2.0 • Published 4 months ago

@jakubmazanec/carson v0.2.0

Weekly downloads
-
License
AGPL-3.0-only
Repository
github
Last release
4 months ago

@jakubmazanec/carson

Installation

npm install @jakubmazanec/carson -g

⚠️ This is an ESM package! It cannot be required from a CommonJS module.

Prerequisites

  • Node.js 16 or later

Usage

Software development is constantly evolving and best practices often change. If you want to migrate to a new tool, or merely update some settings, it can be time-consuming to do it in multiple repositories. Instead, Carson lets you write and use a template that defines what files should exist and what content should they have. Instead of updating files in your repositories manually, you change only the template and Carson handles the updates automatically for you, and keeps every repository in sync. With a template, creating new projects is also super fast and simple.

Quick overview

Update workspace:

npx carson update workspace

Create a new project in an existing workspace:

npx carson create project

See CLI documentation for the complete overview.

How it works

Carson assumes your codebase is organized in a workspace, which is simply a directory, and also usually a git repository, that contains one or multiple projects. A project is a self-contained piece of code, e.g. a package, or an app.

⚠️ Currently only Node.js projects are supported.

Take a look at this simplified example of a workspace:

workspace/
├── .carson/
│   ├── .snapshots
│   └── workspace.json
├── packages/
│   ├── foo/
│   │   ├── .carson/
│   │   │   ├── .snapshots
│   │   │   └── project.json
│   │   ├── src/
│   │   │   └── main.js
│   │   └── package.json
│   └── bar/
│       ├── .carson/
│       │   ├── .snapshots
│       │   └── project.json
│       ├── src/
│       │   └── main.js
│       └── package.json
└── package.json

Notice that the workspace and each of the two packages in it (foo and bar) have a directory named .carson with 2 files:

  1. Configuration file, which specifies the ID of the Carson template that is used to define the files (or just some parts of the files) in the workspace or the project.
  2. Snapshots file with all the information necessary to revert any changes.

Let's say config files workspace/packages/foo/.carson/project.json and workspace/packages/bar/.carson/project.json both contain this:

{
  "template": "carson-templates:project"
}

That means they're using a Carson template named project from a package named carson-templates. Carson template is just a directory containing *.ejs files parsed and rendered via @jakubmazanec/template package; these files are actually EJS templates with YAML front matter. Please read @jakubmazanec/template documentation to find out in more detail how they work. Simply, they're for generating content using JavaScript as a templating language.

Yes, each "Carson template" is composed of multiple "EJS templates". Hopefully, the existence of these two different template "types" isn't very confusing.

Take a look at this simplified example of carson-templates with two templates named workspace and project:

carson-templates@1.0.0
└── templates
    ├── project
    │   └── package.json.ejs
    └── workspace
        └── package.json.ejs

Because we're interested in the project template, let's see file carson-templates@1.0.0/templates/project/package.json.ejs:

---
to: 'package.json'
strategy: merge
---
{
  "repository": {
    "directory": "<%- project.relativePath %>"
  },
  "type": "module",
  "exports": "./dist/main.js",
  "files": [
    "dist",
    "src"
  ],
  "scripts": {
    "build": "swc src -d dist"
  },
  "devDependencies": {
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.56"
  }
}

When Carson performs a workspace update, carson-templates:project template is rendered for each project that uses it, and foo, and bar have their files changed accordingly. In this example that means foo/package.json and bar/package.json are recursively merged with the JSONs obtained from the EJS template above (so SWC is added to development dependencies, or field repository.directory is set to always be the value of a variable project.relativePath, i.e. 'packages/foo' or 'packages/bar'). Updating the workspace works the same way (although using a different Carson template, e.g. carson-templates:workspace).

When you install a newer version of carson-templates that e.g. replaces SWC with another tool, before Carson does fresh update, it first uses saved (and committed to git) snapshots from the previous update to revert all its changes. This way you don't need to keep the previous version of carson-templates around and the updates can be always done automatically from the current state of the workspace.

The fact that EJS templates are just JavaScript means Carson can support almost any use case. For that, there are also more Carson template features you need to learn than this example showcases: mainly additional strategies (you can e.g. instead of merging simply overwrite the whole file, or just create a file with some default content and then let the user change it freely) and what variables with what information is available during EJS template rendering. Please read Carson templates documentation to find out in more detail how they work, or see @jakubmazanec/carson-templates for examples.

Documentation

API

See API reference for auto-generated documentation.

Configuration

Carson configuration files are located in a directory named .carson directory. Files named workspace.json and project.json are used to configure workspace and project respectively (in case of single-project workspace they're both in the same .carson directory).

You can add arbitrary data to these JSONs in addition to the options described below. Because workspace and project configurations are available as JavaScript variables during the rendering of Carson templates, they can be used to store ad-hoc data or to allow custom, per-project configuration.

workspace.json

OptionTypeDescription
templatestringCarson template ID

project.json

OptionTypeDescription
templatestringCarson template ID

CLI

update workspace [<options>]

Updates workspace and all projects using the configured Carson templates.

OptionDescription
--pathPath to the workspace. A relative path is appended to the current working directory. The file system is traversed up to the root when looking for a workspace.

create project [<options>]

Creates a new project in a workspace.

OptionDescription
--pathPath to the workspace in which the project should be created. A relative path is appended to the current working directory. The file system is traversed up to the root when looking for a workspace.
--templateID of the Carson template used to create the project.
--nameProject name.

create workspace [<options>]

Creates new workspace.

OptionDescription
--pathPath where the workspace should be created. A relative path is appended to the current working directory.
--templateID of the Carson template used to create the workspace.

Carson templates

🚧 This section is under development. Please check back later for updates.

Contributing

If you want to contribute, see CONTRIBUTING for details.

License

This package is licensed under the GNU Affero General Public License v3. See LICENSE for details.

1.0.0-next.b3909a5

4 months ago

0.3.0-next.bc04e4f

4 months ago

0.3.0-next.3967720

8 months ago

0.3.0-next.b19329b

7 months ago

0.3.0-next.50761cf

8 months ago

0.3.0-next.9d6cd14

8 months ago

0.3.0-next.f3375f0

7 months ago

0.3.0-next.7305494

7 months ago

0.1.2-20482c1

9 months ago

0.3.0-next.439463c

8 months ago

0.1.2-next.a07dada

9 months ago

0.1.2-c4a4d76

9 months ago

0.1.1-b8faa49

9 months ago

0.3.0-next.d2852b5

8 months ago

0.1.2-next.20b87a5

9 months ago

0.3.0-next.75b0d46

8 months ago

0.3.0-next.f9ef3d7

7 months ago

0.1.0-6cee770

11 months ago

0.1.1-next.121ef1e

9 months ago

0.2.0-next.e6a1d78

9 months ago

0.3.0-next.9d07aa7

8 months ago

0.1.2-a69e5e1

9 months ago

0.1.1-a1d4551

10 months ago

0.1.1-2487994

10 months ago

0.3.0-next.9befaf5

7 months ago

0.3.0-next.7a1e391

7 months ago

0.1.0-8a0a4d5

10 months ago

0.1.1-558431b

10 months ago

0.1.0-dee6883

10 months ago

0.1.2-next.e2c3f48

9 months ago

0.1.0-c2efc51

10 months ago

0.1.0-next.336fe21

10 months ago

0.3.0-next.b415399

8 months ago

0.1.1-fd09a8f

10 months ago

0.1.0-7dbb232

10 months ago

0.3.0-next.dc4435e

8 months ago

0.3.0-next.1b2cc9c

8 months ago

0.1.2-587a832

9 months ago

0.3.0-next.a956e19

7 months ago

0.1.0

10 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0-2f12db0

10 months ago

0.3.0-next.ce9dbc3

8 months ago

0.1.2-f6f0502

9 months ago

0.1.0-bc209e5

11 months ago

0.2.0

9 months ago

0.1.2-next.c0ab242

9 months ago

0.1.0-2d1145f

11 months ago

0.1.0-5f6d70c

11 months ago

0.1.0-c5ebb6f

11 months ago

0.1.0-593aae1

11 months ago

0.1.0-265acad

11 months ago

0.1.0-11dd4a3

11 months ago

0.1.0-9029e49

11 months ago

0.1.0-a500263

11 months ago