UNPKG

5.1 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// const { exec } = require('child_process') // can't run interactive commands.
4const execSh = require('exec-sh')
5const jetpack = require('fs-jetpack')
6const program = require('commander')
7const chalk = require('chalk')
8/* eslint-disable no-console */
9const log = console.log
10const logHeader = msg => log(chalk.yellow('\n' + msg))
11
12program
13 .version('0.0.9')
14 .option('-p, --path <path>', 'Change base path (use relative path)', '.')
15 .option('-a, --add <module>', 'Add a module to list (in allspark.json)', '.')
16 .parse(process.argv)
17
18logHeader('✨ Allspark ✨')
19
20const CURRENT_FULL_PATH = process.cwd()
21
22let BASE_PATH = program.path // relative path. (default to '.')
23BASE_PATH = BASE_PATH.slice(-1) === '/' ? BASE_PATH.slice(0, -1) : BASE_PATH
24
25const ALLSPARK_PATH = BASE_PATH + '/allspark'
26const ALLSPARK_FULL_PATH = CURRENT_FULL_PATH + BASE_PATH.slice(1) + '/allspark'
27// const BASE_PATH = SRC_PATH.split('/').slice(0, -1).join('/') // remove the last-dir part.
28// const SRC_PATH_ABS = CURRENT_FULL_PATH + '/' + SRC_PATH
29
30const configFilePath = ALLSPARK_PATH + '/allspark.json'
31const CONFIG = jetpack.read(configFilePath, 'json')
32if (!CONFIG || !CONFIG.modules) {
33 // config file not found! #TODO => auto create it?
34 process.exit()
35}
36
37const processCommand = (cmd, Module) => {
38 let res = cmd
39 res = res.replace('$DIR', './allspark/' + Module.ID)
40 return res
41}
42
43const execArray = (cmds, callback) => {
44 const execNext = () => {
45 const cmd = cmds.shift() || ''
46 // cmd = cmd.replace('$DIR', './allspark/' + Module.ID)
47 if (cmd) {
48 if (cmd.indexOf('!! ') === 0) {
49 logHeader('⚡️ ' + cmd.slice(3))
50 cmds.length ? execNext() : callback(null)
51 } else {
52 execSh(cmd, { cwd: BASE_PATH }, err => {
53 // log('cmd: ', cmd)
54 if (err) {
55 callback(err)
56 } else {
57 cmds.length ? execNext() : callback(null)
58 }
59 })
60 }
61 }
62 }
63 execNext()
64}
65
66// install dependencies & devDependencies of a Module
67const installDeps = Module => {
68 const cmds = []
69
70 const checkDeps = depsArr => {
71 const depsToAdd = []
72 if (depsArr && depsArr.length > 0) {
73 depsArr.forEach(dep => {
74 const depPath = `${BASE_PATH}/node_modules/${dep}`
75 // log('dep: ' + depPath)
76 if (jetpack.exists(depPath) !== 'dir') {
77 depsToAdd.push(dep)
78 }
79 })
80 // log('depsToAdd: ', depsToAdd)
81 }
82 return depsToAdd
83 }
84 if (typeof Module.install === 'function') {
85 const json = Module.install()
86 const depsToAdd = checkDeps(json.dependencies)
87 if (depsToAdd.length > 0) {
88 cmds.push(`yarn add ${depsToAdd.join(' ')}`)
89 }
90 const devDepsToAdd = checkDeps(json.devDependencies)
91 if (devDepsToAdd.length > 0) {
92 cmds.push(`yarn add ${devDepsToAdd.join(' ')} --dev`)
93 }
94 }
95 return cmds
96}
97
98log('- path: ', BASE_PATH)
99log('- modules: ', CONFIG.modules)
100let cmds = []
101
102CONFIG.modules.forEach(moduleName => {
103 const moduleFullPath = ALLSPARK_FULL_PATH + '/' + moduleName + '/index.js'
104 const module = require(moduleFullPath)
105
106 const Module = module.Module
107 // logHeader('⚡️ ' + Module.NAME)
108 cmds.push('!! ' + Module.NAME)
109
110 cmds = cmds.concat(Module.install().commands || [])
111 const installCmds = installDeps(Module)
112 cmds = cmds.concat(installCmds)
113
114 const json = Module.start()
115 if (json.commands) {
116 json.commands.forEach(cmd => cmds.push(processCommand(cmd, Module)))
117 // cmds = cmds.concat(json.commands)
118 }
119 log('exec commands:\n', cmds)
120})
121execArray(cmds, () => {})
122
123// jetpack
124// .find(ALLSPARK_PATH, {
125// matching: ['**/index.js'],
126// recursive: true,
127// })
128// .forEach(filePath => {
129// // filePath is a relative full-path from BASE_PATH
130// // example "allspark --path ./example" => filePath = example/allspark/go-apis/index.js
131// // const module = require(CURRENT_FULL_PATH + '/' + filePath)
132// // ...
133// // const Module = module.Module
134// // logHeader(Module.NAME)
135// // let cmds = []
136// // cmds = cmds.concat(Module.install().commands || [])
137// // const installCmds = installDeps(Module)
138// // cmds = cmds.concat(installCmds)
139// // const json = Module.start()
140// // if (json.commands) {
141// // cmds = cmds.concat(json.commands)
142// // }
143// // log('exec commands:\n', cmds)
144// // execArray(Module, cmds, () => {})
145// // res.cmd.forEach(cmd => {
146// // log('cmd: ', cmd)
147// // execSh(cmd, { cwd: BASE_PATH }, function (err) {
148// // if (err) {
149// // console.log('Exit code: ', err.code)
150// // }
151// // // collect streams output
152// // // execSh(['bash -c id', 'echo lorem >&2'], true, function (
153// // // err,
154// // // stdout,
155// // // stderr
156// // // ) {
157// // // if (err) log('error: ', err)
158// // // // log('stdout: ', stdout)
159// // // // log('stderr: ', stderr)
160// // // })
161// // })
162// // })
163// })
164
165// export default () => {
166// return 'hello world';
167// };