Skip to content

Commit

Permalink
add new init command (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulfdm authored Aug 14, 2023
1 parent ce2341b commit 10dc06f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-experts-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codeowners-flow/cli': minor
---

add new "init" command
24 changes: 6 additions & 18 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,18 @@ The first step is to install `codeowners-flow` in your project:
pnpm add codeowners-flow
```

Next, create a file named `codeowners.config.mjs`:
Next, run the init command:

```js
/** @type {import('@codeowners-flow/cli/config').UserConfig} */
export default {
outDir: '.github',
rules: [
{
patterns: ['*'],
owners: [
{
name: '@company/team',
},
],
},
],
};
```bash
pnpm codeowners-flow init # or npx codeowners-flow init
```

This command will create a file in the folder you're named `codeowners.config.mjs`. This config file is where you're gonna define your code owners shape.

After that, you can run the CLI:

```bash
pnpm codeowners-flow generate
# or npx codeowners-flow generate
pnpm codeowners-flow generate # or npx codeowners-flow generate
```

The CLI will read your configuration and generate a `CODEOWNERS` file in the specified `outDir`:
Expand Down
15 changes: 14 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import meow from 'meow';

import { generateCodeOwners } from './generate-codeowners.js';
import { initProject } from './init.js';

const cli = meow(
`
Usage
$ codeowners-flow generate
$ codeowners-flow generate
$ codeowners-flow init
Example
$ codeowners-flow generate
Expand Down Expand Up @@ -33,6 +35,17 @@ switch (command) {
});
break;
}
case 'init': {
initProject()
.then(() => {
console.log('Configuration file created! 🎉');
})
.catch((error) => {
console.error('Error initializing project:', error);
process.exit(1);
});
break;
}
default: {
cli.showHelp();
}
Expand Down
48 changes: 48 additions & 0 deletions packages/cli/src/init.test.ts
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',
},
],
},
],
};"
`);
});
});
32 changes: 32 additions & 0 deletions packages/cli/src/init.ts
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',
},
],
},
],
};`;
}

0 comments on commit 10dc06f

Please sign in to comment.