Skip to content

Commit

Permalink
Ensure certain paths are always in gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshuman-37 committed Aug 25, 2024
1 parent 86fa0a9 commit 472001d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions programs/create/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {installDependencies} from './steps/install-dependencies'
import {writeReadmeFile} from './steps/write-readme-file'
import {writeManifestJson} from './steps/write-manifest-json'
import {generateExtensionTypes} from './steps/generate-extension-types'
import {writeGitignore} from './steps/write-gitignore'
import {initializeGitRepository} from './steps/initialize-git-repository'

export interface CreateOptions {
Expand Down Expand Up @@ -53,6 +54,7 @@ export async function extensionCreate(
await writeReadmeFile(projectPath, projectName)
await writeManifestJson(projectPath, projectName)
await initializeGitRepository(projectPath, projectName)
await writeGitignore(projectPath)

if (utils.isTypeScriptTemplate(template)) {
await generateExtensionTypes(projectPath, projectName)
Expand Down
38 changes: 38 additions & 0 deletions programs/create/steps/write-gitignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from 'fs/promises'

const globalLines = ['/dist', 'extension.d.ts']

export async function writeGitignore(projectPath: string) {
const gitIgnorePath = projectPath + '/.gitignore'

const fileHandle = await fs.open(gitIgnorePath, 'a+').catch((err) => {
console.error(err)
process.exit(1)
})

const paths = new Set<String>()
// autoClose is required, otherwise the stream will close the file
// and we'll be unable to append to it
//
// Note: while readLines works for this use-case, care needs to be had
// if adding more contents to the loop, basically, the loop should just
// consume the data from readLines and nothing else
//
// Reference: https://stackoverflow.com/a/70616910
for await (let line of fileHandle.readLines({autoClose: false})) {
line = line.trim()
if (line.length === 0 || line.startsWith('#')) {
continue
}
paths.add(line)
}

const linesToAdd = globalLines.filter((line) => !paths.has(line))

await fileHandle
.appendFile(linesToAdd.join('\n'), {flush: true})
.catch((err) => {
console.error(err)
process.exit(1)
})
}

0 comments on commit 472001d

Please sign in to comment.