Updates tsp init to generate .gitignore file (#3751)
Fixes https://github.com/microsoft/typespec/issues/3262 This adds a new confirmation prompt to `tsp init` to generate a .gitignore file. Defaults to yes (true). --------- Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
This commit is contained in:
Родитель
4553419b75
Коммит
1cc570c72f
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
changeKind: feature
|
||||
packages:
|
||||
- "@typespec/compiler"
|
||||
---
|
||||
|
||||
Adds option to `tsp init` to generate .gitignore file
|
|
@ -49,13 +49,19 @@ export async function initTypeSpecProject(
|
|||
// eslint-disable-next-line no-console
|
||||
console.log(template.description);
|
||||
}
|
||||
const { name } = await prompts([
|
||||
const { name, includeGitignore } = await prompts([
|
||||
{
|
||||
type: "text",
|
||||
name: "name",
|
||||
message: `Project name`,
|
||||
initial: folderName,
|
||||
},
|
||||
{
|
||||
type: "confirm",
|
||||
name: "includeGitignore",
|
||||
message: "Do you want to generate a .gitignore file?",
|
||||
initial: true,
|
||||
},
|
||||
]);
|
||||
|
||||
const libraries = await selectLibraries(template);
|
||||
|
@ -67,6 +73,7 @@ export async function initTypeSpecProject(
|
|||
directory,
|
||||
folderName,
|
||||
parameters,
|
||||
includeGitignore,
|
||||
});
|
||||
|
||||
await scaffoldNewProject(host, scaffoldingConfig);
|
||||
|
|
|
@ -42,6 +42,11 @@ export interface ScaffoldingConfig {
|
|||
*/
|
||||
libraries: InitTemplateLibrarySpec[];
|
||||
|
||||
/**
|
||||
* Whether to generate a .gitignore file.
|
||||
*/
|
||||
includeGitignore: boolean;
|
||||
|
||||
/**
|
||||
* Custom parameters provided in the tempalates.
|
||||
*/
|
||||
|
@ -67,6 +72,7 @@ export function makeScaffoldingConfig(
|
|||
directory: config.directory ?? "",
|
||||
folderName: config.folderName ?? "",
|
||||
parameters: config.parameters ?? {},
|
||||
includeGitignore: config.includeGitignore ?? true,
|
||||
...config,
|
||||
};
|
||||
}
|
||||
|
@ -81,6 +87,7 @@ export async function scaffoldNewProject(host: CompilerHost, config: Scaffolding
|
|||
await writePackageJson(host, config);
|
||||
await writeConfig(host, config);
|
||||
await writeMain(host, config);
|
||||
await writeGitignore(host, config);
|
||||
await writeFiles(host, config);
|
||||
}
|
||||
|
||||
|
@ -163,6 +170,25 @@ async function writeMain(host: CompilerHost, config: ScaffoldingConfig) {
|
|||
return host.writeFile(joinPaths(config.directory, "main.tsp"), await formatTypeSpec(content));
|
||||
}
|
||||
|
||||
const defaultGitignore = `
|
||||
# MacOS
|
||||
.DS_Store
|
||||
|
||||
# Default TypeSpec output
|
||||
tsp-output/
|
||||
dist/
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
`.trim();
|
||||
async function writeGitignore(host: CompilerHost, config: ScaffoldingConfig) {
|
||||
if (!config.includeGitignore || isFileSkipGeneration(".gitignore", config.template.files ?? [])) {
|
||||
return;
|
||||
}
|
||||
|
||||
return host.writeFile(joinPaths(config.directory, ".gitignore"), defaultGitignore);
|
||||
}
|
||||
|
||||
async function writeFiles(host: CompilerHost, config: ScaffoldingConfig) {
|
||||
const templateContext = createFileTemplatingContext(config);
|
||||
if (!config.template.files) {
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import { deepStrictEqual, strictEqual } from "assert";
|
||||
import { beforeEach, describe, it } from "vitest";
|
||||
import { InitTemplate } from "../../src/init/init-template.js";
|
||||
import { makeScaffoldingConfig, scaffoldNewProject } from "../../src/init/scaffold.js";
|
||||
import { TestHost, createTestHost, resolveVirtualPath } from "../../src/testing/index.js";
|
||||
import {
|
||||
makeScaffoldingConfig,
|
||||
ScaffoldingConfig,
|
||||
scaffoldNewProject,
|
||||
} from "../../src/init/scaffold.js";
|
||||
import { createTestHost, resolveVirtualPath, TestHost } from "../../src/testing/index.js";
|
||||
|
||||
describe("compiler: init: templates", () => {
|
||||
let testHost: TestHost;
|
||||
|
@ -14,7 +18,10 @@ describe("compiler: init: templates", () => {
|
|||
return testHost.fs.get(resolveVirtualPath(path));
|
||||
}
|
||||
|
||||
async function runTemplate(overrides: Partial<InitTemplate>): Promise<void> {
|
||||
async function runTemplate(
|
||||
overrides: Partial<InitTemplate>,
|
||||
configOverrides?: Partial<ScaffoldingConfig>
|
||||
): Promise<void> {
|
||||
const template: InitTemplate = {
|
||||
title: "Test Template",
|
||||
description: "This is only a test.",
|
||||
|
@ -26,6 +33,7 @@ describe("compiler: init: templates", () => {
|
|||
name: "test-template",
|
||||
folderName: "test-template",
|
||||
template,
|
||||
...configOverrides,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -45,4 +53,14 @@ describe("compiler: init: templates", () => {
|
|||
strictEqual(getOutputFile("main.tsp")!, 'import "foo";\nimport "bar";\n');
|
||||
});
|
||||
});
|
||||
|
||||
it("can generate .gitignore file by default", async () => {
|
||||
await runTemplate({});
|
||||
strictEqual(typeof getOutputFile(".gitignore"), "string");
|
||||
});
|
||||
|
||||
it("can exclude .gitignore file", async () => {
|
||||
await runTemplate({}, { includeGitignore: false });
|
||||
strictEqual(typeof getOutputFile(".gitignore"), "undefined");
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче