Skip to content

Commit

Permalink
using devDependencies for template
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Balestra committed May 9, 2017
1 parent b8f8850 commit 960377c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/cli/domain/init-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,30 @@ const initPackage = require('./initPackage');
const utils = require('./utils');

module.exports = function (componentName, templateType, options, callback) {
const local = /^\.+\/|^\//.test(templateType);
const packageName= utils.getPackageName(templateType);
const templatePath = path.resolve('node_modules', packageName);
const config = {
cli: options.cli || 'npm',
componentName,
componentPath: path.join(process.cwd(), componentName ),
componentPath: path.join(process.cwd(), componentName),
packageName,
templatePath,
templateType,
packageName: utils.getPackageName(templateType),
logger: options.logger || console,
callback,
local: /^\.\/|^\//.test(templateType)
local
};

createComponentDir(config);

try {
// If template available in the dev registry, generate boilerplate out of its blueprint
const templatePath = path.resolve('node_modules', config.packageName);
require(templatePath);
return blueprint(_.extend({}, config, { templatePath }));
return blueprint(config);
} catch (e) {
// Otherwise install the template
// Otherwise, first install the template then generate boilerplate files.
initPackage(config);
const templatePath = path.resolve(process.cwd(), config.templateType);
return installTemplate(_.extend({}, config, { templatePath }), blueprint);
return installTemplate(config, blueprint);
}
};
34 changes: 27 additions & 7 deletions src/cli/domain/init-template/installTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@
const Spinner = require('cli-spinner').Spinner;
const spawn = require('cross-spawn');
const colors = require('colors/safe');
const path = require('path');

module.exports = function installTemplate(config, callback) {
module.exports = function installTemplate(config, blueprint) {
const templateType = config.templateType;
const cli = config.cli;
const componentPath = config.componentPath;
const local = config.local;
const packageName = config.packageName;
const logger = config.logger;
const callback = config.callback;
const installPath = path.resolve(componentPath, '../');

const installing = new Spinner(`Installing ${packageName} from ${local ? 'local' : 'npm'}...`);
const installing = new Spinner(`🚚 Installing ${packageName} from ${local ? 'local' : 'npm'}...`);
installing.start();

const args = {
npm: [
'install',
'--save',
'--save-exact',
templateType
local ? path.resolve(process.cwd(), templateType) : templateType,
]
};

const installProc = spawn(cli, args[cli], {silent: true, cwd: componentPath});
const installProc = spawn(cli, args[cli], {silent: true, cwd: installPath});

installProc.on('error', () => callback('template type not valid'));
installProc.on('close', code => {
Expand All @@ -35,6 +38,23 @@ module.exports = function installTemplate(config, callback) {
logger.log(
`${colors.green('✔')} Installed ${packageName} from ${local ? templateType : 'npm'}`
);
return callback(config);

// install devDependencies
const installedTemplatePath = path.resolve(installPath, 'node_modules', templateType)
logger.log(
`🚚 Installing required devDependencies`
);

const installDevDeps = spawn(cli, ['install'], {stdio: 'inherit', silent: true, cwd: installedTemplatePath});

installDevDeps.on('close', code => {
if (code !== 0) {
return callback('template type not valid');
}
logger.log(
`${colors.green('✔')} Installed ${packageName}'s devDependencies`
);
return blueprint(config);
})
});
};
};

0 comments on commit 960377c

Please sign in to comment.