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 28, 2024
1 parent 86fa0a9 commit 880f577
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions programs/create/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ export function writingReadmeMetaData() {
return `📄 - Writing ${brightYellow(`README.md`)} metadata...`
}

export function writingGitIgnore() {
return `🙈 - Writing ${brightYellow(`.gitignore`)} lines...`
}

export function writingReadmeMetaDataEError(projectName: string, error: any) {
return (
`${red(`✖︎✖︎✖︎`)} ` +
Expand Down
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
54 changes: 54 additions & 0 deletions programs/create/steps/write-gitignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs/promises'
import * as messages from '../lib/messages'

const globalDependencies = ['# dependencies', '/node_modules']
const globalTesting = ['# testing', '/coverage']
const globalProduction = ['# production', '/dist']
const globalMisc = ['# misc', '.DS_Store', '.env.local', '.env.development.local',
'.env.test.local', '.env.production.local', 'npm-debug.log*', 'yarn-debug.log*', 'yarn-error.log*', 'extension.d.ts']
const globalLockFiles = ['# lock files', 'yarn.lock', 'package-lock.json']

const globalLines = [
...globalDependencies,
...globalTesting,
...globalProduction,
...globalMisc,
...globalLockFiles
]

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) {
continue
}
paths.add(line)
}

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

console.log(messages.writingGitIgnore())

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

0 comments on commit 880f577

Please sign in to comment.