Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/cli/src/create/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getProjectDirFromPackageName,
normalizeEditorOption,
renameFiles,
removeSrcOnlyTsconfigInclude,
shouldConfigureEditorsForCreate,
} from '../utils.js';

Expand Down Expand Up @@ -124,6 +125,61 @@ describe('deriveDefaultPackageName', () => {
});
});

describe('removeSrcOnlyTsconfigInclude', () => {
let projectDir: string;

beforeEach(() => {
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-tsconfig-include-'));
});

afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true });
});

function writeTsconfig(tsconfig: unknown): void {
fs.writeFileSync(path.join(projectDir, 'tsconfig.json'), JSON.stringify(tsconfig, null, 2));
}

function readTsconfig(): Record<string, unknown> {
return JSON.parse(fs.readFileSync(path.join(projectDir, 'tsconfig.json'), 'utf-8'));
}

it('removes the default src-only include', () => {
writeTsconfig({
compilerOptions: {
strict: true,
},
include: ['src'],
});

removeSrcOnlyTsconfigInclude(projectDir);

expect(readTsconfig()).toEqual({
compilerOptions: {
strict: true,
},
});
});

it('keeps custom include patterns', () => {
const tsconfig = {
compilerOptions: {
strict: true,
},
include: ['src', 'tests'],
};
writeTsconfig(tsconfig);

removeSrcOnlyTsconfigInclude(projectDir);

expect(readTsconfig()).toEqual(tsconfig);
});

it('ignores projects without a tsconfig', () => {
expect(() => removeSrcOnlyTsconfigInclude(projectDir)).not.toThrow();
});
});

describe('ensureGitignoreNodeModules', () => {
let projectDir: string;

Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/create/templates/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import colors from 'picocolors';
import type { WorkspaceInfo } from '../../types/index.ts';
import type { ExecutionWithProjectDir } from '../command.ts';
import { discoverTemplate } from '../discovery.ts';
import { setPackageName } from '../utils.ts';
import { removeSrcOnlyTsconfigInclude, setPackageName } from '../utils.ts';
import { executeGeneratorScaffold } from './generator.ts';
import { runRemoteTemplateCommand } from './remote.ts';
import { BuiltinTemplate, type BuiltinTemplateInfo, LibraryTemplateRepo } from './types.ts';
Expand Down Expand Up @@ -49,6 +49,7 @@ export async function executeBuiltinTemplate(
}
const fullPath = path.join(workspaceInfo.rootDir, templateInfo.targetDir);
setPackageName(fullPath, templateInfo.packageName);
removeSrcOnlyTsconfigInclude(fullPath);
return { ...result, projectDir: templateInfo.targetDir };
}

Expand Down Expand Up @@ -76,6 +77,9 @@ export async function executeBuiltinTemplate(
const fullPath = path.join(workspaceInfo.rootDir, templateInfo.targetDir);
// set package name in the project directory
setPackageName(fullPath, templateInfo.packageName);
if (templateInfo.command === 'create-vite@latest') {
removeSrcOnlyTsconfigInclude(fullPath);
}

return {
...result,
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/create/templates/monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { editJsonFile } from '../../utils/json.ts';
import { templatesDir } from '../../utils/path.ts';
import type { ExecutionWithProjectDir } from '../command.ts';
import { discoverTemplate } from '../discovery.ts';
import { copyDir, formatDisplayTargetDir, renameFiles, setPackageName } from '../utils.ts';
import {
copyDir,
formatDisplayTargetDir,
removeSrcOnlyTsconfigInclude,
renameFiles,
setPackageName,
} from '../utils.ts';
import { runRemoteTemplateCommand } from './remote.ts';
import { type BuiltinTemplateInfo, LibraryTemplateRepo } from './types.ts';

Expand Down Expand Up @@ -116,6 +122,7 @@ export async function executeMonorepoTemplate(
: 'website';
const appProjectPath = path.join(fullPath, InitialMonorepoAppDir);
setPackageName(appProjectPath, appPackageName);
removeSrcOnlyTsconfigInclude(appProjectPath);
// Perform auto-migration on the created app
rewriteMonorepoProject(
appProjectPath,
Expand Down Expand Up @@ -150,6 +157,7 @@ export async function executeMonorepoTemplate(
: 'utils';
const libraryProjectPath = path.join(fullPath, libraryDir);
setPackageName(libraryProjectPath, libraryPackageName);
removeSrcOnlyTsconfigInclude(libraryProjectPath);
// Perform auto-migration on the created library
rewriteMonorepoProject(
libraryProjectPath,
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/create/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ export function setPackageName(projectDir: string, packageName: string) {
});
}

export function removeSrcOnlyTsconfigInclude(projectDir: string): void {
const tsconfigPath = path.join(projectDir, 'tsconfig.json');
if (!fs.existsSync(tsconfigPath)) {
return;
}

editJsonFile<{ include?: unknown }>(tsconfigPath, (tsconfig) => {
Comment thread
wyf027 marked this conversation as resolved.
Outdated
if (
Array.isArray(tsconfig.include) &&
tsconfig.include.length === 1 &&
tsconfig.include[0] === 'src'
) {
delete tsconfig.include;
return tsconfig;
}
return undefined;
});
}

const RENAME_FILES = {
_gitignore: '.gitignore',
_npmrc: '.npmrc',
Expand Down
Loading