UNPKG

allspark

Version:

allspark is a modern webapp generator & launcher

168 lines (150 loc) 5.28 kB
#!/usr/bin/env node 'use strict'; // const { exec } = require('child_process') // can't run interactive commands. var execSh = require('exec-sh'); var jetpack = require('fs-jetpack'); var program = require('commander'); var chalk = require('chalk'); /* eslint-disable no-console */ var log = console.log; var logHeader = function logHeader(msg) { return log(chalk.yellow('\n' + msg)); }; program.version('0.0.9').option('-p, --path <path>', 'Change base path (use relative path)', '.').option('-a, --add <module>', 'Add a module to list (in allspark.json)', '.').parse(process.argv); logHeader('✨ Allspark ✨'); var CURRENT_FULL_PATH = process.cwd(); var BASE_PATH = program.path; // relative path. (default to '.') BASE_PATH = BASE_PATH.slice(-1) === '/' ? BASE_PATH.slice(0, -1) : BASE_PATH; var ALLSPARK_PATH = BASE_PATH + '/allspark'; var ALLSPARK_FULL_PATH = CURRENT_FULL_PATH + BASE_PATH.slice(1) + '/allspark'; // const BASE_PATH = SRC_PATH.split('/').slice(0, -1).join('/') // remove the last-dir part. // const SRC_PATH_ABS = CURRENT_FULL_PATH + '/' + SRC_PATH var configFilePath = ALLSPARK_PATH + '/allspark.json'; var CONFIG = jetpack.read(configFilePath, 'json'); if (!CONFIG || !CONFIG.modules) { // config file not found! #TODO => auto create it? process.exit(); } var processCommand = function processCommand(cmd, Module) { var res = cmd; res = res.replace('$DIR', './allspark/' + Module.ID); return res; }; var execArray = function execArray(cmds, callback) { var execNext = function execNext() { var cmd = cmds.shift() || ''; // cmd = cmd.replace('$DIR', './allspark/' + Module.ID) if (cmd) { if (cmd.indexOf('!! ') === 0) { logHeader('⚡️ ' + cmd.slice(3)); cmds.length ? execNext() : callback(null); } else { execSh(cmd, { cwd: BASE_PATH }, function (err) { // log('cmd: ', cmd) if (err) { callback(err); } else { cmds.length ? execNext() : callback(null); } }); } } }; execNext(); }; // install dependencies & devDependencies of a Module var installDeps = function installDeps(Module) { var cmds = []; var checkDeps = function checkDeps(depsArr) { var depsToAdd = []; if (depsArr && depsArr.length > 0) { depsArr.forEach(function (dep) { var depPath = BASE_PATH + '/node_modules/' + dep; // log('dep: ' + depPath) if (jetpack.exists(depPath) !== 'dir') { depsToAdd.push(dep); } }); // log('depsToAdd: ', depsToAdd) } return depsToAdd; }; if (typeof Module.install === 'function') { var json = Module.install(); var depsToAdd = checkDeps(json.dependencies); if (depsToAdd.length > 0) { cmds.push('yarn add ' + depsToAdd.join(' ')); } var devDepsToAdd = checkDeps(json.devDependencies); if (devDepsToAdd.length > 0) { cmds.push('yarn add ' + devDepsToAdd.join(' ') + ' --dev'); } } return cmds; }; log('- path: ', BASE_PATH); log('- modules: ', CONFIG.modules); var cmds = []; CONFIG.modules.forEach(function (moduleName) { var moduleFullPath = ALLSPARK_FULL_PATH + '/' + moduleName + '/index.js'; var module = require(moduleFullPath); var Module = module.Module; // logHeader('⚡️ ' + Module.NAME) cmds.push('!! ' + Module.NAME); cmds = cmds.concat(Module.install().commands || []); var installCmds = installDeps(Module); cmds = cmds.concat(installCmds); var json = Module.start(); if (json.commands) { json.commands.forEach(function (cmd) { return cmds.push(processCommand(cmd, Module)); }); // cmds = cmds.concat(json.commands) } log('exec commands:\n', cmds); }); execArray(cmds, function () {}); // jetpack // .find(ALLSPARK_PATH, { // matching: ['**/index.js'], // recursive: true, // }) // .forEach(filePath => { // // filePath is a relative full-path from BASE_PATH // // example "allspark --path ./example" => filePath = example/allspark/go-apis/index.js // // const module = require(CURRENT_FULL_PATH + '/' + filePath) // // ... // // const Module = module.Module // // logHeader(Module.NAME) // // let cmds = [] // // cmds = cmds.concat(Module.install().commands || []) // // const installCmds = installDeps(Module) // // cmds = cmds.concat(installCmds) // // const json = Module.start() // // if (json.commands) { // // cmds = cmds.concat(json.commands) // // } // // log('exec commands:\n', cmds) // // execArray(Module, cmds, () => {}) // // res.cmd.forEach(cmd => { // // log('cmd: ', cmd) // // execSh(cmd, { cwd: BASE_PATH }, function (err) { // // if (err) { // // console.log('Exit code: ', err.code) // // } // // // collect streams output // // // execSh(['bash -c id', 'echo lorem >&2'], true, function ( // // // err, // // // stdout, // // // stderr // // // ) { // // // if (err) log('error: ', err) // // // // log('stdout: ', stdout) // // // // log('stderr: ', stderr) // // // }) // // }) // // }) // }) // export default () => { // return 'hello world'; // };