Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure certain paths are always in gitignore #158

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}