npm-init-template
This is a library for building npm init packages more easily,
by providing a set of utility functions for collecting
information and then creating the starter package from a folder
of template files.
USAGE
Create a package that exports a single bin command:
{
"name": "create-my-special-thing",
"version": "1.0.0",
"description": "npm init my-special-thing",
"bin": "index.mjs"
}
Then in that bin script (shown here as index.mjs), put code
like this:
#!/usr/bin/env node
import { Init } from 'npm-init-template'
const { prompt, build, values, positionals, run } = new Init(
import.meta.url,
)
await prompt('What is your name?', 'name')
await prompt('What is your quest?', 'quest')
await prompt('What is your favorite color?', 'color', {
default: 'blue',
})
await prompt(
'What is the flight average velocity of an unladen sparrow?',
'sparrow',
)
if (values.sparrow === "I don't know") {
throw new Error('AAAHHHH!!!!')
}
await build({
target: positionals[0] || `${values.name}-${values.quest}`,
})
await run('npm install')
In ./templates/quest.html.mustache, you can put mustache
templates, something like
this:
<html>
<body style="background-color:{{ color }}">
<h1>Hello, {{name}}!</h1>
<p>Good luck on {{quest}}.</p>
<p>
The average flight velocity of an unladen sparrow is {{ sparrow }}.
</p>
</body>
</html>
JSON files use JSON.stringify as the default escaping
mechanism, so in templates/package.json.mustache, you can put
something like this:
{
"name": <% name %>,
"description": <% quest %>
}
Templates can also include each other as partials, each will be
loaded with its name relative to the templates folder.
<html>
<body style="background-color:{{ color }}">
<h1>Hello, {{name}}!</h1>
<p>Good luck on {{quest}}.</p>
<p>
The average flight velocity of an unladen sparrow is {{ sparrow }}.
</p>
<p>Your `package.json` file is:</p>
<pre>{{>package.json}}</pre>
</body>
</html>