Skip to content

Commit

Permalink
Merge pull request #29 from FreakeyPlays/dev
Browse files Browse the repository at this point in the history
Release of Version v0.2.1
  • Loading branch information
FreakeyPlays committed Nov 6, 2022
2 parents 16b8693 + bd6cd23 commit f1ffa42
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class TemplateTodoFormComponent implements OnInit {
ngOnInit(): void {}

public createNewToDo(e?: Event): void {
if (this.newToDo.label.length <= 0) {
return
}

if (typeof this.newToDo.priority === 'string') {
this.newToDo.priority = parseInt(this.newToDo.priority)
}
Expand Down
125 changes: 89 additions & 36 deletions client/src/setEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,113 @@ const { argv } = require('yargs')
const path = require('path')
require('dotenv').config()

const environmentVariablesInKeyValuePairs = Object.fromEntries(
Object.entries(process.env).filter(([key]) => key.startsWith('NG_APP_'))
)

// Read Argument and set Production environment true/false.
const environment = argv.environment
const isProduction = environment === 'prod'

// Read the variables from .env File or from process.env.
let environmentVariablesInKeyValuePairs
if (isProduction) {
environmentVariablesInKeyValuePairs = Object.fromEntries(
Object.entries(process.env).filter(([key]) => key.startsWith('NG_APP_'))
)
} else {
environmentVariablesInKeyValuePairs = readFileSync(
path.resolve(__dirname, '../../.env'),
{
encoding: 'utf-8'
}
)
.split('\n')
.map((value: string): string => {
return value.replace('\r', '')
})
.filter((value: string): boolean => {
return value != ''
})
.map((keyValuePair: string): string[] => {
return keyValuePair.split('=').map((value: string): string => {
return value.trim()
})
})
}

// Create Folder if it doesn't exist.
const folderPath = './src/environments'
if (!existsSync(folderPath)) {
mkdirSync(folderPath, { recursive: true })
}

// Create File if it doesn't exist.
const envFile = '/environment.ts'
if (!existsSync(folderPath + envFile)) {
writeFile(folderPath + envFile, '', (err: any) => {
if (err) {
console.warn(err)
process.exit(-1)
}
console.log(`Created ${envFile} at ${folderPath}`)
})
}
writeToFile(
folderPath + envFile,
'export const environment = {\n production: false \n}',
`Created ${envFile} at ${folderPath}`
)

// Create File if it doesn't exist.

const prodEnvFile = '/environment.prod.ts'
if (!existsSync(folderPath + prodEnvFile)) {
writeFile(folderPath + prodEnvFile, '', (err: any) => {
if (err) {
console.warn(err)
process.exit(-1)
}
console.log(`Created ${prodEnvFile} at ${folderPath}`)
})
}
writeToFile(
folderPath + prodEnvFile,
'export const environment = {\n production: true \n}',
`Created ${prodEnvFile} at ${folderPath}`
)

// Define the final Environment File target Path.
const targetPath = isProduction
? `${folderPath}/environment.prod.ts`
: `${folderPath}/environment.ts`

// Write content of environment File in a String.
let environmentFileContent = `export const environment = {\n production: ${isProduction}`

for (const [key, value] of Object.entries(
environmentVariablesInKeyValuePairs
)) {
environmentFileContent += `,\n ${key}: ${
typeof value === 'string' ? '"' + value + '"' : value
}`
if (isProduction) {
for (const [key, value] of Object.entries(
environmentVariablesInKeyValuePairs
)) {
environmentFileContent += `,\n ${key}: ${
typeof value === 'string' ? '"' + value + '"' : value
}`
}
} else {
for (let keyValuePair of environmentVariablesInKeyValuePairs) {
environmentFileContent += `,\n ${keyValuePair[0]}: ${keyValuePair[1]}`
}
}

environmentFileContent += '\n}'

writeFile(targetPath, environmentFileContent, (err: any) => {
if (err) {
console.warn(err)
process.exit(-1)
writeToFile(
targetPath,
environmentFileContent,
`Wrote variables to ${targetPath}`,
true
)

/**
* This function writes the content to the target Path.
* Before writing there is a check if the File exists. If it does
* exist or the skipExistingCheck is true it will be skipped.
* If successful a console log with successfulMessage will be
* shown. Otherwise the Program will exit.
* @param target String with Path to File
* @param content String with content of the File
* @param successfulMessage String with Successful Message
* @param skipExistingCheck Boolean to skip the Existing check
*/
function writeToFile(
target: String,
content: String,
successfulMessage: String,
skipExistingCheck: boolean = false
): void {
if (!existsSync(target) || skipExistingCheck) {
writeFile(target, content, (err: any) => {
if (err) {
console.warn(err)
process.exit(-1)
}
console.log(successfulMessage)
})
}
console.log(`Wrote variables to ${targetPath}`)
})
}

0 comments on commit f1ffa42

Please sign in to comment.