From 6258ec0b068c086abc1ddebf2520ec3c089f92d3 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Sat, 17 Apr 2021 16:35:38 -0700 Subject: [PATCH] Add CLI handler for elm-pages add command. --- docs/2.0-alpha-instructions.md | 2 +- generator/src/cli.js | 8 ++++++ generator/src/codegen-template-module.js | 32 +++++++++--------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/docs/2.0-alpha-instructions.md b/docs/2.0-alpha-instructions.md index b9fe749b..50518508 100644 --- a/docs/2.0-alpha-instructions.md +++ b/docs/2.0-alpha-instructions.md @@ -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` diff --git a/generator/src/cli.js b/generator/src/cli.js index 0eec3c00..e54d99b6 100755 --- a/generator/src/cli.js +++ b/generator/src/cli.js @@ -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 ") + .description("create a new Page Template module") + .action(async (moduleName) => { + await generate.run({ moduleName }); + }); + program.parse(process.argv); } diff --git a/generator/src/codegen-template-module.js b/generator/src/codegen-template-module.js index edbe84f3..57964fdc 100755 --- a/generator/src/codegen-template-module.js +++ b/generator/src/codegen-template-module.js @@ -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 };