UNPKG

svelte-scaffold

Version:

A CLI tool for scaffolding svelte apps

107 lines (95 loc) 3.08 kB
#!/usr/bin/env node const inquirer = require("inquirer"); const fs = require("fs"); const choices = fs.readdirSync(`${__dirname}/templates`); const currentDirectory = process.cwd(); console.log(` Svelte app is the actual web app . And svelte library allows developers to create custom sharable components . `); const inquiry = [ { name: "choice", type: "list", message: "Project template?", choices }, { name: "appname", type: "input", message: "Project name:", validate: function(input) { if (/^([A-Za-z\-\_\d])+$/.test(input)) return true; else return "Project name may only include letters, numbers, underscores and hashes."; } } ]; inquirer.prompt(inquiry).then(answers => { const projectChoice = answers["choice"]; const projectName = answers["appname"]; const templatePath = `${__dirname}/templates/${projectChoice}`; if (fs.existsSync(`${currentDirectory}/${projectName}`)) { console.log( "A directory with this name already exists . Please choose a different name" ); } else { fs.mkdirSync(`${currentDirectory}/${projectName}`); createDirectoryContents(templatePath, projectName); console.log(` Congrats .... your app is created .. now ... 1 . cd ${projectName} 2 . yarn 3 . yarn dev Then start hacking `); } }); function createDirectoryContents(templatePath, newProjectPath) { const filesToCreate = fs.readdirSync(templatePath); filesToCreate.forEach(file => { const origFilePath = `${templatePath}/${file}`; const stats = fs.statSync(origFilePath); if (stats.isFile()) { const contents = fs.readFileSync(origFilePath, "utf8"); const writePath = `${currentDirectory}/${newProjectPath}/${file}`; fs.writeFileSync(writePath, contents, "utf8"); } else if (stats.isDirectory()) { fs.mkdirSync(`${currentDirectory}/${newProjectPath}/${file}`); fs.readFile( `${currentDirectory}/${newProjectPath}/README.md`, "utf8", function(_err, data) { if (data !== undefined) { let formatted = data.replace("[[APPNAME]]", newProjectPath); fs.writeFile( `${currentDirectory}/${newProjectPath}/README.md`, formatted, function() { fs.readFile( `${currentDirectory}/${newProjectPath}/package.json`, "utf8", function(_err, data) { if (data !== undefined) { let formatted = data.replace( "[[APPNAME]]", newProjectPath ); fs.writeFileSync( `${currentDirectory}/${newProjectPath}/package.json`, formatted ); } } ); } ); } } ); createDirectoryContents( `${templatePath}/${file}`, `${newProjectPath}/${file}` ); } }); }