Skip to content
This repository was archived by the owner on Feb 28, 2020. It is now read-only.

feat: --init option to generate a default application #352

Merged
merged 2 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ module.exports = Generator.extend({
type: String
})

this.option('init', {
type: Boolean,
desc: 'Generate basic default scaffold without prompting user for input.',
defaults: false
})

this.option('skip-build', {
type: Boolean,
desc: 'Skip building the generated application',
Expand All @@ -59,7 +65,19 @@ module.exports = Generator.extend({
ensureNotInProject: actions.ensureNotInProject,

initSpec: function () {
if (this.options.spec) {
if (this.options.init) {
// User passed the --init flag, so no prompts, just generate basic default scaffold
this.destinationSet = true
this.skipPrompting = true
this.appPattern = 'Basic'
this.spec = {
appType: 'scaffold',
appName: this.appname,
docker: true,
metrics: true,
services: {}
}
} else if (this.options.spec) {
try {
this.spec = JSON.parse(this.options.spec)
this.skipPrompting = true
Expand All @@ -72,6 +90,9 @@ module.exports = Generator.extend({
initAppName: function () {
if (this.skipPrompting) return

// save the initial directory for use by the fromSwagger processing.
this.initialWorkingDir = process.cwd()

this.appname = null // Discard yeoman default appname
this.skipPromptingAppName = false
if (this.options.name) {
Expand All @@ -90,9 +111,6 @@ module.exports = Generator.extend({
}
}

// save the initial directory for use by the fromSwagger processing.
this.initialWorkingDir = process.cwd()

if (this.appname === null) {
// Fall back to name of current working directory
// Normalize if it contains special characters
Expand Down Expand Up @@ -146,7 +164,6 @@ module.exports = Generator.extend({
if (this.appname === path.basename(this.destinationRoot())) {
// When the project name is the same as the current directory,
// we are assuming the user has already created the project dir
this.log('working directory is %s', path.basename(this.destinationRoot()))
this.destinationSet = true
return
}
Expand Down
23 changes: 23 additions & 0 deletions test/integration/app/prompted_nobuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,29 @@ describe('Integration tests (prompt no build) for swiftserver:app', function ()
commonTest.itCreatedMetricsFilesWithExpectedContent()
commonTest.autoscaling.itCreatedServiceFilesWithExpectedContent('myAutoscalingService')
})

describe('with --init flag set', function () {
var runContext
var appName = 'fishcakes'

before(function () {
runContext = helpers.run(appGeneratorPath)
.withOptions({ skipBuild: true, init: true })
.inTmpDir(function (tmpDir) {
this.inDir(path.join(tmpDir, appName))
})

return runContext.toPromise()
})

after(function () {
runContext.cleanTestDirectory()
})

commonTest.itCreatedCommonFiles(appName)
commonTest.itCreatedMetricsFilesWithExpectedContent()
commonTest.itCreatedDockerFilesWithExpectedContent(appName)
})
})

describe('crud', function () {
Expand Down
27 changes: 27 additions & 0 deletions test/unit/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,33 @@ describe('Unit tests for swiftserver:app', function () {
}))
})

describe('--init flag specified', function () {
var runContext
var appName

before(function () {
runContext = helpers.run(appGeneratorPath)
.withGenerators(dependentGenerators) // Stub subgenerators
.withOptions({ testmode: true, init: true }) // Workaround to stub subgenerators
.inTmpDir(function (tmpDir) {
appName = path.basename(tmpDir)
})
return runContext.toPromise()
})

after(function () {
runContext.cleanTestDirectory()
})

itCreatedSpecWithServicesAndCapabilities(() => ({
runContext: runContext,
appType: 'scaffold',
appName: appName,
capabilities: [ 'docker', 'metrics' ],
services: {}
}))
})

describe('spec option', function () {
describe('valid', function () {
var runContext
Expand Down