-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(folder structure): create and automate folder and directory structure
[Delivers #7]
- Loading branch information
Showing
27 changed files
with
170 additions
and
21 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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
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,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; |
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
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 |
---|---|---|
|
@@ -192,4 +192,4 @@ describe('Generate', () => { | |
}); | ||
}); | ||
|
||
}); | ||
}); |
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,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); | ||
}); | ||
}); | ||
}); |