1 import { cd, mkdir, echo, ShellString, cat, test } from 'shelljs'
2 import commandLineCommands from 'command-line-commands'
3 import commandLineArgs from 'command-line-args'
5 const validCommands = [ 'component' ]
6 const { command, argv } = commandLineCommands(validCommands)
8 function template(strings, ...keys) {
9 return (function(...values) {
10 var dict = values[values.length - 1] || {}
11 var result = [strings[0]]
12 keys.forEach(function(key, i) {
13 var value = Number.isInteger(key) ? values[key] : dict[key]
14 result.push(value, strings[i + 1])
16 return result.join('')
21 template`import React from 'react'
22 import styles from './${0}.scss'
24 class ${0} extends React.Component {
27 <div className={styles.base}>
46 let optionDefinitions = [
47 { name: 'path', type: String, defaultOption: true }
50 let options = commandLineArgs(optionDefinitions, argv)
52 let path = options.path
53 if (path === undefined) {
54 echo('No component name specified.\nUsage: `npm run generate-component My/Name`')
61 let name = path.split('/').pop()
62 ShellString(baseJsx(name)).to(path + `/${name}.jsx`)
63 ShellString(baseScss).to(path + `/${name}.scss`)
65 let indexPath = path.split('/').slice(0,-1).join('/') + '/index.js'
66 if (test('-f', indexPath)) {
67 let contents = cat(indexPath)
69 let importString = `import ${name} from './${name}/${name}'`
70 let exportString = ` ${name},\n`
72 if (contents.indexOf(importString) < 0) {
73 contents = `${importString}\n${contents}`
75 if (contents.indexOf(exportString) < 0) {
76 let exportTarget = 'export {\n'
77 let position = contents.indexOf(exportTarget) + exportTarget.length
79 contents = contents.slice(0, position)
81 + contents.slice(position)
84 ShellString(contents).to(indexPath)
89 echo(`Unknown command: ${command}`)