Skip to content

Commit

Permalink
feat: Add success, fail messages to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavya-24 committed Jul 4, 2022
1 parent 950393a commit c3beecd
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/commands/alias/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class Export extends AliasBaseCommand {
return
}

// Store the aliases file in the current directory
const filename = 'dataexport.json'
const aliasFilePath = new FileUtil(this).getAliasFilePath()
const destFilePath = process.cwd() + '/' + 'dataexport.json'
const destFilePath = process.cwd() + '/' + filename

if (new FileUtil(this).pathExists(aliasFilePath)) {
const ans = new FileUtil(this).copyFileToDestination(aliasFilePath, destFilePath, 'export')
if (ans) { console.log('Export Completed') }
if (ans) { console.log(`Successfully exported aliases to the file ${filename}`) }
} else {
new FileUtil(this).setupIncompleteWarning()
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/alias/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Import extends AliasBaseCommand {
const destFile = args.dest
const ans = new FileUtil(this).copyFileToDestination(destFile, aliasFilePath, 'import')

if (ans) { console.log('Import Completed') }
if (ans) { console.log(`Successfully exported aliases to the file ${destFile}`) }
} else {
new FileUtil(this).setupIncompleteWarning()
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/alias/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class List extends AliasBaseCommand {
const aliasFilePath = new FileUtil(this).getAliasFilePath()
if (new FileUtil(this).pathExists(aliasFilePath)) {
const db = await List.storage.load(aliasFilePath)
console.log('List of the stored aliases\n')
let output = 'Alias\tCommand'

for (const alias in db) {
Expand Down
1 change: 0 additions & 1 deletion src/commands/alias/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Setup extends AliasBaseCommand {

const proceed = { move: true }
new FileUtil(this).createFolderIfDoesNotExists(aliasFolderPath, proceed)

if (!proceed.move) { return }

try {
Expand Down
11 changes: 8 additions & 3 deletions src/commands/alias/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,33 @@ class Use extends AliasBaseCommand {
return
}

const supposedAlias = this.argv.shift()
let supposedAlias = this.argv.shift()
const aliasFilePath = new FileUtil(this).getAliasFilePath()
const db = await Use.storage.load(aliasFilePath)
const existUtil = new FileUtil(this).extractAlias(supposedAlias, aliasFilePath, db)

let commandToRun = supposedAlias
let foundInSuggestions = true

if (existUtil.index === -2) {
// Setup incomplete
new FileUtil(this).setupIncompleteWarning()
return
} else if (existUtil.index === -1) {
const exitMessage = 'Continue without using'
const result = await Use.prompt.findSuggestions(exitMessage, supposedAlias, db)

if (result !== exitMessage) {
if (result === exitMessage) {
foundInSuggestions = false
} else {
commandToRun = db[result]
supposedAlias = result
}
} else if (existUtil.index >= 0) {
commandToRun = existUtil.command // + this.argv
}

if (foundInSuggestions) { console.log(`Using the command ${commandToRun} from alias ${supposedAlias}`) }

new CommandUtil(this).runCommand(commandToRun, this.argv)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/utilities/FileSnapshot/FilesystemStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class FilesystemStorage {
const aliasFolderName = 'alias'
const aliasFolderPath = dataDirectory + '/' + aliasFolderName
const aliasFileName = 'data.json'

return aliasFolderPath + '/' + aliasFileName
}

Expand Down
22 changes: 21 additions & 1 deletion src/utilities/FileUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,28 @@ class FileUtility {
try {
const existUtil = this.extractAlias(userAlias, aliasFilePath, db)
const aliasIndex = existUtil.index
let added = false

// This will never run for snapshot based memory reference
if (aliasIndex === -2) {
return this.setupIncompleteWarning()
} else if (aliasIndex === -1) {
// no alias exists. Add is operation is Add, else show error for delete
db[userAlias] = userCommand
added = true
} else {
if (db[userAlias] === 'null' || hasFlag) {
db[userAlias] = userCommand
added = true
} else {
console.log(`alias already exists for command "${db[userAlias]}". Consider adding -f for overwriting`)
}
}

if (added) {
console.log(`Successfully created alias ${userAlias} for ${userCommand}`)
}

return db
} catch (err) {
console.log(err)
Expand All @@ -57,19 +66,30 @@ class FileUtility {
const existUtil = this.extractAlias(userAlias, aliasFilePath, db)
const aliasIndex = existUtil.index

let deleted = false

// This will never run for snapshot based memory reference
if (aliasIndex === -2) {
return this.setupIncompleteWarning()
} else if (aliasIndex === -1) {
// no alias exists. Add is operation is Add, else show error for delete
const exitMessage = 'Continue without deleting'
const result = await FileUtility.prompt.findSuggestions(exitMessage, userAlias, db)

if (result === exitMessage) {
console.log(`${userAlias} is not a ${this.ctx.config.bin} command.`)
} else {
delete db[result]
userAlias = result
delete db[userAlias]
deleted = true
}
} else {
delete db[userAlias]
deleted = true
}

if (deleted) {
console.log(`Successfully deleted alias ${userAlias}`)
}

return db
Expand Down
4 changes: 3 additions & 1 deletion src/utilities/InquirerPrompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class InquirerPrompts {
}
})

return commandIDs.slice(0, numOfSuggestions)
const suggestions = commandIDs.slice(0, numOfSuggestions)

return suggestions
}
}
module.exports = InquirerPrompts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MockPrompts {
}

constructSuggestions (_userAlias, _db) {

}
}
module.exports = MockPrompts
9 changes: 6 additions & 3 deletions test/commands/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ describe('Tests for adding alias', () => {
.stub(Add, 'storage', new MemoryStorage({}))
.stub(FileUtil, 'storage', new MemoryStorage({}))
.command(['alias:add', 'hello', 'world'])
.it('should add the alias empty', async _ctx => {
.it('should add the alias empty', async ctx => {
expect(await Add.storage.load()).to.eql({
hello: 'world'
})
expect(ctx.stdout).to.contain('Successfully created alias hello for world')
})
})

Expand All @@ -66,11 +67,12 @@ describe('Tests for adding alias', () => {
.stub(Add, 'storage', new MemoryStorage({ aliasName: 'aliasCommand' }))
.stub(FileUtil, 'storage', new MemoryStorage({ aliasName: 'aliasCommand' }))
.command(['alias:add', 'hello', 'world'])
.it('should add the alias', async _ctx => {
.it('should add the alias', async ctx => {
expect(await Add.storage.load()).to.eql({
aliasName: 'aliasCommand',
hello: 'world'
})
expect(ctx.stdout).to.contain('Successfully created alias hello for world')
})
})

Expand All @@ -94,10 +96,11 @@ describe('Tests for adding alias', () => {
.stub(Add, 'storage', new MemoryStorage({ hello: 'name' }))
.stub(FileUtil, 'storage', new MemoryStorage({ hello: 'name' }))
.command(['alias:add', 'hello', 'world', '-f'])
.it('should add the alias forcefully', async _ctx => {
.it('should add the alias forcefully', async ctx => {
expect(await Add.storage.load()).to.eql({
hello: 'world'
})
expect(ctx.stdout).to.contain('Successfully created alias hello for world')
})
})

Expand Down
8 changes: 5 additions & 3 deletions test/commands/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { expect, test } = require('@oclif/test')
const MemoryStorage = require('../../src/utilities/FileSnapshot/MemoryStorage.js')
const Delete = require('../../src/commands/alias/delete.js')
const FileUtil = require('../../src/utilities/FileUtility')
const MockPrompts = require('../../src/utilities/MockPrompt')
const MockPrompts = require('../../src/utilities/MockPrompts')

describe('Tests for deleting alias', () => {
describe('Before Setup', () => {
Expand Down Expand Up @@ -41,10 +41,11 @@ describe('Tests for deleting alias', () => {
.stub(Delete, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' }))
.stub(FileUtil, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' }))
.command(['alias:delete', 'hello'])
.it('should delete the alias', async _ctx => {
.it('should delete the alias', async ctx => {
expect(await Delete.storage.load()).to.eql({
hello2: 'world2'
})
expect(ctx.stdout).to.contain('Successfully deleted alias hello')
})
})

Expand Down Expand Up @@ -72,10 +73,11 @@ describe('Tests for deleting alias', () => {
.stub(Delete, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' }))
.stub(FileUtil, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' }))
.command(['alias:delete', 'he'])
.it('should show the suggestions but accepted', async _ctx => {
.it('should show the suggestions but accepted', async ctx => {
expect(await Delete.storage.load()).to.eql({
hello2: 'world2'
})
expect(ctx.stdout).to.contain('Successfully deleted alias hello')
})
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/commands/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Tests for exporting alias', () => {
.stub(Export, 'storage', new MemoryStorage({}, false))
.stub(FileUtil, 'storage', new MemoryStorage({}, false))
.command(['alias:export'])
.it('should throw chalk error', async _ctx => {
.it('should throw chalk error', async ctx => {
expect(await FileUtil.storage.load()).to.eql({

})
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Tests for exporting alias', () => {
expect(await FileUtil.storage.load()).to.eql({

})
expect(ctx.stdout).to.contain('Export Completed')
expect(ctx.stdout).to.contain('Successfully exported aliases to the file dataexport.json')

const fileStorage = new FilesystemStorage()
const db = await fileStorage.load(path)
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('Tests for exporting alias', () => {
hello: 'world',
hello1: 'world2'
})
expect(ctx.stdout).to.contain('Export Completed')
expect(ctx.stdout).to.contain('Successfully exported aliases to the file dataexport.json')
const fileStorage = new FilesystemStorage()
const db = await fileStorage.load(path)
expect(db).to.be.a('object')
Expand Down
4 changes: 2 additions & 2 deletions test/commands/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Tests for importing alias', () => {
expect(await FileUtil.storage.load()).to.eql({

})
expect(ctx.stdout).to.contain('Import Completed')
expect(ctx.stdout).to.contain(`Successfully exported aliases to the file ${filename}`)
})

after(async function () {
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('Tests for importing alias', () => {
alist: 'alias:list',
hello: 'world'
})
expect(ctx.stdout).to.contain('Import Completed')
expect(ctx.stdout).to.contain(`Successfully exported aliases to the file ${filename}`)
})

after(async function () {
Expand Down
Loading

0 comments on commit c3beecd

Please sign in to comment.