-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ade Yahya
committed
Nov 7, 2017
1 parent
7f98f8c
commit 44a9656
Showing
5 changed files
with
315 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,115 @@ | ||
#!/usr/bin/env node | ||
var program = require('commander'), | ||
filendir = require('filendir'), | ||
colors = require('colors'), | ||
templates = require('./template.js'); | ||
const program = require('commander'); | ||
const filendir = require('filendir'); | ||
const colors = require('colors'); | ||
const templates = require('./template.js'); | ||
const fs = require('fs'); | ||
const R = require('ramda') | ||
const inquirer = require("inquirer") | ||
const path = require("path") | ||
|
||
var createFile = function(path,css) { | ||
css = css || 'scss' | ||
const createConfig = () => { | ||
const questions = [ | ||
{ | ||
type: 'list', | ||
name: 'stylesheet', | ||
message: 'Choose your stylesheet', | ||
choices: ['sass', 'scss', 'styled-jsx', 'less', 'stylus', 'css', 'none'], | ||
filter: function (val) { | ||
return val.toLowerCase(); | ||
} | ||
}, | ||
{ | ||
type: 'list', | ||
name: 'typechecker', | ||
message: 'Choose your TypeChecker', | ||
choices: ['flow', 'typescript'], | ||
filter: function (val) { | ||
return val.toLowerCase(); | ||
} | ||
} | ||
] | ||
|
||
var filename = path.match(/[^\\|\/]*$/)[0] | ||
var dir = path.substring((path.match(/[^\\|\/]*$/).index), -1) | ||
inquirer.prompt(questions).then(function (answers) { | ||
filendir.writeFile("regenr.config.js", templates.config(answers), (err) => { | ||
if (err) | ||
throw new Error(err) | ||
|
||
console.log(`::React Generator::`) | ||
filendir.writeFile(dir + filename + "/" + filename + ".js", templates.getReact(filename, css, true) , function(err) { | ||
if (err) return console.log(err) | ||
console.log("Config File Created!") | ||
}) | ||
}); | ||
} | ||
|
||
console.log(colors.yellow.underline(`${dir}${filename}/${filename}.js`)) | ||
return true; | ||
}) | ||
const createFile = (file, config) => { | ||
const filename = file.match(/[^\\|\/]*$/)[0] | ||
const dir = file.substring((file.match(/[^\\|\/]*$/).index), -1) | ||
const fullPath = path.join(dir, filename) | ||
const extension = config.typechecker === "typescript" ? "ts" : "js" | ||
|
||
filendir.writeFile(`${dir}${filename}/${filename}.${css}`, templates.getStyle(filename, css) , function(err) { | ||
if (err) return console.log(err) | ||
console.log(colors.blue.underline(`${dir}${filename}/${filename}.${css}`)) | ||
return true; | ||
}) | ||
// check is file exist? | ||
fs.readdir(fullPath, (err, files) => { | ||
if (err) { | ||
filendir.writeFile( | ||
path.join(fullPath, "package.json"), | ||
templates.index(filename, extension), | ||
err => { | ||
if (err) | ||
throw new Error(err) | ||
|
||
filendir.writeFile(dir + filename + "/" + "package.json", templates.getIndex(filename, true) , function(err) { | ||
if (err) return console.log(err) | ||
console.log(`created file ${path.join(fullPath, "package.json")}`) | ||
} | ||
) | ||
|
||
console.log(colors.magenta.underline(`${dir}${filename}/index.js`)) | ||
return true; | ||
}) | ||
filendir.writeFile( | ||
path.join(fullPath, filename + "." + extension), | ||
templates.react(filename, config), | ||
err => { | ||
if (err) | ||
throw new Error(err) | ||
|
||
console.log(`created file ${path.join(fullPath, filename + "." + extension)}`) | ||
} | ||
) | ||
|
||
if (config.stylesheet != "none") { | ||
const styleExtension = config.stylesheet === "styled-jsx" ? "js" : config.stylesheet | ||
filendir.writeFile( | ||
path.join(fullPath, "style." + styleExtension), | ||
config.stylesheet === "styled-jsx" | ||
? 'import css from "styled-jsx/css\nexport const style = css\`\`' | ||
: "", | ||
err => { | ||
console.log(`created file ${path.join(fullPath, "style." + styleExtension)}`) | ||
} | ||
) | ||
} | ||
} | ||
|
||
if (files) { | ||
return console.log(colors.red(`${fullPath} already exist!`)) | ||
} | ||
}) | ||
} | ||
|
||
const initialize = (file, program) => { | ||
if (file === "init") | ||
return createConfig() | ||
|
||
fs.readFile("regenr.config.js", {encoding: "utf8", flag: "r"}, (err, data) => { | ||
if (err) | ||
console.log(colors.red("can't find config file, try to run:") + "\n=> regenr init") | ||
|
||
if (data) { | ||
const config = require("./regenr.config.js") | ||
return createFile(file, config) | ||
} | ||
}) | ||
} | ||
|
||
program | ||
.arguments('<file>') | ||
.option('-c, --css <css>', 'Css preprocessor') | ||
// .option('-c, --config <config>', 'Config File') | ||
.action(function(file) { | ||
if (typeof program.css != 'undefined') { | ||
createFile(file, program.css) | ||
} else { | ||
createFile(file) | ||
} | ||
initialize(file, program) | ||
}) | ||
.parse(process.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
stylesheet: "scss", | ||
typechecker: "flow", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,32 @@ | ||
var template = (() => { | ||
var options = { | ||
css: 'scss', | ||
privateRep: true, | ||
cssModules: false | ||
} | ||
|
||
var getIndex = (componentName, privateRep) => { | ||
privateRep = privateRep || options.privateRep | ||
return `{ | ||
module.exports = { | ||
index: (componentName, extension) => { | ||
return `{ | ||
"name": "${componentName}", | ||
"version": "0.0.0", | ||
"private": ${privateRep}, | ||
"main": "./${componentName}.js" | ||
"private": true, | ||
"main": "./${componentName}.${extension}" | ||
}` | ||
} | ||
|
||
var getReact = (componentName, style, cssModules) => { | ||
cssModules = cssModules || options.cssModules | ||
return `import React from 'react' | ||
${cssModules ? "import styles from './" + componentName + '.' + style + "'" : ''} | ||
class ${componentName} extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
} | ||
render() { | ||
return ( | ||
<div ${cssModules ? 'className={ ' + 'styles.' + componentName + ' }' : '' }> | ||
{/* Your code here */} | ||
</div> | ||
) | ||
} | ||
}, | ||
|
||
react: (componentName, config) => { | ||
return `${config.typechecker === "flow" ? "//@flow" : null} | ||
import React from "react"; | ||
class ${componentName} extends React.Component<{}> { | ||
render() { | ||
return ( | ||
{/* your code here */} | ||
) | ||
} | ||
} | ||
export default ${componentName}` | ||
} | ||
|
||
var getStyle = (componentName,style) => { | ||
style = style || 'scss' | ||
|
||
if (style === 'sass') { | ||
return `.${componentName} | ||
/* Your stylesheet here */` | ||
} else { | ||
return `.${componentName} { | ||
/* Your stylesheet here */ | ||
export default ${componentName}; | ||
` | ||
}, | ||
config: config => { | ||
return `module.exports = { | ||
stylesheet: "${config.stylesheet}", | ||
typechecker: "${config.typechecker}", | ||
}` | ||
} | ||
} | ||
|
||
return { | ||
getStyle: getStyle, | ||
getReact: getReact, | ||
getIndex: getIndex | ||
} | ||
})() | ||
|
||
module.exports = template; | ||
} | ||
} |
Oops, something went wrong.