-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support template-path
and name
CLI options for interactive CLI
#9471
Merged
pgrzesik
merged 5 commits into
master
from
support-template-path-and-path-in-interactive-cli
May 14, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fcf22e9
refactor: Add `create-from-local-template` util
pgrzesik 1ccc4d2
feat(CLI Onboarding): Support `template-path` param
pgrzesik cc3fefb
feat(CLI Onboarding): Support `--name` CLI option
pgrzesik e602fa2
feat(CLI Onboarding): Skip "enable" question when options provided
pgrzesik f876719
feat(CLI Onboarding): Dont allow setup with options in service dir
pgrzesik File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const untildify = require('untildify'); | ||
const fse = require('fs-extra'); | ||
const { renameService } = require('./renameService'); | ||
|
||
const ServerlessError = require('../serverless-error'); | ||
|
||
module.exports = async ({ templatePath, projectDir, projectName }) => { | ||
const sourcePath = untildify(templatePath); | ||
|
||
try { | ||
await fse.copy(sourcePath, projectDir, { | ||
dereference: true, | ||
filter: async (src) => { | ||
const stats = await fse.lstat(src); | ||
return !stats.isSymbolicLink(); | ||
}, | ||
}); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
throw new ServerlessError( | ||
`Could not find a template under provider path: ${sourcePath}`, | ||
'INVALID_TEMPLATE_PATH' | ||
); | ||
} | ||
|
||
throw new ServerlessError(`Cannot copy template: ${err.message}`, 'COPY_LOCAL_TEMPLATE_ERROR'); | ||
} | ||
|
||
if (projectName) { | ||
renameService(projectName, projectDir); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const chai = require('chai'); | ||
const fsp = require('fs').promises; | ||
const { load: yamlParse } = require('js-yaml'); | ||
const createFromLocalTemplate = require('../../../../lib/utils/create-from-local-template'); | ||
const { getTmpDirPath } = require('../../../utils/fs'); | ||
|
||
const templatesPath = path.resolve(__dirname, '../../../../lib/plugins/create/templates'); | ||
|
||
chai.use(require('chai-as-promised')); | ||
|
||
const expect = chai.expect; | ||
|
||
describe('test/unit/lib/utils/create-from-local-template.test.js', () => { | ||
describe('Without `projectName` provided', () => { | ||
it('should create from template referenced locally', async () => { | ||
const tmpDirPath = path.join(getTmpDirPath(), 'some-service'); | ||
await createFromLocalTemplate({ | ||
templatePath: path.join(templatesPath, 'aws-nodejs'), | ||
projectDir: tmpDirPath, | ||
}); | ||
const stats = await fsp.lstat(path.join(tmpDirPath, 'serverless.yml')); | ||
expect(stats.isFile()).to.be.true; | ||
}); | ||
}); | ||
|
||
describe('When `templatePath` does not exist', () => { | ||
it('should result in an error', async () => { | ||
const tmpDirPath = path.join(getTmpDirPath(), 'some-service'); | ||
await expect( | ||
createFromLocalTemplate({ | ||
templatePath: path.join(templatesPath, 'nonexistent'), | ||
projectDir: tmpDirPath, | ||
}) | ||
).to.eventually.be.rejected.and.have.property('code', 'INVALID_TEMPLATE_PATH'); | ||
}); | ||
}); | ||
|
||
describe('With `projectName` provided', () => { | ||
let tmpDirPath; | ||
|
||
before(async () => { | ||
tmpDirPath = path.join(getTmpDirPath(), 'some-service'); | ||
await createFromLocalTemplate({ | ||
templatePath: path.join(templatesPath, 'fn-nodejs'), | ||
projectDir: tmpDirPath, | ||
projectName: 'testproj', | ||
}); | ||
}); | ||
|
||
it('should set service name in serverless.yml', async () => | ||
expect( | ||
yamlParse(await fsp.readFile(path.join(tmpDirPath, 'serverless.yml'))).service | ||
).to.equal('testproj')); | ||
|
||
it('should set name in package.json', async () => | ||
expect(JSON.parse(await fsp.readFile(path.join(tmpDirPath, 'package.json'))).name).to.equal( | ||
'testproj' | ||
)); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also throw here errors, if given options were passed but user is in service context (we should not silently ignore them)