Skip to content

Commit 96b0ffe

Browse files
authored
[dev-too] Fix migration error for packages without metadata section (Azure#25994)
It is possible that package.json (e.g., some core packages) doesn't have metadata yet. This PR handles the case. ### Packages impacted by this PR dev-tool
1 parent cabd530 commit 96b0ffe

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

common/tools/dev-tool/src/commands/migrate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default leafCommand(commandInfo, async (options) => {
8181

8282
// We'll just default the date to Jan 1, 1970 because it's convenient to work with an always-defined date.
8383
const migrationDate = new Date(
84-
project.packageJson[METADATA_KEY].migrationDate ?? "1970-01-01T00:00:00Z"
84+
project.packageJson[METADATA_KEY]?.migrationDate ?? "1970-01-01T00:00:00Z"
8585
);
8686

8787
// Bare command with no sub-mode arguments.
@@ -230,7 +230,7 @@ async function startMigrationPass(project: ProjectInfo, migrationDate: Date): Pr
230230
}
231231

232232
log.info(`Starting migration pass for '${project.name}'.`);
233-
log.info(`Last migration: ${project.packageJson[METADATA_KEY].migrationDate ?? "never"}`);
233+
log.info(`Last migration: ${project.packageJson[METADATA_KEY]?.migrationDate ?? "never"}`);
234234

235235
return runMigrations(pending, project);
236236
}

common/tools/dev-tool/src/commands/run/testNodeJSInput.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ export const commandInfo = makeCommandInfo(
1212

1313
export default leafCommand(commandInfo, async (options) => {
1414
const projectInfo = await resolveProject(process.cwd());
15-
const defaultMochaArgs =
16-
`${
17-
projectInfo.packageJson.type === "module" ? "" : "-r esm "
18-
} --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace`;
15+
const defaultMochaArgs = `${
16+
projectInfo.packageJson.type === "module" ? "" : "-r esm "
17+
} --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace`;
1918
const updatedArgs = options["--"]?.map((opt) =>
2019
opt.includes("**") && !opt.startsWith("'") && !opt.startsWith('"') ? `"${opt}"` : opt
2120
);

common/tools/dev-tool/src/migrations/sample-config-to-metadata.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export default createMigration(
1616
{
1717
async isApplicable(ctx) {
1818
// This migration is only applicable to client packages.
19-
return ctx.project.packageJson["sdk-type"] === "client";
19+
return (
20+
ctx.project.packageJson["sdk-type"] === "client" &&
21+
ctx.project.packageJson["//sampleConfiguration"] !== undefined
22+
);
2023
},
2124

2225
async validate(ctx) {
@@ -33,9 +36,12 @@ export default createMigration(
3336

3437
const packageJson = JSON.parse((await readFile(packageJsonPath)).toString("utf-8")) as {
3538
"//sampleConfiguration"?: SampleConfiguration;
36-
[METADATA_KEY]: AzureSdkMetadata;
39+
[METADATA_KEY]?: AzureSdkMetadata;
3740
};
3841

42+
if (!packageJson[METADATA_KEY]) {
43+
packageJson[METADATA_KEY] = {};
44+
}
3945
if (packageJson["//sampleConfiguration"]) {
4046
packageJson[METADATA_KEY].sampleConfiguration = packageJson["//sampleConfiguration"];
4147

common/tools/dev-tool/src/util/migrations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,11 @@ export async function updateMigrationDate(
599599
const packageJson = JSON.parse((await readFile(packageJsonPath)).toString("utf-8"));
600600

601601
// Defensively check that the current date in package.json is undefined or older than the new date.
602+
if (!packageJson[METADATA_KEY]) {
603+
packageJson[METADATA_KEY] = {};
604+
}
602605
if (
603-
packageJson[METADATA_KEY]?.migrationDate &&
606+
packageJson[METADATA_KEY].migrationDate &&
604607
new Date(packageJson[METADATA_KEY].migrationDate) >= migration.date
605608
) {
606609
panic(`${project.name} is being migrated to an older version than the current version.`);

common/tools/dev-tool/src/util/resolveProject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ declare global {
5555
dependencies: Record<string, string>;
5656
devDependencies: Record<string, string>;
5757

58-
[METADATA_KEY]: AzureSdkMetadata;
58+
[METADATA_KEY]?: AzureSdkMetadata;
5959
}
6060
}
6161

@@ -78,7 +78,7 @@ export interface AzureSdkMetadata {
7878
/**
7979
* Paths that contain instances of the package's version number that should be updated automatically.
8080
*/
81-
constantPaths: Array<{
81+
constantPaths?: Array<{
8282
/** The path to the containing file. */
8383
path: string;
8484
/** A line prefix to match */

0 commit comments

Comments
 (0)