Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options.type #2112

Merged
merged 4 commits into from
Apr 3, 2025
Merged
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
1 change: 1 addition & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Each defaults to a value based on the running system, including an repository if
| `--pnpm` | `string` | pnpm version for `package.json`'s `packageManager` field | Existing value in `package.json` if it exists |
| `--repository` | `string` | Name for the new repository | The same as `--directory` |
| `--title` | `string` | 'Title Case' title for the repository | Title-cased `repository` |
| `--type` | `string` | package.json modules type | Existing value in `package.json` if it exists, or `"module"` |
| `--version` | `string` | package version to publish as and store in `package.json` | Existing value in `package.json` if it exists, or `"0.0.0"` |
| `--words` | `string[]` | additional words to add to the CSpell dictionary | Existing `words` in a `cspell.json` file if it exists, and any new words in from other options |

Expand Down
1 change: 1 addition & 0 deletions src/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe("base", () => {
pnpm: expect.any(String),
repository: "create-typescript-app",
title: "Create TypeScript App",
type: expect.any(String),
version: expect.any(String),
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-require-imports
words: require("../cspell.json").words,
Expand Down
7 changes: 7 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export const base = createBase({
.optional()
.describe("GitHub branch ruleset ID for main branch protections"),
title: z.string().describe("'Title Case' title for the repository"),
type: z
.union([z.literal("commonjs"), z.literal("module")])
.optional()
.describe("package.json modules type"),
version: z
.string()
.optional()
Expand Down Expand Up @@ -294,6 +298,8 @@ export const base = createBase({
async () => await readTitle(getReadme, getRepository),
);

const getType = lazyValue(async () => (await getPackageData()).type);

const getVersion = lazyValue(async () => (await getPackageData()).version);

const getWords = lazyValue(async () => await readWords(take));
Expand Down Expand Up @@ -324,6 +330,7 @@ export const base = createBase({
repository: getRepository,
rulesetId: getRulesetId,
title: getTitle,
type: getType,
version: getVersion,
words: getWords,
workflowsVersions: getWorkflowData,
Expand Down
246 changes: 246 additions & 0 deletions src/blocks/blockESLint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,250 @@ describe("blockESLint", () => {
}
`);
});

test("with options.type set to commonjs", () => {
const creation = testBlock(blockESLint, {
options: {
...optionsBase,
type: "commonjs",
},
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": {
"Linting": {
"contents": {
"after": [
"
For example, ESLint can be run with \`--fix\` to auto-fix some lint rule complaints:

\`\`\`shell
pnpm run lint --fix
\`\`\`
",
],
"before": "
This package includes several forms of linting to enforce consistent code quality and styling.
Each should be shown in VS Code, and can be run manually on the command-line:
",
"items": [
"- \`pnpm lint\` ([ESLint](https://eslint.org) with [typescript-eslint](https://typescript-eslint.io)): Lints JavaScript and TypeScript source files",
],
"plural": "Read the individual documentation for each linter to understand how it can be configured and used best.",
},
},
},
},
"block": [Function],
},
{
"addons": {
"jobs": [
{
"name": "Lint",
"steps": [
{
"run": "pnpm lint",
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"properties": {
"devDependencies": {
"@eslint/js": "9.22.0",
"@types/node": "22.13.10",
"eslint": "9.22.0",
"typescript-eslint": "8.26.1",
},
"scripts": {
"lint": "eslint . --max-warnings 0",
},
},
},
"block": [Function],
},
{
"addons": {
"extensions": [
"dbaeumer.vscode-eslint",
],
"settings": {
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
},
"eslint.probe": [
"javascript",
"javascriptreact",
"json",
"jsonc",
"markdown",
"typescript",
"typescriptreact",
"yaml",
],
"eslint.rules.customizations": [
{
"rule": "*",
"severity": "warn",
},
],
},
},
"block": [Function],
},
],
"files": {
"eslint.config.mjs": "import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(
{ ignores: ["lib", "node_modules", "pnpm-lock.yaml"] },
{ linterOptions: {"reportUnusedDisableDirectives":"error"} },
eslint.configs.recommended,
{ extends: [tseslint.configs.strictTypeChecked, tseslint.configs.stylisticTypeChecked], files: ["**/*.js", "**/*.ts"], languageOptions: {"parserOptions":{"projectService":{"allowDefaultProject":["*.config.*s"]},"tsconfigRootDir":import.meta.dirname}}, }
);",
},
"scripts": [
{
"commands": [
"pnpm lint --fix",
],
"phase": 3,
},
],
}
`);
});

test("with options.type set to module", () => {
const creation = testBlock(blockESLint, {
options: {
...optionsBase,
type: "module",
},
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": {
"Linting": {
"contents": {
"after": [
"
For example, ESLint can be run with \`--fix\` to auto-fix some lint rule complaints:

\`\`\`shell
pnpm run lint --fix
\`\`\`
",
],
"before": "
This package includes several forms of linting to enforce consistent code quality and styling.
Each should be shown in VS Code, and can be run manually on the command-line:
",
"items": [
"- \`pnpm lint\` ([ESLint](https://eslint.org) with [typescript-eslint](https://typescript-eslint.io)): Lints JavaScript and TypeScript source files",
],
"plural": "Read the individual documentation for each linter to understand how it can be configured and used best.",
},
},
},
},
"block": [Function],
},
{
"addons": {
"jobs": [
{
"name": "Lint",
"steps": [
{
"run": "pnpm lint",
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"properties": {
"devDependencies": {
"@eslint/js": "9.22.0",
"@types/node": "22.13.10",
"eslint": "9.22.0",
"typescript-eslint": "8.26.1",
},
"scripts": {
"lint": "eslint . --max-warnings 0",
},
},
},
"block": [Function],
},
{
"addons": {
"extensions": [
"dbaeumer.vscode-eslint",
],
"settings": {
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
},
"eslint.probe": [
"javascript",
"javascriptreact",
"json",
"jsonc",
"markdown",
"typescript",
"typescriptreact",
"yaml",
],
"eslint.rules.customizations": [
{
"rule": "*",
"severity": "warn",
},
],
},
},
"block": [Function],
},
],
"files": {
"eslint.config.js": "import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(
{ ignores: ["lib", "node_modules", "pnpm-lock.yaml"] },
{ linterOptions: {"reportUnusedDisableDirectives":"error"} },
eslint.configs.recommended,
{ extends: [tseslint.configs.strictTypeChecked, tseslint.configs.stylisticTypeChecked], files: ["**/*.js", "**/*.ts"], languageOptions: {"parserOptions":{"projectService":{"allowDefaultProject":["*.config.*s"]},"tsconfigRootDir":import.meta.dirname}}, }
);",
},
"scripts": [
{
"commands": [
"pnpm lint --fix",
],
"phase": 3,
},
],
}
`);
});
});
5 changes: 4 additions & 1 deletion src/blocks/blockESLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export const blockESLint = base.createBlock({
const { explanations, extensions, ignores, imports, rules, settings } =
addons;

const configFileName =
options.type === "commonjs" ? "eslint.config.mjs" : "eslint.config.js";

const explanation =
explanations.length > 0
? `${explanations
Expand Down Expand Up @@ -207,7 +210,7 @@ Each should be shown in VS Code, and can be run manually on the command-line:
}),
],
files: {
"eslint.config.js": `${explanation}${importLines.join("\n")}
[configFileName]: `${explanation}${importLines.join("\n")}

export default tseslint.config(
{ ignores: [${ignoreLines.join(", ")}] },
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blockPackageJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe("blockPackageJson", () => {
expect(creation).toMatchInlineSnapshot(`
{
"files": {
"package.json": "{"name":"test-repository","version":"0.0.0","description":"A very very very very very very very very very very very very very very very very long HTML-ish description ending with an emoji. 🧵","repository":{"type":"git","url":"git+https://github.com/test-owner/test-repository.git"},"author":{"email":"[email protected]"},"type":"commonjs","files":["README.md","package.json"],"engines":{"node":">=20.12.0"}}",
"package.json": "{"name":"test-repository","version":"0.0.0","description":"A very very very very very very very very very very very very very very very very long HTML-ish description ending with an emoji. 🧵","repository":{"type":"git","url":"git+https://github.com/test-owner/test-repository.git"},"author":{"email":"[email protected]"},"type":"module","files":["README.md","package.json"],"engines":{"node":">=20.12.0"}}",
},
"scripts": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blockPackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const blockPackageJson = base.createBlock({
"package.json": sortPackageJson(
JSON.stringify(
removeUndefinedObjects({
type: "module",
...options.packageData,
...addons.properties,
author: { email: options.email.npm, name: options.author },
Expand Down Expand Up @@ -76,6 +75,7 @@ export const blockPackageJson = base.createBlock({
...options.packageData?.scripts,
...addons.properties.scripts,
},
type: options.type ?? "module",
version: options.version ?? "0.0.0",
}),
),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface PartialPackageData {
publishConfig?: PartialPublishConfig;
repository?: string | { type: string; url: string };
scripts?: Record<string, string>;
type?: "commonjs" | "module";
version?: string;
}

Expand Down
Loading