Skip to content

Commit

Permalink
tsp init to automatically run tsp install (#5824)
Browse files Browse the repository at this point in the history
Close #5766.

Per discussion from #5796 and
Teams, this PR:

- automatically runs `tsp install` as part of `tsp init` if the template
created a `package.json` file
- skips running or mentioning `tsp install` if `package.json` is not
created
- does NOT provide a flag for opt in/out
- does NOT provide a confirmation dialogue


![image](https://github.com/user-attachments/assets/53a656d8-c795-419b-92a2-034883a94bcc)
  • Loading branch information
tjprescott authored Jan 31, 2025
1 parent 12f1b2d commit fcb4421
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/tspInitUpdate-2025-0-31-9-41-10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

`tsp init` will not automatically run `tsp install` if a `package.json` file is created.
23 changes: 19 additions & 4 deletions packages/compiler/src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { readdir } from "fs/promises";
import pc from "picocolors";
import prompts from "prompts";
import * as semver from "semver";
import { CliCompilerHost } from "../core/cli/types.js";
import { installTypeSpecDependencies } from "../core/install.js";
import { createDiagnostic } from "../core/messages.js";
import { getBaseFileName, getDirectoryPath } from "../core/path-utils.js";
import { CompilerHost, Diagnostic, NoTarget, SourceFile } from "../core/types.js";
Expand All @@ -10,15 +12,20 @@ import { readUrlOrPath } from "../utils/misc.js";
import { getTypeSpecCoreTemplates } from "./core-templates.js";
import { validateTemplateDefinitions, ValidationResult } from "./init-template-validate.js";
import { EmitterTemplate, InitTemplate, InitTemplateLibrarySpec } from "./init-template.js";
import { makeScaffoldingConfig, normalizeLibrary, scaffoldNewProject } from "./scaffold.js";
import {
isFileSkipGeneration,
makeScaffoldingConfig,
normalizeLibrary,
scaffoldNewProject,
} from "./scaffold.js";

export interface InitTypeSpecProjectOptions {
templatesUrl?: string;
template?: string;
}

export async function initTypeSpecProject(
host: CompilerHost,
host: CliCompilerHost,
directory: string,
options: InitTypeSpecProjectOptions = {},
) {
Expand Down Expand Up @@ -80,15 +87,23 @@ export async function initTypeSpecProject(
});

await scaffoldNewProject(host, scaffoldingConfig);
const projectJsonCreated = !isFileSkipGeneration(
"package.json",
scaffoldingConfig.template.files ?? [],
);

// eslint-disable-next-line no-console
console.log("");
// eslint-disable-next-line no-console
console.log("TypeSpec init completed. You can run `tsp install` now to install dependencies.");

// eslint-disable-next-line no-console
console.log(pc.green("Project created successfully."));

if (projectJsonCreated) {
// eslint-disable-next-line no-console
console.log(pc.green("Installing dependencies..."));
await installTypeSpecDependencies(host, directory);
}

if (Object.values(emitters).some((emitter) => emitter.message !== undefined)) {
// eslint-disable-next-line no-console
console.log(pc.yellow("\nPlease review the following messages from emitters:"));
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/init/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function scaffoldNewProject(host: CompilerHost, config: Scaffolding
await writeFiles(host, config);
}

function isFileSkipGeneration(fileName: string, files: InitTemplateFile[]): boolean {
export function isFileSkipGeneration(fileName: string, files: InitTemplateFile[]): boolean {
for (const file of files) {
if (file.destination === fileName) {
return file.skipGeneration ?? false;
Expand Down

0 comments on commit fcb4421

Please sign in to comment.