Skip to content

Commit

Permalink
Merge pull request #135 from giothevanni/feature/extend-init-command
Browse files Browse the repository at this point in the history
Feature - extend init command
  • Loading branch information
RyanHavoc committed Nov 27, 2017
2 parents f69fb54 + ae11f7e commit c5d784c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 29 deletions.
7 changes: 4 additions & 3 deletions manager/astrum-init.js
@@ -1,6 +1,5 @@
#!/usr/bin/env node
var Command = require('commander').Command,
program = require('commander'),
var program = require('commander'),
fs = require('fs-extra'),
chalk = require('chalk'),
inquirer = require('inquirer'),
Expand All @@ -10,11 +9,13 @@ program
.usage('[path]')
.description(chalk.yellow('Initilize a new pattern library.'));



/**
* Override argv[1] so that usage command is
* formatted correctly.
*/
process.argv[1] = 'patterns init';
process.argv[1] = 'astrum init';

program.parse(process.argv);

Expand Down
93 changes: 68 additions & 25 deletions manager/utils.js
@@ -1,14 +1,15 @@
var fs = require('fs-extra'),
chalk = require('chalk'),
inquirer = require('inquirer'),
dir = require('global-modules'),
mkdirp = require('mkdirp'),
isWindows = require('is-windows'),
pjson = require('../package.json');
pjson = require('../package.json'),
process = require('process'),
path = require('path');

module.exports = {

module_path: dir + '/' + pjson.name,
module_path: path.resolve(`${__dirname}/..`),
$root: process.cwd(),
$config: null,
$data: null,
Expand Down Expand Up @@ -36,34 +37,76 @@ module.exports = {
var _this = this,
error = false;

fs.exists(path, function(r) {
if (r) {
throw(new Error(chalk.red('Pattern library has already been initialized.')));
}
});

_this.saveConfig(path, function() {
mkdirp(path, function (err) {
if (err) {
console.error(chalk.red('Error: ' + err));
error = true;
}
function copyTemplateDir(){

fs.copy(_this.pathify(_this.module_path + '/_template'), path, function (err) {
_this.saveConfig(path, function() {
mkdirp(path, function (err) {
if (err) {
console.log(chalk.red('Error: ' + err));
console.error(chalk.red('Error: ' + err));
error = true;
}

_this.init();
_this.updateVersion(pjson.version);
fs.copy(_this.pathify(_this.module_path + '/_template'), path, function (err) {
if (err) {
console.log(chalk.red('Error: ' + err));
error = true;
}

return callback();
_this.init();
_this.updateVersion(pjson.version);

return callback();
});
});

return ! error;
});
}


/**
* Enable the user to initialize the library in an existing folder.
* This is for users who want to commit only project specific files in their repositories
* and have all of the astrum files get generated through a CI Server
*/
if (fs.existsSync(path) === true){

console.log(chalk.red('----------------------------------------------------------------'));
console.log(chalk.red('\u26A0 Warning: Pattern library detected in the given folder'));
console.log(chalk.red('----------------------------------------------------------------'));

inquirer.prompt(
{
type: 'confirm',
name: 'init_overwrite',
message: 'This will copy/overwrite only the project independent Astrum files (html, css & js).\nDo you want to proceed?',
default: true
}).then(function(answers){

if (answers.init_overwrite === true) {

let appTemplateDir = `${_this.module_path}/_template`;
let filePaths = [
_this.pathify('/app'),
_this.pathify('/index.html')
];

filePaths.forEach(function(filePath){
fs.copy(appTemplateDir + filePath, path + filePath, function(){

console.log(chalk.yellow(`${path}${filePath} has been copied from the template folder.`));
});
});

}
});

// Standard Use Case, initializing in an empty folder.
} else {
copyTemplateDir();
}


return ! error;
});
},

updateVersion: function(number) {
Expand All @@ -84,7 +127,7 @@ module.exports = {
fs.copy(_this.pathify(_this.module_path + '/_template/app'), _this.$config.path + '/app');
fs.copy(_this.pathify(_this.module_path + '/_template/index.html'), _this.$config.path + '/index.html');
fs.copy(_this.pathify(_this.module_path + '/_template/LICENSE.txt'), _this.$config.path + '/LICENSE.txt');

_this.updateVersion(pjson.version);

/**
Expand Down Expand Up @@ -117,7 +160,7 @@ module.exports = {

getConfig: function() {
var _this = this;

return JSON.parse(fs.readFileSync(_this.pathify(_this.$root + '/astrum-config.json')));
},

Expand Down Expand Up @@ -617,4 +660,4 @@ module.exports = {

return path;
}
};
};
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -30,7 +30,6 @@
"commander": "^2.9.0",
"cwd": "^0.10.0",
"fs-extra": "^0.28.0",
"global-modules": "^0.2.2",
"inquirer": "^1.0.2",
"is-windows": "^0.2.0",
"mkdirp": "^0.5.1",
Expand Down

0 comments on commit c5d784c

Please sign in to comment.