Skip to content

Commit

Permalink
feat(local-cli): fetch template as tarball, extract and install it
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd authored and dmbch committed Oct 13, 2017
1 parent 6378026 commit 1982028
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 33 deletions.
123 changes: 97 additions & 26 deletions packages/local-cli/init.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,103 @@
'use strict';

var fs = require('fs');
var path = require('path');
var tar = require('tar');
var validatePackageName = require('validate-npm-package-name');
var pm = require('./lib/package-manager');

function init (appRoot, appName, options) {
var useYarn = !!(pm.isGlobalCliUsingYarn() || pm.getYarnVersionIfAvailable());

var prodDependencies = [
'hops-express',
'hops-react',
'hops-redux',
'react',
'react-dom',
'react-helmet',
'react-redux',
'react-router',
'react-router-dom',
'redux',
'redux-thunk'
];
var devDependencies = [
'hops-build',
'jest',
'jest-preset-hops',
'react-test-renderer'
];
pm.installPackages(prodDependencies, { yarn: useYarn });
pm.installPackages(devDependencies, { yarn: useYarn, dev: true });

// fetch template from github/npm and copy files into app folder
function readPackageManifest (file) {
return JSON.parse(fs.readFileSync(file).toString('utf-8'));
}

function writePackageManifest (file, manifest) {
fs.writeFileSync(file, JSON.stringify(manifest, null, 2));
}

function sortObjectKeys (input) {
var result = {};
Object.keys(input).sort().forEach(function (k) { result[k] = input[k]; });
return result;
}

function mergePackageManifest (oldManifest, newManifest) {
return Object.assign(
{},
newManifest,
oldManifest,
{
dependencies: sortObjectKeys(Object.assign(
newManifest.dependencies,
oldManifest.dependencies
))
}
);
}

function getValidatedTemplateName (name, root) {
var validationResult = validatePackageName(name);
if (
!(
validationResult.validForNewPackages ||
validationResult.validForOldPackages
)
) {
return path.resolve(root, name);
} else if (name.indexOf('hops-template') === 0) {
return name;
}
return null;
}

function init (root, appName, options) {
var appRoot = path.resolve(root, appName);
var template = getValidatedTemplateName(options.template, root);
var pathToPackageManifest = path.resolve(appRoot, 'package.json');
var oldPackageManifest = readPackageManifest(pathToPackageManifest);
var tarball = null;

if (template) {
tarball = pm.getTarball(template, options);
} else {
console.error(
'Could not find a hops-template with the specified name:',
options.template
);
process.exit(1);
}

if (tarball) {
tar.extract({
file: tarball,
strip: 1
}).then(function () {
fs.unlinkSync(tarball);
var newPackageManifest = readPackageManifest(pathToPackageManifest);
writePackageManifest(
pathToPackageManifest,
mergePackageManifest(oldPackageManifest, newPackageManifest)
);
pm.installPackages([], options);
}).catch(function (error) {
console.error(
'Error while unpacking tar archive:',
tarball
);
console.error(error);
});
} else {
console.error(
'Could not download tarball for:',
template
);
process.exit(1);
}

console.log('Hooray \\o/');
console.log('Your project has been successfully created.');
console.log(
'You should change into its directory and execute "hops" to see a list of',
'available commands.'
);
}
module.exports = init;
37 changes: 30 additions & 7 deletions packages/local-cli/lib/package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@ function installPackages (packages, options) {
command = options.installCommand.split(' ');
} else {
if (options.yarn) {
command = [
'yarn',
'add',
'--exact'
];
if (packages.length) {
command = [
'yarn',
'add',
'--exact'
];
} else {
command = [
'yarn',
'install'
];
}
if (options.dev) {
command.push('--dev');
}
} else {
command = [
'npm',
'install',
'--save-exact'
'install'
];
if (packages.length) {
command.push('--save-exact');
}
if (options.dev) {
command.push('--save-dev');
} else {
Expand Down Expand Up @@ -88,6 +97,20 @@ function installPackages (packages, options) {
}
module.exports.installPackages = installPackages;

function getTarball (name, options) {
var command = 'npm pack ' + name;
try {
if (options.verbose) {
console.log('Executing:', command);
}
return execIgnoreStdError(command, options);
} catch (error) {
console.error(error);
return null;
}
}
module.exports.getTarball = getTarball;

function isPackageInstalled (name) {
try {
require.resolve(name);
Expand Down
2 changes: 2 additions & 0 deletions packages/local-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"url": "https://github.com/xing/hops.git"
},
"dependencies": {
"tar": "^4.0.1",
"validate-npm-package-name": "^3.0.0",
"yargs": "^9.0.1"
},
"optionalDependencies": {
Expand Down

0 comments on commit 1982028

Please sign in to comment.