swig
Swig is a simple CLI tool for automating dev workflows via compositions of series and parallel tasks.
Write simple javascript or typescript functions and easily execute them in a shell in your project's root directory.
Fast. Simple. Convenient.
Pre-requisites
Node.js >= 16 (version 20 is recommended)
Why Swig Instead of Gulp?
Why recreate the "series", "parallel" and "task runner" functionality that Gulp already has? Gulp is great, but I wanted to be able to customize how it works, strip out all the noise and add the ability to use a variety of javascript/typescript flavors with little or no extra setup required.
Typescript Quick Start
Swig does not require typescript, but it is the recommended approach. For instructions on quickly setting up a new project that uses swig with typescript, see Swig Typescript Quick Start (or simply run npx swig-cli-init@latest
in your target directory).
For general getting started steps, continue below.
Getting Started
-
(Optional) Install swig-cli
globally for convenient shortened commands and much faster task execution (no initial delay from npm/npx):
# With npm:
npm i -g swig-cli@latest
# OR with volta:
volta install swig-cli
-
Install swig-cli
package as a dev dependency so you can import series
and parallel
in your swigfile:
npm i -D swig-cli
-
Create a swigfile
in the root of your project, such as swigfile.js
(see Swigfile Syntax Options Matrix)
-
Add some tasks to your swigfile (see Series, Parallel and Composability Example)
-
List detected tasks from your swigfile (exported functions):
# Global swig-cli install
swig
# OR local swig-cli install
npx swig
-
Run a task:
# Global swig-cli install
swig yourTask
# OR local swig-cli install
npx swig yourTask
Swigfile Syntax Options Matrix
Your swigfile
can be one of the following:
swigfile.cjs
swigfile.mjs
swigfile.js
swigfile.ts
If there are multiple swigfiles in your project directory, swig will use the first one it finds, using the order above. The following shows the various options for your swigfile file extension, package.json type
and javascript/typescript syntax flavor to use.
Swigfile | package.json type | Syntax | Notes |
---|
.js | module | ESM | |
.js | commonjs | CommonJS | |
.cjs | any | CommonJS | |
.mjs | any | ESM | |
.ts | commonjs | CommonJS | Must have valid tsconfig.json options. Must use ts-node and NOT tsx for CommonJS. |
.ts | module | ESM | Must have valid tsconfig.json options. Must have either tsx or ts-node installed. |
If using typescript:
Series, Parallel and Composability Example
A dev task like build
might have several steps where some steps can happen in parallel and others must happen sequentially. So we define each of the steps as functions and compose them into an exported task we're calling simply build
:
Sample swigfile contents:
async function buildPrep() {...}
async function buildServer() {...}
async function buildClient() {...}
async function postBuild() {...}
export const build = series(buildPrep, parallel(buildClient, buildServer), postBuild)
And then we can run it with :
// Global swig-cli install
swig build
// OR local swig-cli install
npx swig build
Example Projects
See dotnet-react-sandbox and it's use of swig-cli-modules to encapsulate tasks for projects that use it as a template.
Compatibility
NodeJS Version Support
Node.js versions >= 16 are supported.
Tested Node.js versions:
- 16.20.2
- 18.16.1
- 18.19.0
- 20.19.2
- 22.16.0
- 24.1.0
Important: NodeJS and/or tsx has a bug that prevents swig from working with tsx for 18.17.x and 18.18.x versions. If you want to use the tsx variant of swig and have one of those versions, you'll need to either downgrade to 18.16.x (or below) or upgrade to 18.19.x (or above).
Optional dependencies
Known working versions of optional dependencies for typescript variant projects:
Additional Documentation