-
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.
- Loading branch information
Showing
5 changed files
with
105 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@codeowners-flow/cli': minor | ||
--- | ||
|
||
add new "init" command |
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,48 @@ | ||
import { initProject } from './init.js'; | ||
|
||
const mockExistsSync = vi.fn(); | ||
const mockWriteFileSync = vi.fn(); | ||
vi.mock('node:fs', () => ({ | ||
default: { | ||
existsSync: (...args: any) => mockExistsSync(...args), | ||
writeFileSync: (...args: any) => mockWriteFileSync(...args), | ||
}, | ||
})); | ||
|
||
describe('fn: initProject', () => { | ||
it('throws an error if config file already exists', async () => { | ||
mockExistsSync.mockReturnValue(true); | ||
await expect(initProject()).rejects.toThrowError( | ||
'Configuration file already exists.', | ||
); | ||
}); | ||
|
||
it('writes the config file correctly', async () => { | ||
const spyCwd = vi.spyOn(process, 'cwd'); | ||
|
||
spyCwd.mockReturnValue('/path/to'); | ||
|
||
mockExistsSync.mockReturnValue(false); | ||
await initProject(); | ||
|
||
const [path, content] = mockWriteFileSync.mock.calls[0]; | ||
|
||
expect(path).toMatchInlineSnapshot('"/path/to/codeowners.config.mjs"'); | ||
expect(content).toMatchInlineSnapshot(` | ||
"/** @type {import('@codeowners-flow/cli/config').UserConfig} */ | ||
export default { | ||
outDir: '.github', | ||
rules: [ | ||
{ | ||
patterns: ['*'], | ||
owners: [ | ||
{ | ||
name: '@company-team', | ||
}, | ||
], | ||
}, | ||
], | ||
};" | ||
`); | ||
}); | ||
}); |
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,32 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
export async function initProject() { | ||
const content = getConfigTemplate(); | ||
|
||
const configPath = path.resolve(process.cwd(), 'codeowners.config.mjs'); | ||
const exists = fs.existsSync(configPath); | ||
|
||
if (exists) { | ||
throw new Error('Configuration file already exists.'); | ||
} | ||
|
||
fs.writeFileSync(configPath, content); | ||
} | ||
|
||
function getConfigTemplate() { | ||
return `/** @type {import('@codeowners-flow/cli/config').UserConfig} */ | ||
export default { | ||
outDir: '.github', | ||
rules: [ | ||
{ | ||
patterns: ['*'], | ||
owners: [ | ||
{ | ||
name: '@company-team', | ||
}, | ||
], | ||
}, | ||
], | ||
};`; | ||
} |