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.jsonwhich includes anxpackproperty - have a property called
maininpackage.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
pacoteto install the xPack in the global home folder - identify the
mainproperty inpackage.json - import the
XpmInitTemplateclass from the main JavaScript file by invoking require() - instantiate the
XpmInitTemplateclass - 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.