Skip to content

Commit

Permalink
feat: stanza generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Chun-Yang committed Jul 27, 2016
1 parent deeee2f commit 9a8d5a5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/stanza-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const action = (name) => {
}

create(name);
}
};

program
.arguments('<name>')
Expand Down
15 changes: 15 additions & 0 deletions bin/stanza-generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env node
/* eslint-disable no-console */

const program = require('commander');
const generate = require('../lib/generate');

program
.arguments('<name>')
.action(generate);

program.on('--help', () => {
console.log('Create a meteor project with react and redux configured.');
});

program.parse(process.argv);
4 changes: 4 additions & 0 deletions bin/stanza.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ program
.command('update', 'Update a meteor project with design.zip from webflow.')
.alias('u');

program
.command('generate', 'Generate a custom file and update custom/index.js.')
.alias('g');

program.on('--help', () => {
console.log(' Examples:');
console.log('');
Expand Down
25 changes: 25 additions & 0 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const _ = require('lodash');
const path = require('path');
const rt = require('reacterminator');
const { ls } = require('shelljs');

function getMeteorRoot() {
let currentPath = path.resolve('.');
do {
const paths = ls('-A', currentPath);
const isMeteorRoot =
_.includes(paths, '.meteor') &&
_.includes(paths, 'package.json');

if (isMeteorRoot) {
return currentPath;
}

currentPath = path.resolve(currentPath, '..');
}
while (currentPath !== '/');

throw new Error('Ops, can not find your meteor project root directory.');
}

module.exports = (rawPath) => rt.generate(rawPath, `${getMeteorRoot()}/client/imports`);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"commander": "git://github.com/tj/commander.js.git#c6236d9504b60d9a2e6aa7fc3ce17a12f48f4a3e",
"glob": "^7.0.3",
"lodash": "^4.6.1",
"reacterminator": "^0.10.12",
"reacterminator": "^0.11.0",
"shelljs": "^0.6.0"
}
}
22 changes: 22 additions & 0 deletions test/integration/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-env mocha */
const fs = require('fs');
const { assert } = require('chai');
const path = require('path');
const generate = require('../../lib/generate');
const { cd, rm, mkdir, touch } = require('shelljs');

describe('generate', () => {
it('generate a component', () => {
const examplesPath = path.resolve(__dirname, '../../examples');
cd(examplesPath);
rm('-rf', 'example');
mkdir('-p', './example/.meteor/');
touch('./example/package.json');
cd('example');

generate('components/components/Signup');

assert(fs.statSync('client/imports/custom/index.js').isFile());
assert(fs.statSync('client/imports/custom/components/Signup.jsx').isFile());
});
});

0 comments on commit 9a8d5a5

Please sign in to comment.