UNPKG

create-modulo

Version:

Starter projects for Modulo.html - Ready for all uses - Markdown-SSG / SSR / API-backed SPA

152 lines (133 loc) 4.72 kB
#! /usr/bin/env node const fs = require("fs"); const path = require("path"); const https = require('https'); // or 'https' for https:// URLs const TERM = { MAGENTA_BG: '\x1b[45m', BLACK_FG: '\x1b[30m', MAGENTA_FG: '\x1b[35m', RED_FG: '\x1b[31m', GREEN_FG: '\x1b[32m', YELLOW_FG: '\x1b[33m', BLUE_FG: '\x1b[34m', RESET: '\x1b[0m', BRIGHT: '\x1b[1m', DIM: '\x1b[2m', UNDERSCORE: '\x1b[4m', }; //TERM.LOGO = TERM.DIM + '[%]' + TERM.RESET; //TERM.LOGO = TERM.MAGENTA_BG + '[ᵐ°dᵘ⁄o]' + TERM.RESET;// + TERM.UNDERSCORE; TERM.LOGO = TERM.MAGENTA_FG + 'ᵐ°dᵘ⁄o' + TERM.RESET;// + TERM.UNDERSCORE; const log = (...args) => { console.log(TERM.LOGO, ' ', ...args); }; const logSuccess = (...args) => { console.log(TERM.LOGO, TERM.GREEN_FG + 'SUCCESS' + TERM.RESET, ...args); }; const logError = (...args) => { console.log(TERM.LOGO, TERM.RED_FG + 'ERROR' + TERM.RESET, ...args); }; function copyRecursiveSync(src, dest) { const exists = fs.existsSync(src); const stats = exists && fs.statSync(src); const isDirectory = exists && stats.isDirectory(); if (isDirectory) { try { fs.mkdirSync(dest); } catch { // Ignore errors } for (const childItemName of fs.readdirSync(src)) { copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName)); } } else { fs.copyFileSync(src, dest); } } function parseArgs(argArray, shiftFirst=true) { if (shiftFirst) { argArray.shift(); // always get rid of first argument } if (argArray.length < 1) { return argArray; } if (argArray[0].endsWith('modulo.js') || argArray[0].endsWith('create-modulo')) { argArray.shift(); // shift again, if necessary } return argArray; } /* // Helper utility to download files, using Node standard library function downloadFile(url, outPath, callback) { https.get(url, response => { if (response.statusCode >= 400) { // Errors, e.g. 404 log('-- !! create-modulo failed to download: ', outPath); log(response); } else if (response.statusCode >= 300) { // Redirect downloadFile(response.headers.location, outPath, callback); } else { // Normal case, e.g. 200 OK const file = fs.createWriteStream(outPath, 'utf8'); file.on('finish', () => { file.close(); // After download completed close filestream callback(); }); response.pipe(file); } }); } function downloadModulo(name, version, callback) { const URL = `https://unpkg.com/mdu.js@${ version }/src/Modulo.js`; downloadFile(URL, `${ name }/src/static/js/Modulo.js`, callback); } */ function jsonWriteSync(name) { const path = `./${ name }/package.json`; const data = Object.assign({ name }, JSON.parse(fs.readFileSync(path))) const dataStr = JSON.stringify(data, null, 4); fs.writeFileSync(path, dataStr); } function main(argv, cwd, platform) { const args = parseArgs(argv); let name = 'new-modulo-app'; if (args.length < 1) { log(`Defaulting to "${ name }"`); } else { name = args[0]; } let template = args[1] if (template && !(template in { starter: 1 })) { logError('The following template(s) are available: starter'); return; } else if (template) { log(`Project template: "${ template }"`); } let templateName = template || 'starter' const templatePath = path.join(__dirname, '..', 'build', templateName); copyRecursiveSync(templatePath, name); jsonWriteSync(name); log(`Created ${ templateName } project at: ${ name }/`); //logSuccess('---'); // cleaning up output //log('Run Modulo commands in Node.js:'); //log(` cd ${ name }`); //log(` npm run modulo`); if (template) { logSuccess('---'); return; // dont try to auto-open } const open = (platform === 'darwin' ? 'open' : platform === 'win32' ? 'start' : 'xdg-open'); const fullCommand = `${ open } index.html` const projectPath = cwd + '/' + name; try { const child_process = require("child_process"); const opts = { cwd: projectPath, detached: true, stdio: 'ignore' }; child_process.spawn('/bin/sh', [ '-c', fullCommand ], opts); logSuccess('---'); } catch { log('To view your site, open index.html in your browser:'); log(` cd ${ name }`); log(` firefox index.html`); } } main(Array.from(process.argv), process.cwd(), process.platform);