microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a9bc288cda0d8226ce647df708ecca5fb0c4a893

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/migrate/src/cli.ts

134lines · modecode

1#!/usr/bin/env node
2
3/* eslint-disable no-console */
4import { MANIFEST, NodePackage, resolvePath } from "@typespec/compiler";
5import * as fs from "fs";
6import { readFile } from "fs/promises";
7import * as semver from "semver";
8import yargs from "yargs";
9import { migrationConfigurations } from "./migration-config.js";
10import {
11 migrateFileRename,
12 migratePackageVersion,
13 migrateTextFiles,
14 migrateTypeSpecFiles,
15} from "./migration-impl.js";
16import { MigrationKind } from "./migration-types.js";
17import { findTypeSpecFiles } from "./utils.js";
18
19interface Options {
20 path: string;
21 tspVersion: string;
22}
23
24async function main() {
25 console.log(`TypeSpec migration tool v${MANIFEST.version}\n`);
26
27 const cliOptions: Options = await yargs(process.argv.slice(2))
28 .option("path", {
29 alias: "p",
30 describe: "Path to the input directory. Defaults to the current directory.",
31 type: "string",
32 default: process.cwd(),
33 })
34 .option("tspVersion", {
35 alias: "t",
36 describe:
37 "Specifies the TypeSpec compiler version used by the input. Defaults to the version of the compiler package in package.json.",
38 type: "string",
39 default: "",
40 })
41 .help().argv;
42
43 const PackageJsonFile = "package.json";
44 if (cliOptions.tspVersion.length === 0) {
45 // Locate current package.json
46 const pkgFile = resolvePath(cliOptions.path, PackageJsonFile);
47 const packageJson: NodePackage = JSON.parse(await readFile(pkgFile, "utf-8"));
48
49 // Locate current compiler version
50 const CadlCompiler = "@cadl-lang/compiler";
51 const TypeSpecCompiler = "@typespec/compiler";
52 if (
53 packageJson?.devDependencies !== undefined &&
54 packageJson?.devDependencies[CadlCompiler] !== undefined
55 ) {
56 cliOptions.tspVersion = packageJson.devDependencies[CadlCompiler];
57 } else if (
58 packageJson?.devDependencies !== undefined &&
59 packageJson?.devDependencies[TypeSpecCompiler] !== undefined
60 ) {
61 cliOptions.tspVersion = packageJson.devDependencies[TypeSpecCompiler];
62 } else {
63 console.error("Unable to find TypeSpec compiler version in package.json.");
64 return;
65 }
66 }
67
68 if (!fs.existsSync(cliOptions.path)) {
69 console.error(`Path not found. ${cliOptions.path}`);
70 return;
71 }
72
73 let changesMade = false;
74
75 // Iterate thru migration configuration and invoke migration functions
76 console.log(`Current Typespec version ${cliOptions.tspVersion}.`);
77 const stepKeys = Object.keys(migrationConfigurations);
78 for (const key of stepKeys) {
79 if (semver.gt(key, cliOptions.tspVersion)) {
80 console.log(
81 `Migration step found to upgrade from ${cliOptions.tspVersion} to ${key}. Migrating...`
82 );
83
84 for (const migrationStep of migrationConfigurations[key]) {
85 const files = await findTypeSpecFiles(cliOptions.path);
86 switch (migrationStep.kind) {
87 case MigrationKind.AstContentMigration:
88 const result = await migrateTypeSpecFiles(files, migrationStep);
89 // If migration has been performed log status
90 if (result.filesChanged.length > 0) {
91 changesMade = true;
92 console.log(`Updated ${result.filesChanged.length} TypeSpec files:`);
93 for (const file of result.filesChanged) {
94 console.log(` - ${file}`);
95 }
96 }
97 break;
98 case MigrationKind.FileContentMigration:
99 changesMade = await migrateTextFiles(files, migrationStep);
100 break;
101 case MigrationKind.FileRename:
102 changesMade = await migrateFileRename(files, migrationStep);
103 break;
104 case MigrationKind.PackageVersionUpdate:
105 const pkgFile = resolvePath(cliOptions.path, PackageJsonFile);
106 changesMade = await migratePackageVersion(pkgFile, migrationStep);
107 break;
108 default:
109 console.log(`Unexpected error: unknown migration kind: ${migrationStep} `);
110 }
111 }
112
113 cliOptions.tspVersion = key;
114 } else {
115 console.log(
116 `${cliOptions.tspVersion} is already greater than or equal to ${key}. Migration step skipped...`
117 );
118 }
119 }
120
121 if (changesMade) {
122 console.log(
123 "\nThis is a best effort migration, double check that everything was migrated correctly."
124 );
125 } else {
126 console.log("\nNo typespec files have been migrated.");
127 }
128}
129
130main().catch((e) => {
131 // eslint-disable-next-line no-console
132 console.error(e);
133 process.exit(1);
134});
135