Skip to content

Commit

Permalink
ft(folder structure): create and automate folder and directory structure
Browse files Browse the repository at this point in the history
[Delivers #7]
  • Loading branch information
iAmao committed Jun 11, 2017
1 parent 24b95ae commit fd3bc3d
Show file tree
Hide file tree
Showing 27 changed files with 170 additions and 21 deletions.
11 changes: 0 additions & 11 deletions example/.babelrc

This file was deleted.

Empty file added example/dist/css/style.css
Empty file.
Empty file added example/dist/images/images.txt
Empty file.
Empty file added example/dist/index.html
Empty file.
Empty file added example/dist/js/script.js
Empty file.
Empty file added example/dist/vendor/vendor.txt
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added example/src/index.jsx
Empty file.
Empty file.
Empty file added example/src/store.js
Empty file.
Empty file added example/src/utils/helper.js
Empty file.
Empty file.
Empty file.
Empty file added example/test/e2e/e2e.txt
Empty file.
Empty file added example/test/helper.js
Empty file.
Empty file.
9 changes: 6 additions & 3 deletions lib/Inquire.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class Inquire {
this.questions = [];
this.validate = {
notEmpty(value) {
return (value && value.length && value.length > 0) ? true : 'Please enter a value';
return (value && value.length && value.length > 0) ?
true : 'Please enter a value';
},
yesNo(value) {
return (value && ['y', 'n', 'yes', 'no'].indexOf(value.toLowerCase()) !== -1) ?
return (value &&
['y', 'n', 'yes', 'no'].indexOf(value.toLowerCase()) !== -1) ?
true : 'Invalid input to yes/no question';
}
};
Expand Down Expand Up @@ -45,7 +47,8 @@ class Inquire {

/**
* Combine and ask all the questions in the questions property
* @param {Function} callback - function to run after user is done with questions
* @param {Function} callback - function to run after user is done
* with questions
* @returns {Void} - returns nothing
* @memberOf Inquire
*/
Expand Down
21 changes: 16 additions & 5 deletions lib/ReactRaise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const Inquire = require('./Inquire');
const Generate = require('./Generate');
const Structure = require('./Structure');

/**
* ReactRaise class
* @class ReactRaise
Expand Down Expand Up @@ -43,19 +45,27 @@ class ReactRaise {

/**
* initialize command and ask setup questions
* @param {Function} callback - function to execute after answers has been given
* @param {Function} callback - function to execute after answers
* has been given
* @returns {Void} returns nothing
* @memberOf ReactRaise
*/
init(callback) {
const g = new Generate();
const configGenerate = new Generate();
const fileStructure = new Structure();
const startInquire = new Inquire();
startInquire.question('description', 'input', 'Can you describe your app[optional]');
startInquire.question(
'description',
'input',
'Can you describe your app[optional]');
startInquire.question(
'main',
'input',
'What is the main entry file of your app[Default: index.js]');
startInquire.question('author', 'input', 'What is your name[optional]');
startInquire.question(
'author',
'input',
'What is your name[optional]');
startInquire.question(
'license',
'input',
Expand All @@ -73,7 +83,8 @@ class ReactRaise {
if (callback) {
callback();
}
g.all(answers);
configGenerate.all(answers);
fileStructure.build();
});
}
}
Expand Down
71 changes: 71 additions & 0 deletions lib/Structure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const structureSchema = require('../sample/folder-schema');
const baseDir = require('./baseDir');

/**
* Define and build the application folder structure
* @class Structure
*/
class Structure {

/**
* Creates an instance of Structure.
* @param {string} extend - path to add to current working drectory
* @param {any} schema - folder structure schema
* @memberOf Structure
*/
constructor(extend = '', schema) {
this.schema = schema || structureSchema;
this.filepath = `${baseDir.getCurrentWorkingDir()}/${extend}`;
}

/**
* Move through the folder schema and create folders and files
* @param {Array} dir - current directory to iterate through
* @param {Array} pathname - contains each individual parent
* folders name
* @returns {Void} returns nothing
* @memberOf Structure
*/
traverse(dir, pathname) {
if (!pathname) {
pathname = [];
}
dir.forEach((root, index, parent) => {
if (typeof root === 'object') {
pathname.push(Object.keys(root)[0]);
mkdirp.sync(
path.join(this.filepath, `/example/${pathname.join('/')}`));
this.traverse(root[Object.keys(root)[0]], pathname);
} else {
fs.writeFile(
path.join(
this.filepath,
`/example/${pathname.join('/')}/${root}`),
'');
if (index === parent.length - 1) {
pathname.pop();
}
}
});
}

/**
* Build the folder structure based off the provided schema
* @returns {Promise} resolves promise when directories have been created
* @memberOf Structure
*/
build() {
return new Promise((resolve) => {
this.traverse(this.schema);
if (baseDir.directoryExists(
path.join(this.filepath, '/example/test/helper.js'))) {
resolve(this.schema);
}
});
}
}

module.exports = Structure;
8 changes: 7 additions & 1 deletion lib/baseDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ module.exports = {
return process.cwd();
},

/**
* check if a file or directory exists
* @param {String} filePath - file path to check
* @returns {Boolean} if exists return true else return false
*/
directoryExists(filePath) {
try {
return fs.statSync(filePath).isDirectory() || fs.statSync(filePath).isFile();
return fs.statSync(filePath).isDirectory()
|| fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions sample/folder-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@ module.exports = [
{ images: ['images.txt'] },
'index.html'
]
},
{
test: [
{ actions: ['actionTypes.spec.js'] },
{ reducers: ['root.reducer.spec.js'] },
{ components: ['Home.component.spec.js'] },
{ e2e: ['e2e.txt'] },
'helper.js'
]
}
];
2 changes: 1 addition & 1 deletion test/lib/Generate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ describe('Generate', () => {
});
});

});
});
60 changes: 60 additions & 0 deletions test/lib/Structure.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;

const Structure = require('../../lib/Structure');
const baseDir = require('../../lib/baseDir');
const scheme = require('../../sample/folder-schema');

describe('Structure', () => {
let structure;
const folderScheme = [
{ new: ['text.txt'] },
{ old: [{ red: ['red.txt'] }] }
];

beforeEach((done) => {
structure = new Structure('test/lib');
mkdirp.sync(path.join(__dirname, `example`));
done();
});

afterEach((done) => {
const filepath = path.join(__dirname, 'example');
exec('rm -r ' + filepath, (err, stdout, stderr) => {
return done(err);
});
});

it('Should create an instance of Structure class', () => {
structure = new Structure();
expect(structure).to.be.instanceOf(Structure);
});

describe('traverse', () => {
it('should traverse a folder schema and create directories and files', (done) => {
structure.traverse(folderScheme);
setTimeout(() => {
expect(baseDir.directoryExists(
path.join(__dirname, '/example/new/text.txt'))).to.eql(true);
expect(baseDir.directoryExists(
path.join(__dirname, '/example/old/red/red.txt'))).to.eql(true);
done();
}, 900);
});
});

describe('build', () => {
it('should build the file directory from the supplied schema', (done) => {
structure.build(scheme);
setTimeout(() => {
expect(baseDir.directoryExists(
path.join(__dirname, '/example/test/helper.js'))).to.eql(true);
expect(baseDir.directoryExists(
path.join(__dirname, '/example/src/store.js'))).to.eql(true);
done();
}, 900);
});
});
});

0 comments on commit fd3bc3d

Please sign in to comment.