Skip to content

Commit

Permalink
chore: Copy example files recursively to work on mono repos
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Jul 9, 2024
1 parent ad2a143 commit d9219be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/commands/environment/copy-example-files.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { join } from 'node:path'
import { Command } from 'commander'
import { projectConfig } from '../../config/project.config'
import { copyFile } from '../../utils/file-system'
import { copyFile, readPathMatchingFiles } from '../../utils/file-system'

export function copyExampleFilesCommand() {
return new Command()
.name('copy-example-files')
.summary('Copy example files from the ')
.summary('Copy example dotenv files on each repo path')
.action(() => {
for (const path of projectConfig.reposPaths()) {
copyFile({
source: join(path, '.env.example'),
target: join(path, '.env'),
})
const exampleEnvFiles = readPathMatchingFiles(path, '.env.example')

if (exampleEnvFiles.length > 0) {
for (const file of exampleEnvFiles) {
copyFile({
source: file,
target: file.replace('.example', ''),
})
}
}
}
})
}
51 changes: 39 additions & 12 deletions src/utils/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import { confirm } from '@inquirer/prompts'
import type {
FileModificationOperation,
Expand All @@ -7,21 +8,21 @@ import type {
} from './interfaces'
import logger from './log'

export const isFile = (path: string) => {
export async function isFile(path: string) {
return fs.existsSync(path) && fs.lstatSync(path).isFile()
}

export const isDirectory = (path: string) => {
export async function isDirectory(path: string) {
return fs.existsSync(path) && fs.lstatSync(path).isDirectory()
}

export const copyFile = ({ source, target }: SourceTargetOperation) => {
if (isFile(target)) {
export async function copyFile({ source, target }: SourceTargetOperation) {
if (await isFile(target)) {
logger.warn(`Path: ${target} already exists.`)
return
}

if (isFile(source)) {
if (await isFile(source)) {
fs.copyFileSync(source, target, 0)
logger.info(`File: ${source} copied to ${target}.`)
} else {
Expand All @@ -30,11 +31,11 @@ export const copyFile = ({ source, target }: SourceTargetOperation) => {
}
}

export const createFile = ({
export function createFile({
file,
content,
overwrite = false,
}: FileModificationOperation) => {
}: FileModificationOperation) {
if (!isFile(file) || overwrite) {
logger.info(`Creating file: ${file}...`)
fs.writeFileSync(file, content, 'utf8')
Expand All @@ -44,7 +45,7 @@ export const createFile = ({
}
}

export const createPath = ({ path }: PathModificationOperation) => {
export function createPath({ path }: PathModificationOperation) {
if (!isDirectory(path)) {
logger.info(`Creating path: ${path}...`)
fs.mkdirSync(path, { recursive: true })
Expand All @@ -54,11 +55,11 @@ export const createPath = ({ path }: PathModificationOperation) => {
}
}

export const deletePath = async ({
export async function deletePath({
path,
force = false,
}: PathModificationOperation) => {
if (isDirectory(path)) {
}: PathModificationOperation) {
if (await isDirectory(path)) {
if (
force ||
(await confirm({ message: `Delete directory ${path} recursively?` }))
Expand All @@ -68,7 +69,7 @@ export const deletePath = async ({
}
}

if (isFile(path)) {
if (await isFile(path)) {
if (
force ||
(await confirm({
Expand All @@ -82,3 +83,29 @@ export const deletePath = async ({

return
}

export function readPathMatchingFiles(
dir: string,
matchFileName: string,
): Array<string> {
const results: Array<string> = [];

function recursiveReadDir(dir: string) {
const entries = fs.readdirSync(dir, { withFileTypes: true })

for (const entry of entries) {
const fullPath = path.join(entry.parentPath, entry.name)

if (entry.isDirectory()) {
recursiveReadDir(fullPath)
}

if (entry.isFile() && entry.name.includes(matchFileName)) {
results.push(fullPath)
}
}
}

recursiveReadDir(dir)
return results
}

0 comments on commit d9219be

Please sign in to comment.