diff --git a/javascript-modules/init/lib/templates/bookshop-config.ejs.t b/javascript-modules/init/lib/templates/bookshop-config.ejs.t index 7a09c966..68077c1d 100644 --- a/javascript-modules/init/lib/templates/bookshop-config.ejs.t +++ b/javascript-modules/init/lib/templates/bookshop-config.ejs.t @@ -1,5 +1,8 @@ module.exports = { + component_directory: "<%- component_directory %>", engines: { "@bookshop/<%= ssg %>-engine": {} - } + }, + format: "<%= format %>", + style: "<%= style %>" } diff --git a/javascript-modules/init/lib/templates/css.ejs.t b/javascript-modules/init/lib/templates/css.ejs.t new file mode 100644 index 00000000..8a338b9d --- /dev/null +++ b/javascript-modules/init/lib/templates/css.ejs.t @@ -0,0 +1,3 @@ +.c-<%= componentName %> { + +} \ No newline at end of file diff --git a/javascript-modules/init/lib/templates/templates.js b/javascript-modules/init/lib/templates/templates.js index 55325b5a..1644d164 100644 --- a/javascript-modules/init/lib/templates/templates.js +++ b/javascript-modules/init/lib/templates/templates.js @@ -49,6 +49,10 @@ export const scss = { render: ejs.compile(readFileSync(join(__dirname, "scss.ejs.t"), 'utf8')), }; +export const css = { + render: ejs.compile(readFileSync(join(__dirname, "css.ejs.t"), 'utf8')), +}; + export const bookshop_toml = { render: ejs.compile(readFileSync(join(__dirname, "bookshop-toml.ejs.t"), 'utf8')), extension: 'bookshop.toml' diff --git a/javascript-modules/init/main.js b/javascript-modules/init/main.js index 11bb22c0..79504f93 100755 --- a/javascript-modules/init/main.js +++ b/javascript-modules/init/main.js @@ -107,8 +107,9 @@ const initComponent = async (options, bookshopConfigFiles) => { process.exit(1); } - let targetFormat = options.format; - if (!targetFormat) { + const allowed_formats = ["yml", "toml", "js", "json"] + let targetFormat = options.format || bookshopConfig.format || ""; + if (!targetFormat || !allowed_formats.includes(targetFormat)) { const resp = await inquirer.prompt([{ type: 'list', name: 'format', @@ -122,6 +123,22 @@ const initComponent = async (options, bookshopConfigFiles) => { } console.log(''); + const allowed_style_formats = ["css", "scss"] + let targetStyle = options.style || bookshopConfig.style || ""; + if (!targetStyle || !allowed_style_formats.includes(targetStyle)) { + const resp = await inquirer.prompt([{ + type: 'list', + name: 'style', + message: 'What flavor of CSS would you like for components?', + choices: ['SCSS (Recommended)', 'CSS'], + filter(val) { + return val.split(' ')[0].toLowerCase(); + }, + }]); + targetStyle = resp.style; + } + console.log(''); + const componentDirPath = join(bookshop, "components", options.component); mkdirSync(componentDirPath, { recursive: true }); @@ -145,9 +162,9 @@ const initComponent = async (options, bookshopConfigFiles) => { if (frameworks[0] !== "svelte") { renderFile( - templates["scss"], + templates[targetStyle], { componentName }, - join(componentDirPath, `${componentFileName}.scss`) + join(componentDirPath, `${componentFileName}.${targetStyle}`) ); } @@ -173,7 +190,12 @@ const initBookshop = async (options) => { renderFile( templates["bookshop_config"], - { ssg: options.framework[0] }, + { + component_directory: options.new, + ssg: options.framework[0], + format: options.format, + style: options.style + }, join(options.new, `bookshop`, `bookshop.config.cjs`) ); @@ -188,7 +210,7 @@ const initBookshop = async (options) => { renderFile( templates["global_style"], {}, - join(options.new, `shared`, `styles`, `global.scss`) + join(options.new, `shared`, `styles`, `global.${options.style ?? "css"}`) ); } @@ -201,18 +223,21 @@ const initBookshop = async (options) => { } options.component = "sample"; + console.log(chalk.magenta(`\nCreating a sample component in ${options.new}`)); await initComponent(options); } async function run() { - program.option("-n, --new ", "Create a new Bookshop in the given directory"); - program.option("-c, --component ", "Create a new component with the given name"); + program.option("-n, --new [project name]", "Create a new Bookshop in the given directory"); + program.option("-c, --component [component]", "Create a new component with the given name"); program.option("-f, --framework ", "Optional: Space separated list of frameworks to use. Will be auto-detected if not supplied"); - program.addOption(new Option('--format ', 'Convert Bookshop files to another format').choices(['yml', 'toml', 'json', 'js'])); + program.addOption(new Option("--format ", "Optional: The configuration format you would like to use for components")) + program.option("-s, --style