Skip to content

Commit

Permalink
added functionality to specify custom project name for projectInstall
Browse files Browse the repository at this point in the history
  • Loading branch information
eahefnawy committed Apr 7, 2016
1 parent 58898e7 commit 542ca15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/actions/ProjectInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ module.exports = function(S) {

// Fill in project attributes
_this.project = S.getProject();
_this.project.name = _this.project.name ? _this.project.name : _this.evt.options.name;
_this.project.name = _this.evt.options.name ? _this.evt.options.name : _this.project.name;

// Add default project variables
_this.project.addVariables({
Expand Down
27 changes: 17 additions & 10 deletions lib/actions/ProjectInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ module.exports = function(S) {
option: 'local',
shortcut: 'l',
description: 'A local path to the project to install instead of NPM'
}, {
option: 'name',
shortcut: 'n',
description: 'Optional - Name for your installed project'
}
],
parameters: [
Expand Down Expand Up @@ -94,10 +98,14 @@ module.exports = function(S) {
return BbPromise.reject(new SError(`Please enter the name of the project you wish to install, like: serverless install project <projectname>`));
}

if (SUtils.dirExistsSync(path.join(process.cwd(), _this.evt.options.project))) {
return BbPromise.reject(new SError(`Folder ${_this.evt.options.project} already exists in the current directory`));
_this.evt.options.name = _this.evt.options.name || _this.evt.options.project;

if (SUtils.dirExistsSync(path.join(process.cwd(), _this.evt.options.name))) {
return BbPromise.reject(new SError(`Folder ${_this.evt.options.name} already exists in the current directory`));
}



/**
* Control Flow
*/
Expand Down Expand Up @@ -154,17 +162,16 @@ module.exports = function(S) {
*/

_installNpmProject() {
const projName = this.evt.options.project
const tmpDir = path.join(os.tmpdir(), `__sls_project_install_${projName}_${Date.now()}`);
const tmpDir = path.join(os.tmpdir(), `__sls_project_install_${this.evt.options.project}_${Date.now()}`);
const userCwd = process.cwd();

// create temp package.json for npm install
return fse.mkdirsAsync(tmpDir)
.then(() => process.chdir(tmpDir))
.then(() => fse.copyAsync(S.getServerlessPath('templates', 'nodejs', 'package.json'), path.join(tmpDir, 'package.json')))
.then(() => BbPromise.fromCallback(cb => exec('npm install ' + projName, cb)))
.then(() => fse.moveAsync(path.join(tmpDir, 'node_modules', projName), path.join(userCwd, projName)))
.then(() => fse.copyAsync(path.join(tmpDir, 'node_modules'), path.join(userCwd, projName, 'node_modules')))
.then(() => BbPromise.fromCallback(cb => exec('npm install ' + this.evt.options.project, cb)))
.then(() => fse.moveAsync(path.join(tmpDir, 'node_modules', this.evt.options.project), path.join(userCwd, this.evt.options.name)))
.then(() => fse.copyAsync(path.join(tmpDir, 'node_modules'), path.join(userCwd, this.evt.options.name, 'node_modules')))
.then(() => process.chdir(userCwd))
.then(() => fse.removeAsync(tmpDir));
}
Expand All @@ -175,7 +182,7 @@ module.exports = function(S) {

_installLocalProject() {
const pathToProjectTemplate = path.resolve(process.cwd(), this.evt.options.local),
newFolderName = this.evt.options.project,
newFolderName = this.evt.options.name,
pathToNewProject = path.join(process.cwd(), newFolderName);

return fse.copyAsync(pathToProjectTemplate, pathToNewProject);
Expand All @@ -192,15 +199,15 @@ module.exports = function(S) {
return BbPromise.try(function(){
// Update Global Serverless Instance
if( !S.hasProject() ) {
let projectPath = path.resolve(path.join(path.dirname('.'), _this.evt.options.project));
let projectPath = path.resolve(path.join(path.dirname('.'), _this.evt.options.name));
S.updateConfig({ projectPath: projectPath});
return S.init();
}
})
.then(function() {
return S.actions.projectInit({
options: {
name: _this.evt.options.project,
name: _this.evt.options.name,
stage: _this.evt.options.stage,
region: _this.evt.options.region,
profile: _this.evt.options.profile,
Expand Down

0 comments on commit 542ca15

Please sign in to comment.