Last active
November 3, 2017 13:08
-
-
Save VinozzZ/1ed876418397d7f9d97f4de4577d8d1d to your computer and use it in GitHub Desktop.
CLI create react components and services
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
#!/usr/bin/env node | |
var Promise = require("bluebird"); | |
var fs = Promise.promisifyAll(require("fs")); | |
var directoryDict = { | |
c: "templates/componentWithoutReducer", | |
cr: "templates/componentWithReducer", | |
s: "templates/service" | |
}; | |
var pathInput = ""; | |
var nameInput = ""; | |
var categoryInput = ""; | |
var camelizedNameInput = ""; | |
var path = require("path"); | |
var mkdirp = require('mkdirp'); | |
var program = require("commander"); | |
program | |
.version('0.0.1') | |
.description('create react components and services CLI') | |
program | |
.command("component <path> <name>") | |
.alias("c") | |
.description("create a pure component") | |
.action((path, name) => { | |
generator(path, name) | |
}) | |
var getFiles = function (directory) { | |
return fs.readdirAsync(path.join(__dirname, directory)); | |
}; | |
var getContent = function (directory, filename) { | |
return fs.readFileAsync(path.join(__dirname, directory + "/" + filename), "utf8"); | |
}; | |
var writeFile = function (targetPath, targetFilePath, content) { | |
return mkdirp(targetPath, function (err) { | |
if (err) return console.log("error:", err); | |
fs.writeFileAsync(targetFilePath, content); | |
if(targetFilePath.includes("scss")) { | |
var styleIndexFolder = path.join(__dirname + "/.." + "/src/assets/stylesheets"); | |
console.log(styleIndexFolder); | |
var styleIndexFile = styleIndexFolder + "/index.scss"; | |
var importPath = path.relative(styleIndexFolder, targetFilePath); | |
importPath = importPath.replace(/\\/g, '/'); | |
fs.appendFileAsync(styleIndexFile, `@import "${importPath}";`); | |
} | |
}); | |
} | |
var camelized = function (str) { | |
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) { | |
return letter.toUpperCase(); | |
}).replace(/\s+|-/g, ''); | |
} | |
process.argv.forEach((val, idx) => { | |
if(idx === 2) { | |
categoryInput = val; | |
camelizedNameInput = camelized(val); | |
} else if(idx === 3) { | |
pathInput = val; | |
} else if (idx === 4) { | |
nameInput = val; | |
} | |
}); | |
console.log('nameInput', nameInput); | |
console.log('pathInput', pathInput); | |
function generator() { | |
getFiles(directoryDict[categoryInput]).map(function (filename) { | |
return Promise.all([getContent(directoryDict[categoryInput], filename), filename]); | |
}) | |
.then(function (results) { | |
var promises = results.map(function(result) { | |
var targetFilename = result[1].replace("sample", nameInput) | |
var targetPath = path.join(__dirname, "../" + pathInput + "/" + nameInput); | |
var targetFilePath = path.join(__dirname, "../" + pathInput + "/" + nameInput + "/" + targetFilename); | |
var content = result[0].replace(new RegExp("sample", "g"), nameInput).replace(new RegExp("Sample", "g"), camelizedNameInput); | |
writeFile(targetPath, targetFilePath, content); | |
}) | |
return Promise.all(promises) | |
}) | |
.then(function() { | |
console.log("done generating files") | |
}) | |
.catch(function rejected(err) { | |
console.log("error:", err.stack); | |
prompt.finish(); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment