Project templates
As presented in the User's Guide, xpm is able to create new projects based on templates.
For this, pass the --template <package>
option to xpm init
.
Template project structure
To be accepted as a template, a project must:
- be an xpm package (have a
package.json
which includes anxpack
property - have a property called
main
inpackage.json
, pointing to a JavaScript file that can be consumed byawait import()
(formerlyrequire()
) - the main file must export a class called
XpmInitTemplate
- an instances of this class must have a
run()
method. - have all dependencies bundled in (via
bundleDependencies
)
xpm project creation logic
The steps invoked by xpm to initialise a project from a template are:
- call
pacote
to install the xPack in the global home folder - identify the
main
property inpackage.json
- import the
XpmInitTemplate
class from the main JavaScript file by invoking require() - instantiate the
XpmInitTemplate
class - execute the
run()
method.
The full code is in init.js
, but a simplified version looks like this:
await pacote.extract(config.template, globalPackagePath,
{ cache: cacheFolderPath })
const mainTemplatePath = path.join(globalPackagePath, globalJson.main)
context.CliError = CliError
context.CliExitCodes = CliExitCodes
const { XpmInitTemplate } = await import(mainTemplatePath)
const xpmInitTemplate = new XpmInitTemplate(context)
const code = await xpmInitTemplate.run()
TODO: explain the user configurable properties.