Skip to content

Commit

Permalink
Feat/len add for file method (#2)
Browse files Browse the repository at this point in the history
* feat: add for file method to templating

* fix: cli path on logs

* fix: change port to 3000
  • Loading branch information
jlenon7 authored Feb 3, 2022
1 parent e84332c commit 21cae29
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
31 changes: 30 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import figlet from 'figlet'

import { Command } from 'commander'
import { Logger } from '@secjs/logger'
import { dirname, resolve } from 'path'
import { dirname, parse, resolve } from 'path'
import { Templating } from './src/Templating'
import { FieldsSanitizer } from './src/FieldsSanitizer'

Expand All @@ -15,6 +15,35 @@ const packageJson = require('./package.json')

command.version(packageJson.version, '-v, --version')

command
.command('generateFile [filePath]')
.description(
'Generate the file according to file template and environment variables.',
)
.option('-s, --set [fields...]', 'Subscribe environment variables')
.action(async (filePath, fields) => {
const logger = new Logger('CLI')

try {
filePath = resolve(filePath)
fields = FieldsSanitizer.validateAll(fields.set)

const templating = new Templating()

await templating.forFile(filePath)
await templating.formatEnvs()
await templating.formatFields(fields)

await templating.generate()

const { base } = parse(filePath)

logger.success(`✅ File ${base} has been replaced!`)
} catch (error) {
logger.error(`❌ Something went wrong: ${error.toString()}`)
}
})

command
.command('generate [path]')
.description(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jlenon7/templating",
"version": "1.1.0",
"version": "1.1.1",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand Down
10 changes: 10 additions & 0 deletions src/Templating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ export class Templating {
return this.templates
}

async forFile(filePath: string) {
const { base, dir } = parse(filePath)

this.templatesPath = `${dir}/${base}`

this.logger.success(`🔍 Template found: ${base}`)

this.templates.set(base, (await readFile(filePath)).toString())
}

async generate() {
const promises = []

Expand Down
25 changes: 24 additions & 1 deletion tests/templating.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname } from 'path'
import { readFile } from 'fs/promises'
import { readFile, writeFile } from 'fs/promises'
import { Templating } from '../src/Templating'

describe('\n Templating Class', () => {
Expand Down Expand Up @@ -76,4 +76,27 @@ describe('\n Templating Class', () => {

expect(file.toString().includes('jlenon7/templating:latest')).toBe(true)
})

it('should be able to generate the formatted file', async () => {
const filePath = process.cwd() + '/manifest/templates/config-map.yml'

const template = (await readFile(filePath)).toString()

const templating = new Templating()

await templating.forFile(filePath)

process.env.HOST = 'http://127.0.0.1'
process.env.NODE_ENV = 'production'

await templating.formatEnvs()
await templating.formatFields({ PORT: '3000' })

await templating.generate()

const file = (await readFile(filePath)).toString()
await writeFile(filePath, template)

expect(file.includes('3000')).toBe(true)
})
})

0 comments on commit 21cae29

Please sign in to comment.