-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(local-cli): fetch template as tarball, extract and install it
- Loading branch information
1 parent
6378026
commit 1982028
Showing
3 changed files
with
129 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters