Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Commit

Permalink
chore: set up for codeowners transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
william2958 committed Jun 13, 2023
1 parent 76dc195 commit 585b41f
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
"argumentName": "PACKAGE_NAME",
"associatedCommands": ["meta-init"],
"required": false
},
{
"parameterKind": "flag",
"description": "Whether to output a codeowners file",
"longName": "--codeowners",
"associatedCommands": ["meta-init", "meta-sync"],
"required": false
}
]
}
7 changes: 7 additions & 0 deletions rush-plugins/rush-metadata-plugin/command-line.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
"argumentName": "PACKAGE_NAME",
"associatedCommands": ["meta-init"],
"required": false
},
{
"parameterKind": "flag",
"description": "Whether to output a codeowners file",
"longName": "--codeowners",
"associatedCommands": ["meta-init", "meta-sync"],
"required": false
}
]
}
10 changes: 4 additions & 6 deletions rush-plugins/rush-metadata-plugin/config/project-metadata.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"purpose": "d",
"pointOfContact": "d",
"projectGroup": "d",
"purpose": "l",
"pointOfContact": ["j", "k", "l"],
"projectGroup": "k",
"targetRuntime": "node",
"riskLevel": 0,
"productLine": "",
"environmentVars": "d"
"productLine": "m"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"metadataFileName": "config/project-metadata.json",
"codeownersFileName": "config/CODEOWNERS",
"fields": [
{
"name": "purpose",
"description": "The purpose of the package.",
"prompt": "What is the purpose of this package?zzz",
"prompt": "What is the purpose of this package?",
"fieldType": "string",
"required": true
},
Expand Down
26 changes: 18 additions & 8 deletions rush-plugins/rush-metadata-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@ async function main(): Promise<void> {
}
}
)
.command('sync', 'Sync the metadata in the monorepo.', async () => {
const { syncMeta } = await import('./syncMeta');
try {
await syncMeta();
} catch (e: any) {
console.error('error: ', e);
process.exit(1);
.command(
'sync',
'Sync the metadata in the monorepo.',
(yargs) => {
return yargs.option('codeowners', {
type: 'boolean',
describe: 'option to generate codeowners file'
});
},
async ({ codeowners }) => {
const { syncMeta } = await import('./syncMeta');
try {
await syncMeta({ codeowners: !!codeowners });
} catch (e: any) {
console.error('error: ', e);
process.exit(1);
}
}
})
)
.demandCommand(1, 'You need at least one command before moving on')
.parse();
}
7 changes: 6 additions & 1 deletion rush-plugins/rush-metadata-plugin/src/initMeta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export const initMeta = async ({ project }: { project: string }): Promise<void>

const allFields: IMetadataField[] = getAllMetadataFields();

const answers: Record<string, string> = await queryFields(allFields);
const answers: Record<string, string | string[]> = await queryFields(allFields);

if (answers.pointOfContact) {
const enteredPointsOfContact: string = answers.pointOfContact as string;
answers.pointOfContact = enteredPointsOfContact.split(',').map((s) => s.trim());
}

JsonFile.save(answers, metaFilePath, { ensureFolderExists: true });
};
19 changes: 16 additions & 3 deletions rush-plugins/rush-metadata-plugin/src/logic/customMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ export const getDefaultMetadataFileName = (): string => {
return metadataFileName;
};

export const getCustomMetadataInfo = (): IPluginConfig => {
export const getDefaultCodeownersFileName = (): string => {
const { codeownersFileName }: { codeownersFileName: string } = DefaultFields;
return codeownersFileName;
};

export interface ICustomMetadataInfo {
metadataFileName: string;
codeownersFileName: string;
fields: IMetadataField[];
}
export const getCustomMetadataInfo = (): ICustomMetadataInfo => {
const rushConfiguration: RushConfiguration = loadRushConfiguration();

const pluginOptionsJsonFilePath: string = path.join(
Expand All @@ -24,6 +34,7 @@ export const getCustomMetadataInfo = (): IPluginConfig => {

// Custom configurations for plugin
let metadataRelativeFolder: string = getDefaultMetadataFileName();
let codeownersFileName: string = getDefaultCodeownersFileName();
let customFields: IMetadataField[] = [];

let metaConfigs: IPluginConfig | undefined;
Expand All @@ -38,15 +49,17 @@ export const getCustomMetadataInfo = (): IPluginConfig => {
if (metaConfigs.metadataFileName) {
metadataRelativeFolder = metaConfigs.metadataFileName;
}
if (metaConfigs.codeownersFileName) {
codeownersFileName = metaConfigs.codeownersFileName;
}
if (metaConfigs.fields) {
customFields = metaConfigs.fields;
}
}

console.log('custom metadata folder: ', metadataRelativeFolder);

return {
metadataFileName: metadataRelativeFolder,
codeownersFileName,
fields: customFields
};
};
Expand Down
14 changes: 11 additions & 3 deletions rush-plugins/rush-metadata-plugin/src/syncMeta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@ import { loadRushConfiguration } from '../logic/rushConfiguration';
import { getCustomMetadataInfo } from '../logic/customMeta';
import { ICoreMetadata } from '../template';
import { syncMetadataFile } from './syncMetadataFile';
import { outputToCodeowners } from '../transformers/outputToCodeowners';

// Used to sync all the metadata files in the monorepo according to the metadata spec
export const syncMeta = async (): Promise<void> => {
export const syncMeta = async ({ codeowners }: { codeowners: boolean }): Promise<void> => {
const rushConfiguration: RushConfiguration = loadRushConfiguration();
for (const rushProject of rushConfiguration.projects) {
log('rush project: ', rushProject.projectFolder);

// Look for custom plugin configurations
const { metadataFileName } = getCustomMetadataInfo();
const { metadataFileName, codeownersFileName } = getCustomMetadataInfo();

// Check if metadata file exists already
const metaFilePath: string = path.join(rushProject.projectFolder, metadataFileName);
let newMetadataFile: any;
if (fs.existsSync(metaFilePath)) {
// Read and parse the file
const loadedJsonFile: ICoreMetadata = JsonFile.load(metaFilePath);

log(chalk.green(`Updating metadata file at: ${metaFilePath}`));

const newMetadataFile: any = syncMetadataFile(loadedJsonFile);
newMetadataFile = syncMetadataFile(loadedJsonFile);

JsonFile.save(newMetadataFile, metaFilePath, { updateExistingFile: true });

if (codeowners) {
// Sync this project's POCs to the project's codeowners file
const codeownersAbsoluteFilePath: string = path.join(rushProject.projectFolder, codeownersFileName);
outputToCodeowners(newMetadataFile.pointOfContact, codeownersAbsoluteFilePath);
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const outputToCodeowners = (pointsOfContact: string[], outputFileLocation: string): void => {
// Output to gitlab format codeowners

console.log('outputting these POCs to the codeowners file: ', pointsOfContact);
console.log('at location: ', outputFileLocation);
};
7 changes: 4 additions & 3 deletions rush-plugins/rush-metadata-plugin/src/types/pluginConfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IMetadataField } from "./metadataField";
import { IMetadataField } from './metadataField';

export interface IPluginConfig {
metadataFileName: string;
metadataFileName?: string;
codeownersFileName?: string;
// Link to the custom schema for this metadata object (generated by this plugin)
metadataSchema?: string;
fields: IMetadataField[];
}
}

0 comments on commit 585b41f

Please sign in to comment.