Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 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 {
formatTargetDir,
getProjectDirFromPackageName,
renameFiles,
setLibraryGreeting,
shouldConfigureEditorsForCreate,
} from '../utils.js';

Expand Down Expand Up @@ -404,3 +405,35 @@ describe('renameFiles', () => {
expect(read('_foo')).toBe('bar\n');
});
});

describe('setLibraryGreeting', () => {
let tmpDir: string;

beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-library-greeting-'));
});

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

it('rebrands the greeting in the source and test files', () => {
fs.mkdirSync(path.join(tmpDir, 'src'));
fs.mkdirSync(path.join(tmpDir, 'tests'));
fs.writeFileSync(path.join(tmpDir, 'src/index.ts'), 'return "Hello, tsdown!";\n');
fs.writeFileSync(path.join(tmpDir, 'tests/index.test.ts'), 'toBe("Hello, tsdown!");\n');

setLibraryGreeting(tmpDir);

expect(fs.readFileSync(path.join(tmpDir, 'src/index.ts'), 'utf8')).toBe(
'return "Hello, vite+!";\n',
);
expect(fs.readFileSync(path.join(tmpDir, 'tests/index.test.ts'), 'utf8')).toBe(
'toBe("Hello, vite+!");\n',
);
});

it('ignores missing files', () => {
expect(() => setLibraryGreeting(tmpDir)).not.toThrow();
});
});
3 changes: 2 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 { setLibraryGreeting, setPackageName } from '../utils.ts';
import { executeGeneratorScaffold } from './generator.ts';
import { removeNestedLibraryToolConfig } from './monorepo.ts';
import { runRemoteTemplateCommand } from './remote.ts';
Expand Down Expand Up @@ -50,6 +50,7 @@ export async function executeBuiltinTemplate(
}
const fullPath = path.join(workspaceInfo.rootDir, templateInfo.targetDir);
setPackageName(fullPath, templateInfo.packageName);
setLibraryGreeting(fullPath);
if (workspaceInfo.isMonorepo) {
removeNestedLibraryToolConfig(fullPath);
}
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/create/templates/monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { templatesDir } from '../../utils/path.ts';
import { editYamlFile, readYamlFile } from '../../utils/yaml.ts';
import type { ExecutionWithProjectDir } from '../command.ts';
import { discoverTemplate } from '../discovery.ts';
import { copyDir, formatDisplayTargetDir, renameFiles, setPackageName } from '../utils.ts';
import {
copyDir,
formatDisplayTargetDir,
renameFiles,
setLibraryGreeting,
setPackageName,
} from '../utils.ts';
import { runRemoteTemplateCommand } from './remote.ts';
import { type BuiltinTemplateInfo, LibraryTemplateRepo } from './types.ts';

Expand Down Expand Up @@ -153,6 +159,7 @@ export async function executeMonorepoTemplate(
: 'utils';
const libraryProjectPath = path.join(fullPath, libraryDir);
setPackageName(libraryProjectPath, libraryPackageName);
setLibraryGreeting(libraryProjectPath);
removeNestedLibraryToolConfig(libraryProjectPath);
// Perform auto-migration on the created library
rewriteMonorepoProject(
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 @@ -203,6 +203,25 @@ export function setPackageName(projectDir: string, packageName: string) {
});
}

const LIBRARY_TEMPLATE_GREETING = { from: 'Hello, tsdown!', to: 'Hello, vite+!' } as const;

/** Rebrand the greeting the shared library template ships with (it is authored for tsdown). */
export function setLibraryGreeting(projectDir: string): void {
for (const file of ['src/index.ts', 'tests/index.test.ts']) {
const filePath = path.join(projectDir, file);
if (!fs.existsSync(filePath)) {
continue;
}
const content = fs.readFileSync(filePath, 'utf8');
if (content.includes(LIBRARY_TEMPLATE_GREETING.from)) {
fs.writeFileSync(
filePath,
content.replaceAll(LIBRARY_TEMPLATE_GREETING.from, LIBRARY_TEMPLATE_GREETING.to),
);
}
}
}

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