Skip to content

Commit

Permalink
cli init now allow for template to support initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Balestra committed Apr 3, 2017
1 parent 225db00 commit ed141e5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 20 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"body-parser": "^1.16.0",
"clean-css": "3.4.18",
"colors": "1.1.2",
"cross-spawn": "^5.1.0",
"dependency-graph": "0.5.0",
"detective": "4.3.1",
"errorhandler": "^1.5.0",
Expand All @@ -78,6 +79,7 @@
"oc-template-handlebars": "4.0.0",
"oc-template-jade": "4.0.0",
"opn": "4.0.2",
"ora": "^1.2.0",
"parse-author": "1.0.0",
"read": "1.0.7",
"require-package-name": "2.0.1",
Expand Down
108 changes: 108 additions & 0 deletions src/cli/domain/init-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'use strict';

var fs = require('fs-extra');
var ora = require('ora');
var spawn = require('cross-spawn');
var path = require('path');
var colors = require('colors/safe');
var execSync = require('child_process').execSync;

function shouldUseYarn() {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}

var cli = shouldUseYarn() ? 'yarn' : 'npm';

module.exports = function (componentName, templateType, callback) {
var templatePath = path.resolve(process.cwd(), templateType);
var componentPath = path.join(process.cwd(), componentName);

// Step 1 - Create componentName directory
var step1 = ora(`Creating directory...`).start();
fs.ensureDirSync(componentName);
step1.succeed(`Created directory "${componentName}"`);

// Initialize npm
var initProc = spawn.sync(cli, ['init', '--yes'], { silent: true, cwd: componentName });

initProc.on('error', function (error) {
return callback('template type not valid');
});

// Step 2 - Install template module
var local = /^\.\/|^\//.test(templateType);
var args = {
npm: [
'install',
'--save',
'--save-exact',
local
? templatePath
: templateType
],
yarn: [
'add',
'--exact',
local
? templatePath
: templateType
]
};
var step2 = ora(
`Installing ${templateType} from ${local ? 'local' : 'npm'}...`
).start();


var installProc = spawn(cli, args[cli], {silent: true, cwd: componentName});

installProc.on('error', function (error) {
return callback('template type not valid');
});

installProc.on('close', function (code) {
if (code !== 0) {
return callback('template type not valid');
}

step2.succeed(
`Installed ${templateType} from ${local ? 'local' : 'npm'}`
);

// Step 3 - Copy boilerplates from the template module
try {
var step3 = ora(`Generating boilerplate files...`).start();

var baseComponentPath = path.join(
componentPath,
'node_modules',
templateType,
'component'
);

var baseComponentFiles = path.join(baseComponentPath, 'src');

fs.copySync(baseComponentFiles, componentPath);

var packageContent = require(baseComponentPath + '/package');
var initializedPackage = require(componentPath + '/package');

packageContent.name = componentName;
packageContent.dependencies = initializedPackage.dependencies;

fs.writeJsonSync(componentPath + '/package.json', packageContent);
step3.succeed(`Boilerplate files generated at ${componentPath}`);
return callback(null, { ok: true });
} catch (e) {
console.error(colors.red(
`Boilerplate generation failed, please report to ${templateType} owner`
)
);
return callback('An error happened when initialising the component');
}
});
};
45 changes: 25 additions & 20 deletions src/cli/domain/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var getLocalNpmModules = require('./get-local-npm-modules');
var packageComponents = require('./package-components');
var mock = require('./mock');
var validator = require('../../registry/domain/validators');
var initTemplate = require('./init-template');


module.exports = function(dependencies){
var logger = dependencies.logger;
Expand Down Expand Up @@ -39,30 +41,33 @@ module.exports = function(dependencies){
return callback('name not valid');
}

if(!validator.validateTemplateType(templateType)){
return callback('template type not valid');
}

try {

var pathDir = '../../components/base-component-' + templateType,
baseComponentDir = path.resolve(__dirname, pathDir),
npmIgnorePath = path.resolve(__dirname, pathDir + '/.npmignore');
// LEGACY TEMPLATES
if(validator.validateTemplateType(templateType)){
try {
var pathDir = '../../components/base-component-' + templateType,
baseComponentDir = path.resolve(__dirname, pathDir),
npmIgnorePath = path.resolve(__dirname, pathDir + '/.npmignore');

fs.ensureDirSync(componentName);
fs.copySync(baseComponentDir, componentName);
fs.copySync(npmIgnorePath, componentName + '/.gitignore');
fs.ensureDirSync(componentName);
fs.copySync(baseComponentDir, componentName);
fs.copySync(npmIgnorePath, componentName + '/.gitignore');

var componentPath = path.resolve(componentName, 'package.json'),
component = _.extend(fs.readJsonSync(componentPath), {
name: componentName
});
var componentPath = path.resolve(componentName, 'package.json'),
component = _.extend(fs.readJsonSync(componentPath), {
name: componentName
});

fs.outputJsonSync(componentPath, component);
fs.outputJsonSync(componentPath, component);

return callback(null, { ok: true });
} catch(e){
return callback(e);
return callback(null, { ok: true });
} catch(e){
return callback(e);
}
}
try {
initTemplate(componentName, templateType, callback);
} catch (e) {
return callback('template type not valid');
}
},
mock: mock(),
Expand Down

0 comments on commit ed141e5

Please sign in to comment.