Skip to content
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

fix: write default app config on app init #353

Merged
merged 6 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const path = require('path')
const fs = require('fs-extra')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:init', { provider: 'debug' })
const { flags } = require('@oclif/command')
const { loadAndValidateConfigFile, importConfigJson } = require('../../lib/import')
const { loadAndValidateConfigFile, importConfigJson, writeDefaultAppConfig } = require('../../lib/import')
const { getCliInfo } = require('../../lib/app-helper')
const chalk = require('chalk')
const { servicesToGeneratorInput } = require('../../lib/app-helper')
Expand Down Expand Up @@ -112,6 +112,9 @@ class InitCommand extends BaseCommand {
}
}

// write default app config to .aio file
writeDefaultAppConfig(process.cwd(), { interactive, merge })

// finalize configuration data
this.log('✔ App initialization finished!')
return res
Expand Down
25 changes: 25 additions & 0 deletions src/lib/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ function loadAndValidateConfigFile (fileOrBuffer) {
return res
}

/**
* Writes default app config to .aio file
*
* @param {string} parentDir the parent folder to write the .aio file to
* @param {object} [flags] flags for file writing
* @param {boolean} [flags.overwrite=false] set to true to overwrite the existing .env file
* @param {boolean} [flags.merge=false] set to true to merge in the existing .env file (takes precedence over overwrite)
* @param {boolean} [flags.interactive=false] set to true to prompt the user for file overwrite
* @returns {Promise} promise from writeFile call
*/
function writeDefaultAppConfig (parentDir, flags) {
// write app config to .aio file
const appConfig = {
shazron marked this conversation as resolved.
Show resolved Hide resolved
app: {
actions: 'actions',
dist: 'dist',
web: 'web-src'
}
}
return writeAio(appConfig, parentDir, flags)
}

/**
* Pretty prints the json object as a string.
* Delimited by 2 spaces.
Expand Down Expand Up @@ -425,13 +447,15 @@ async function writeConsoleConfig (json) {
* @returns {Promise} promise from writeFile call
*/
async function writeAio (json, parentFolder, flags) {
console.log('-----------writting aio-------------------------')
shazron marked this conversation as resolved.
Show resolved Hide resolved
aioLogger.debug(`writeAio - parentFolder:${parentFolder} flags:${flags}`)
aioLogger.debug(`writeAio - json: ${prettyPrintJson(json)}`)

const destination = path.join(parentFolder, AIO_FILE)
aioLogger.debug(`writeAio - destination: ${destination}`)

const data = prettyPrintJson(json)
console.log('-----------done-------------------------')
shazron marked this conversation as resolved.
Show resolved Hide resolved
return writeFile(destination, data, flags)
}

Expand Down Expand Up @@ -598,6 +622,7 @@ module.exports = {
loadAndValidateConfigFile,
writeConsoleConfig,
writeAio,
writeDefaultAppConfig,
writeEnv,
flattenObjectWithSeparator,
importConfigJson,
Expand Down
18 changes: 17 additions & 1 deletion test/commands/lib/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { importConfigJson, writeAio, writeEnv, mergeEnv, splitEnvLine, flattenObjectWithSeparator, loadConfigFile } = require('../../../src/lib/import')
const { importConfigJson, writeAio, writeEnv, mergeEnv, splitEnvLine, flattenObjectWithSeparator, loadConfigFile, writeDefaultAppConfig } = require('../../../src/lib/import')
const fs = require('fs-extra')
const path = require('path')
const inquirer = require('inquirer')
Expand All @@ -31,6 +31,9 @@ test('exports', () => {
expect(writeEnv).toBeDefined()
expect(writeEnv).toBeInstanceOf(Function)

expect(writeDefaultAppConfig).toBeDefined()
expect(writeDefaultAppConfig).toBeInstanceOf(Function)

expect(flattenObjectWithSeparator).toBeDefined()
expect(flattenObjectWithSeparator).toBeInstanceOf(Function)
})
Expand Down Expand Up @@ -77,6 +80,19 @@ test('writeAio', async () => {
return expect(fs.writeFile).toHaveBeenCalledTimes(3)
})

test('writeDefaultAppConfig', async () => {
const parentFolder = 'my-parent-folder'
const aioPath = path.join(parentFolder, '.aio')

writeDefaultAppConfig(parentFolder, { overwrite: true })
await expect(fs.writeFile.mock.calls[0][0]).toMatch(aioPath)

writeDefaultAppConfig(parentFolder, { overwrite: false })
await expect(fs.writeFile.mock.calls[1][0]).toMatch(aioPath)

return expect(fs.writeFile).toHaveBeenCalledTimes(2)
})

test('splitEnvLine', () => {
expect(splitEnvLine('#comment')).toEqual(['#comment', undefined])
expect(splitEnvLine('# comment')).toEqual(['# comment', undefined])
Expand Down