Add CLI handler for elm-pages add command.

This commit is contained in:
Dillon Kearns 2021-04-17 16:35:38 -07:00
parent 0c6c8610cc
commit 6258ec0b06
3 changed files with 21 additions and 21 deletions

View File

@ -45,5 +45,5 @@ Then you need to install the NPM pacakge from the top-level project with `npm in
### Commands
- Run a dev server - `elm-pages dev`
- Generate scaffolding for a new Page Template - `../../generator/src/codegen-template-module.js Slide.Number_`
- Generate scaffolding for a new Page Template - `elm-pages add Slide.Number_`
- Run a build - `elm-pages build`

View File

@ -2,6 +2,7 @@
const build = require("./build.js");
const dev = require("./dev-server.js");
const generate = require("./codegen-template-module.js");
const commander = require("commander");
@ -29,6 +30,13 @@ async function main() {
await dev.start(options);
});
program
.command("add <moduleName>")
.description("create a new Page Template module")
.action(async (moduleName) => {
await generate.run({ moduleName });
});
program.parse(process.argv);
}

View File

@ -1,34 +1,26 @@
#!/usr/bin/env node
const fs = require("./dir-helpers.js");
const path = require("path");
const routeHelpers = require("./route-codegen-helpers");
async function run() {
if (process.argv.length === 3) {
const moduleName = process.argv[2];
if (!moduleName.match(/[A-Z][A-Za-z0-9]+(\.[A-Z][A-Za-z0-9])*/)) {
console.error("Invalid module name.");
process.exit(1);
}
const content = fileContent(moduleName);
const fullFilePath = path.join(
`src/Template/`,
moduleName.replaceAll(".", "/") + ".elm"
);
await fs.tryMkdir(path.dirname(fullFilePath));
fs.writeFile(fullFilePath, content);
} else {
console.error(`Unexpected CLI options: ${process.argv}`);
async function run({ moduleName }) {
if (!moduleName.match(/[A-Z][A-Za-z0-9]+(\.[A-Z][A-Za-z0-9])*/)) {
console.error("Invalid module name.");
process.exit(1);
}
const content = fileContent(moduleName);
const fullFilePath = path.join(
`src/Template/`,
moduleName.replaceAll(".", "/") + ".elm"
);
await fs.tryMkdir(path.dirname(fullFilePath));
fs.writeFile(fullFilePath, content);
}
/**
* @param {string} templateName
*/
function fileContent(templateName) {
return `module Template.${templateName} exposing (Model, Msg, template)
return `module Template.${templateName} exposing (Model, Msg, StaticData, template)
import Element exposing (Element)
import Document exposing (Document)
@ -95,4 +87,4 @@ view static =
`;
}
run();
module.exports = { run };