Skip to content

Commit 19aaef0

Browse files
committed
feat: add a new pipe command
1 parent d331839 commit 19aaef0

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

commands/CredentialsPipe.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @bitkidd/adonis-credentials
3+
*
4+
* (c) Chirill Ceban <cc@bitkidd.dev>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import fs from 'fs'
11+
import execa from 'execa'
12+
import { args, BaseCommand, flags } from '@adonisjs/core/build/standalone'
13+
import { Credentials } from '../src/Credentials'
14+
15+
export default class CredentialsPipe extends BaseCommand {
16+
public static commandName = 'credentials:pipe'
17+
public static description = 'Read and pipe credentials'
18+
public static settings = {
19+
loadApp: false,
20+
stayAlive: true,
21+
}
22+
23+
@args.string({
24+
description: 'Specify an ace command to pipe credentials to',
25+
})
26+
public command: string
27+
28+
@flags.string({
29+
description: 'Specify an environment for credentials file (default: development)',
30+
})
31+
public env: string
32+
33+
public async run(): Promise<void> {
34+
const env = this.env || process.env.NODE_ENV || 'development'
35+
const [command, ...params] = this.command?.split(' ')
36+
const credentialsPath = this.application.resourcesPath('credentials')
37+
38+
if (!fs.existsSync(`${credentialsPath}/${env}.key`)) {
39+
this.logger.error(`Credentials key file for '${env}' environment does not exist`)
40+
return
41+
}
42+
43+
if (!fs.existsSync(`${credentialsPath}/${env}.credentials`)) {
44+
this.logger.error(`Credentials file for '${env}' environment does not exist`)
45+
return
46+
}
47+
48+
const credentials = new Credentials({ env, credentialsPath })
49+
50+
try {
51+
await execa.node(command, [...params], {
52+
stdio: 'inherit',
53+
env: { ...(credentials.get() as Record<string, string>) },
54+
})
55+
} catch (error) {
56+
console.log(error)
57+
}
58+
}
59+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
"instructionsMd": "./build/instructions.md",
8585
"commands": [
8686
"@bitkidd/adonis-credentials/build/commands/CredentialsCreate",
87-
"@bitkidd/adonis-credentials/build/commands/CredentialsEdit"
87+
"@bitkidd/adonis-credentials/build/commands/CredentialsEdit",
88+
"@bitkidd/adonis-credentials/build/commands/CredentialsPipe"
8889
]
8990
},
9091
"dependencies": {

test/credentials-pipe.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* @bitkidd/adonis-credentials
3+
*
4+
* (c) Chirill Ceban <cc@bitkidd.dev>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import 'reflect-metadata'
11+
12+
import test from 'japa'
13+
import { Kernel } from '@adonisjs/core/build/standalone'
14+
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
15+
16+
import CredentialsCreate from '../commands/CredentialsCreate'
17+
import CredentialsPipe from '../commands/CredentialsPipe'
18+
import { fs, setupApplication } from '../test-helpers'
19+
20+
let app: ApplicationContract
21+
22+
test.group('Command - Credentials Pipe', (group) => {
23+
group.beforeEach(async () => {
24+
app = await setupApplication()
25+
const command = new CredentialsCreate(app, new Kernel(app))
26+
await command.run()
27+
})
28+
29+
group.afterEach(async () => {
30+
await fs.cleanup()
31+
})
32+
33+
test('should throw an error when credentials key file does not exist', async (assert) => {
34+
await fs.remove('resources/credentials/test.key')
35+
36+
const command = new CredentialsPipe(app, new Kernel(app))
37+
command.command = `echo 'test';`
38+
await command.run()
39+
40+
assert.deepStrictEqual(
41+
command.ui.testingRenderer.logs.map((log) => ({
42+
...log,
43+
message: log.message.replace(/(\[.*?\])/g, '').trim(),
44+
})),
45+
[
46+
{
47+
stream: 'stderr',
48+
message: `Credentials key file for 'test' environment does not exist`,
49+
},
50+
]
51+
)
52+
})
53+
54+
test('should throw an error when credentials file does not exist', async (assert) => {
55+
await fs.remove('resources/credentials/test.credentials')
56+
57+
const command = new CredentialsPipe(app, new Kernel(app))
58+
command.command = `echo 'test';`
59+
await command.run()
60+
61+
assert.deepStrictEqual(
62+
command.ui.testingRenderer.logs.map((log) => ({
63+
...log,
64+
message: log.message.replace(/(\[.*?\])/g, '').trim(),
65+
})),
66+
[
67+
{
68+
stream: 'stderr',
69+
message: `Credentials file for 'test' environment does not exist`,
70+
},
71+
]
72+
)
73+
})
74+
})

0 commit comments

Comments
 (0)