Skip to content
Merged
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
1 change: 0 additions & 1 deletion packages/create-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"commander": "^5.1.0",
"execa": "^5.1.1",
"fs-extra": "^11.1.1",
"lodash": "^4.17.21",
"prompts": "^2.4.2",
"semver": "^7.5.4",
"supports-color": "^9.4.0",
Expand Down
42 changes: 42 additions & 0 deletions packages/create-docusaurus/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {siteNameToPackageName} from '../utils';

describe('siteNameToPackageName', () => {
it('converts simple cases', () => {
const testCases: [string, string][] = [
['Foo Bar', 'foo-bar'],
['fooBar', 'foo-bar'],
['__FOO_BAR__', 'foo-bar'],
['XMLHttpRequest', 'xml-http-request'],
['sitemapXML', 'sitemap-xml'],
['XMLHttp', 'xml-http'],
['xml-http', 'xml-http'],
];

testCases.forEach(([input, expected]) => {
expect(siteNameToPackageName(input)).toEqual(expected);
});
});

it('converts ñ', () => {
expect(siteNameToPackageName('mañanaFoo')).toEqual('ma-ana-foo');
});

it('converts __', () => {
expect(siteNameToPackageName('foo__bar')).toEqual('foo-bar');
});

it('skips 🔥', () => {
expect(siteNameToPackageName('🔥')).toEqual('🔥');
});

it('skips !!!', () => {
expect(siteNameToPackageName('!!!')).toEqual('!!!');
});
});
14 changes: 11 additions & 3 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fs from 'fs-extra';
import {fileURLToPath} from 'url';
import path from 'path';
import _ from 'lodash';
import {logger} from '@docusaurus/logger';
import execa from 'execa';
import prompts, {type Choice} from 'prompts';
Expand All @@ -17,6 +16,7 @@ import supportsColor from 'supports-color';
// TODO remove dependency on large @docusaurus/utils
// would be better to have a new smaller @docusaurus/utils-cli package
import {askPreferredLanguage} from '@docusaurus/utils';
import {siteNameToPackageName} from './utils.js';

type LanguagesOptions = {
javascript?: boolean;
Expand Down Expand Up @@ -164,7 +164,15 @@ async function readTemplates(): Promise<Template[]> {
);

// Classic should be first in list!
return _.sortBy(templates, (t) => t.name !== recommendedTemplate);
return templates.sort((a, b) => {
if (a.name === recommendedTemplate) {
return -1;
}
if (b.name === recommendedTemplate) {
return 1;
}
return 0;
});
}

async function copyTemplate(
Expand Down Expand Up @@ -562,7 +570,7 @@ export default async function init(
// Update package.json info.
try {
await updatePkg(path.join(dest, 'package.json'), {
name: _.kebabCase(siteName),
name: siteNameToPackageName(siteName),
version: '0.0.0',
private: true,
});
Expand Down
23 changes: 23 additions & 0 deletions packages/create-docusaurus/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* We use a simple kebab-case-like conversion
* It's not perfect, but good enough
* We don't want to depend on lodash in this package
* See https://github.com/facebook/docusaurus/pull/11653
* @param siteName
*/
export function siteNameToPackageName(siteName: string): string {
const match = siteName.match(
/[A-Z]{2,}(?=[A-Z][a-z]+\d*|\b|_)|[A-Z]?[a-z]+\d*|[A-Z]|\d+/g,
);
if (match) {
return match.map((x) => x.toLowerCase()).join('-');
}
return siteName;
}
Loading