Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions bin/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,27 @@ program
} else {
const spinner = ora("creating directory structure").start();

init.initialize(projectname, gitrepository, options.eslint,
init.initialize(
projectname,
gitrepository,
options.eslint,
function initProject(res) {
if (res) {
setTimeout(() => {
spinner.text = "application created successfully";
spinner.succeed();
console.log(
`\t$ cd ${projectname}\n \t$ npm install \n \tHappy hacking ♥`
);
}, 1000);

} else {
setTimeout(() => {
spinner.text = "something went wrong!";
spinner.fail();
}, 1000);
if (res) {
setTimeout(() => {
spinner.text = "application created successfully";
spinner.succeed();
console.log(
`\t$ cd ${projectname}\n \t$ npm install \n \tHappy hacking ♥`
);
}, 1000);
} else {
setTimeout(() => {
spinner.text = "something went wrong!";
spinner.fail();
}, 1000);
}
}
});
);
}
})
.on("--help", function() {
Expand All @@ -66,7 +69,6 @@ program
* command changing .reactclirc file
*/
program.command("config [key] [value]").alias("c").action(function(key, value) {
const spinner = ora("configuring .reactclirc").start();
configRc(key, value, function config(err, res) {
if (err) {
setTimeout(() => {
Expand All @@ -83,7 +85,7 @@ program.command("config [key] [value]").alias("c").action(function(key, value) {
});

/**
* command view directory structure - components/tests
* command view directory structure - tests
*/
program
.command("view")
Expand Down Expand Up @@ -325,5 +327,4 @@ program
/**
* parse commander object
*/

program.parse(process.argv);
5 changes: 2 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ const path = require("path");
* @param {string} value - value to be changed
* @param {function} cb - callback
*/

function configReactCliRc(key, value, cb) {
fs.readFile(path.join(process.cwd(), ".reactclirc"), (err, buffer) => {
fs.readFile(path.join(process.cwd()), (err, buffer) => {
let jsonContent = JSON.parse(buffer.toString());
jsonContent[key] = value;
fs.writeFile(
path.join(process.cwd(), ".reactclirc"),
path.join(process.cwd()),
JSON.stringify(jsonContent, null, 2),
"utf-8",
err => {
Expand Down
48 changes: 21 additions & 27 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs.extra');
const path = require('path');
const getSourceDirectory = require('./source');

/**
* create component file with fs
Expand All @@ -14,7 +13,7 @@ generate.prototype.createComponentFile = function(re, componentName, answersInne
const _modFile = file.replace(re, componentName);
const __modFile = this.createModFile(_modFile, answersInner, componentName);
console.log(__modFile);
fs.writeFileSync(path.join(process.cwd(), 'src/components', module, componentName + '.react.js'), __modFile, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), 'src/components', module, componentName + '.js'), __modFile, 'utf-8');
}

/**
Expand All @@ -25,36 +24,35 @@ generate.prototype.createComponentFile = function(re, componentName, answersInne
* @param {string} answersInner - options provided when creating component
* @param {function} cb - callback for status return
*/

generate.prototype.generateComponent = function(type, module, componentName, answers, answersInner, cb) {
try {
if(componentName !== undefined) {
const exists = fs.accessSync(path.join(process.cwd(), getSourceDirectory(), 'components', module, componentName), fs.F_OK);
cb(`${componentName}.react.js already exists`);
const exists = fs.accessSync(path.join(process.cwd(), module, componentName), fs.F_OK);
cb(`${componentName}.js already exists`);
return;
}
else {
const exists = fs.accessSync(path.join(process.cwd(), getSourceDirectory(), 'components', module + '.react.js'), fs.F_OK);
cb(`${componentName}.react.js already exists`);
const exists = fs.accessSync(path.join(process.cwd(), module + '.js'), fs.F_OK);
cb(`${componentName}.js already exists`);
return;
}
}
catch(e) {
const file = fs.readFileSync(path.join(__dirname, '..', `templates/components/${type}-component.react.js`), 'utf-8');
const file = fs.readFileSync(path.join(__dirname, '..', `templates/components/${type}-component.js`), 'utf-8');
const re = /<name>/gi;
if(componentName !== undefined) {
try {
if(answers.propTypes === 'no') {
fs.mkdirSync(path.join(process.cwd(), getSourceDirectory(), 'components', module), 0755);
fs.mkdirSync(path.join(process.cwd(), module), 0755);
const _modFile = file.replace(re, componentName);
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), 'components', module, componentName + '.react.js'), _modFile, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), module, componentName + '.js'), _modFile, 'utf-8');
cb(true);
}
else {
fs.mkdirSync(path.join(process.cwd(), getSourceDirectory(), 'components', module), 0755);
fs.mkdirSync(path.join(process.cwd(), module), 0755);
const _modFile = file.replace(re, componentName);
const __modFile = this.createModFile(_modFile, answersInner, componentName);
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), 'components', module, componentName + '.react.js'), __modFile, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), module, componentName + '.js'), __modFile, 'utf-8');
cb(true);
}
}
Expand All @@ -65,7 +63,7 @@ generate.prototype.generateComponent = function(type, module, componentName, ans
else {
const _modFile = file.replace(re, componentName);
const __modFile = this.createModFile(_modFile, answersInner, componentName);
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), 'components', module, componentName + '.react.js'), __modFile, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), module, componentName + '.js'), __modFile, 'utf-8');
cb(true);
}
}
Expand All @@ -74,7 +72,7 @@ generate.prototype.generateComponent = function(type, module, componentName, ans
try {
const _modFile = file.replace(re, module);
const __modFile = this.createModFile(_modFile, answersInner, module);
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), 'components', module + '.react.js'), __modFile, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), module + '.js'), __modFile, 'utf-8');
cb(true);
}
catch(ex) {
Expand All @@ -90,7 +88,6 @@ generate.prototype.generateComponent = function(type, module, componentName, ans
* @param {string} answersInner - options provided when creating component
* @param {string} componentName - component name
*/

generate.prototype.createModFile = function(_modFile, answersInner, componentName) {
return _modFile.slice(0, Number(_modFile.indexOf('export'))-1) + this.generatePropTypesSet(answersInner, componentName) + _modFile.slice(Number(_modFile.indexOf('export'))-1);
}
Expand All @@ -100,12 +97,11 @@ generate.prototype.createModFile = function(_modFile, answersInner, componentNam
* @param {string} answersInner
* @param {string} componentName - component name
*/

generate.prototype.generatePropTypesSet = function(answersInner, componentName) {
let __propTypes = {};

for(let propName in answersInner) {
__propTypes[propName] = `React.PropTypes.${answersInner[propName]}`;
__propTypes[propName] = `PropTypes.${answersInner[propName]}`;
}

const _answersInner = JSON.stringify(__propTypes, null, 2);
Expand All @@ -123,7 +119,6 @@ generate.prototype.generatePropTypesSet = function(answersInner, componentName)
* @param {string} answersInner - options provided when creating component
* @param {function} cb - callback for status return
*/

generate.prototype.createComponent = function(module, componentName, answers, answersInner, cb) {
this.generateComponent(answers.componentType, module, componentName, answers, answersInner, cb);
}
Expand All @@ -134,41 +129,40 @@ generate.prototype.createComponent = function(module, componentName, answers, an
* @param {string} testName - test file name
* @param {function} cb - callback for status return
*/

generate.prototype.createTest = function(module, testName, cb) {
try {
if(testName !== undefined) {
const existsModule = fs.accessSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', module, testName), fs.F_OK);
const existsModule = fs.accessSync(path.join(process.cwd(), 'tests', module, testName), fs.F_OK);
cb(`${componentName}.js test file already exists`);
return;
}
else {
const exists = fs.accessSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', module + '.js'), fs.F_OK);
const exists = fs.accessSync(path.join(process.cwd(), 'tests', module + '.js'), fs.F_OK);
cb(`${componentName}.js test file already exists`);
return;
}
}
catch(e) {
const file = fs.readFileSync(path.join(__dirname, '..', `templates/src/__tests__/app.js`), 'utf-8');
const file = fs.readFileSync(path.join(__dirname, '..', `templates/tests/App.test.js`), 'utf-8');
if(testName !== undefined) {
try {
fs.mkdirSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', module), 0755);
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', module, testName + '.js'), file, 'utf-8');
fs.mkdirSync(path.join(process.cwd(), 'tests', module), 0755);
fs.writeFileSync(path.join(process.cwd(), 'tests', module, testName + '.js'), file, 'utf-8');
cb(true);
}
catch(ex) {
if(ex.syscall == 'open') {
cb('module doesn\'t exist');
}
else {
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', module, testName + '.js'), file, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), 'tests', module, testName + '.js'), file, 'utf-8');
cb(true);
}
}
}
else {
try {
fs.writeFileSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', module + '.js'), file, 'utf-8');
fs.writeFileSync(path.join(process.cwd(), 'tests', module + '.js'), file, 'utf-8');
cb(true);
}
catch(ex) {
Expand All @@ -183,4 +177,4 @@ generate.prototype.createTest = function(module, testName, cb) {
}
}

module.exports = generate;
module.exports = generate;
Loading