-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update addToGitignore functionality
- Modularize isInGitignore functionality - Move inquirer confirm question to inside addToGitignore - Check isInGitignore before ask user to add to .gitignore - Check last file character and add new line if it is necessary - Move add to gitignore call for rc file after saving the config file About testing: - Add tests - Modify check-string-in-file exporting an object, so content can be stubbed - Add a inquirer wrapper in cli file for same previous reason: allow to stub inquirer calls. Closes #21
- Loading branch information
1 parent
32612de
commit 4389ce7
Showing
7 changed files
with
153 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
node_modules/ | ||
dist-types/ | ||
.api-mock-runner | ||
.api-mock-runner/ | ||
.apimockrc | ||
coverage/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as inquirer from '@inquirer/prompts'; | ||
|
||
/** | ||
* Confirm through CLI adding a file to .gitignore | ||
* @async | ||
* @function confirmAddToGitignore | ||
* @param {string} fileName - The file name to add to .gitignore | ||
* @returns {Promise<boolean>} True if the user confirms, false otherwise | ||
*/ | ||
async function confirmAddToGitignore(fileName) { | ||
return await inquirer.confirm({ | ||
message: `Add ${fileName} to .gitignore?`, | ||
}); | ||
} | ||
|
||
export const cli = { confirmAddToGitignore }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { checkStringInFile } from './check-string-in-file.js'; | ||
import { cli } from './cli.js'; | ||
|
||
export const GITIGNORE_PATH = path.join(process.cwd(), '.gitignore'); | ||
|
||
/** | ||
* Append a newline with file or folder name to .gitignore. | ||
* If .gitignore does not exist, it will be created. | ||
* If the file or folder name is already in .gitignore, it will not be added again. | ||
* Any action is confirmed through the CLI by the user. | ||
* @async | ||
* @function addToGitignore | ||
* @param {string} fileName - The file or folder name to append to .gitignore | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async function addToGitignore(fileName) { | ||
const existsGitignoreFile = fs.existsSync(GITIGNORE_PATH); | ||
if ((!existsGitignoreFile || !(await isInGitignore(fileName))) && (await cli.confirmAddToGitignore(fileName))) { | ||
const leadingCharacter = existsGitignoreFile ? getLeadingCharacter() : ''; | ||
fs.appendFileSync(GITIGNORE_PATH, `${leadingCharacter}${fileName}\n`); | ||
} | ||
} | ||
|
||
/** | ||
* Check if a string is in .gitignore | ||
* @async | ||
* @function isInGitignore | ||
* @param {string} textToCheck - The text to check | ||
* @returns {Promise<boolean>} True if the text is in .gitignore, false otherwise | ||
*/ | ||
async function isInGitignore(textToCheck) { | ||
const result = await checkStringInFile.check(textToCheck, GITIGNORE_PATH); | ||
return result; | ||
} | ||
|
||
function getLeadingCharacter() { | ||
let leadingCharacter = ''; | ||
const lastFileCharacter = fs.readFileSync(GITIGNORE_PATH, 'utf8').slice(-1); | ||
leadingCharacter = lastFileCharacter === '\n' ? '' : '\n'; | ||
return leadingCharacter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { expect, use } from 'chai'; | ||
import fs from 'node:fs'; | ||
import { restore, stub } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { checkStringInFile } from '../../../src/services/check-string-in-file.js'; | ||
import { cli } from '../../../src/services/cli.js'; | ||
import addToGitignore, { GITIGNORE_PATH } from '../../../src/services/gitignore.js'; | ||
use(sinonChai); | ||
|
||
describe('unit:addToGitignore', () => { | ||
const gitignoreContentNoNewline = 'fileContentTest'; | ||
const fileNameTest = 'fileNameTest'; | ||
const lineToAdd = `${fileNameTest}\n`; | ||
let appendFileSyncStub; | ||
let checkStringInFileStub; | ||
let confirmStub; | ||
let existsSyncStub; | ||
let readFileSyncStub; | ||
|
||
beforeEach(() => { | ||
appendFileSyncStub = stub(fs, 'appendFileSync'); | ||
checkStringInFileStub = stub(checkStringInFile, 'check'); | ||
confirmStub = stub(cli, 'confirmAddToGitignore'); | ||
existsSyncStub = stub(fs, 'existsSync'); | ||
readFileSyncStub = stub(fs, 'readFileSync'); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('should not add filename when already is in it', async () => { | ||
existsSyncStub.returns(true); | ||
checkStringInFileStub.returns(true); | ||
await addToGitignore(fileNameTest); | ||
expect(existsSyncStub).to.have.been.calledWith(GITIGNORE_PATH); | ||
expect(checkStringInFileStub).to.have.been.calledWith(fileNameTest, GITIGNORE_PATH); | ||
expect(confirmStub).to.not.have.been.called; | ||
expect(readFileSyncStub).to.not.have.been.called; | ||
expect(appendFileSyncStub).to.not.have.been.called; | ||
}); | ||
|
||
it('should not add filename when user refuses', async () => { | ||
existsSyncStub.returns(false); | ||
confirmStub.returns(false); | ||
await addToGitignore(fileNameTest); | ||
expect(existsSyncStub).to.have.been.called; | ||
expect(confirmStub).to.have.been.called; | ||
expect(readFileSyncStub).to.not.have.been.called; | ||
expect(appendFileSyncStub).to.not.have.been.called; | ||
}); | ||
|
||
it('should add newline and filename to existing .gitignore when user accepts', async () => { | ||
existsSyncStub.returns(true); | ||
confirmStub.returns(true); | ||
readFileSyncStub.returns(gitignoreContentNoNewline); | ||
await addToGitignore(fileNameTest); | ||
expect(existsSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH); | ||
expect(confirmStub).to.have.been.calledOnceWith(fileNameTest); | ||
expect(readFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, 'utf8'); | ||
expect(appendFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, `\n${lineToAdd}`); | ||
}); | ||
|
||
it('should add filename to existing .gitignore when user accepts', async () => { | ||
existsSyncStub.returns(true); | ||
confirmStub.returns(true); | ||
readFileSyncStub.returns(`${gitignoreContentNoNewline}\n`); | ||
await addToGitignore(fileNameTest); | ||
expect(existsSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH); | ||
expect(confirmStub).to.have.been.calledOnceWith(fileNameTest); | ||
expect(readFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, 'utf8'); | ||
expect(appendFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, `${lineToAdd}`); | ||
}); | ||
|
||
it('should add filename to missing .gitignore when user accepts', async () => { | ||
existsSyncStub.returns(false); | ||
confirmStub.returns(true); | ||
await addToGitignore(fileNameTest); | ||
expect(existsSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH); | ||
expect(confirmStub).to.have.been.calledOnceWith(fileNameTest); | ||
expect(readFileSyncStub).to.not.have.been.called; | ||
expect(appendFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, `${lineToAdd}`); | ||
}); | ||
}); |