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
3 changes: 3 additions & 0 deletions .github/workflows/argos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:
# We want trigger workflow on labeled too!
- labeled

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
Expand Down
44 changes: 0 additions & 44 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/continuous-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
- docusaurus-v**
pull_request:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
Expand Down
2 changes: 2 additions & 0 deletions .oxfmtrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"packages/docusaurus-*/lib/*",

"packages/create-docusaurus/lib/*",
"!packages/create-docusaurus/src/**",

"!packages/docusaurus-*/lib/theme/**",

"packages/create-docusaurus/templates/*/docusaurus.config.js",
Expand Down
1 change: 1 addition & 0 deletions packages/create-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"tslib": "^2.6.0"
},
"devDependencies": {
"@types/cross-spawn": "^6.0.6",
"@types/supports-color": "^10.0.0"
},
"engines": {
Expand Down
115 changes: 115 additions & 0 deletions packages/create-docusaurus/src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* 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 cross-spawn instead of spawn because of Windows compatibility issues.
// For example, "yarn" doesn't work on Windows, it requires "yarn.cmd"
// Tools like execa() use cross-spawn under the hood, and "resolve" the command
import crossSpawn from 'cross-spawn';
import supportsColor from 'supports-color';
import {
PackageManagers,
type PackageManager,
type GitCloneStrategy,
type Source,
} from './constants.js';
import {askForCustomGitCloneCommand} from './prompts.js';

// This is the same as node's child_process.SpawnOptions type, but extract from
// cross-spawn directly to ensure direct compatibility.
type SpawnOptions = NonNullable<Parameters<typeof crossSpawn>[2]>;

/**
* Run a command, similar to execa(cmd,args) but simpler
* @param command
* @param args
* @param options
* @returns the command exit code
*/
async function runCommand(
command: string,
args: string[] = [],
options: SpawnOptions = {},
): Promise<number> {
// This does something similar to execa.command()
// we split a string command (with optional args) into command+args
// this way it's compatible with spawn()
const [realCommand, ...baseArgs] = command.split(' ');
const allArgs = [...baseArgs, ...args];
if (!realCommand) {
throw new Error(`Invalid command: ${command}`);
}

return new Promise<number>((resolve, reject) => {
const p = crossSpawn(realCommand, allArgs, {stdio: 'ignore', ...options});
p.on('error', reject);
p.on('close', (exitCode) =>
exitCode !== null
? resolve(exitCode)
: reject(new Error(`No exit code for command ${command}`)),
);
});
}

async function hasPackageManager(
packageManager: PackageManager,
): Promise<boolean> {
return (await runCommand(packageManager, ['--version'])) === 0;
}

export async function getAvailablePackageManagers(): Promise<PackageManager[]> {
const list = await Promise.all(
PackageManagers.map(async (name) => {
return (await hasPackageManager(name)) ? name : null;
}),
);
return list.filter((item) => item !== null);
}

export async function runPackageManagerInstallCommand(
pkgManager: PackageManager,
): Promise<boolean> {
const installCommand =
pkgManager === 'yarn'
? 'yarn'
: pkgManager === 'bun'
? 'bun install'
: `${pkgManager} install --color always`;

return (
(await runCommand(installCommand, [], {
env: {
...process.env,
// Force coloring the output
...(supportsColor.stdout ? {FORCE_COLOR: '1'} : {}),
},
})) === 0
);
}

async function getGitCloneCommand(
gitStrategy: GitCloneStrategy,
): Promise<string> {
switch (gitStrategy) {
case 'shallow':
case 'copy':
return 'git clone --recursive --depth 1';
case 'custom': {
return askForCustomGitCloneCommand();
}
case 'deep':
default:
return 'git clone';
}
}

export async function runGitCloneCommand(
source: Source & {type: 'git'},
dest: string,
): Promise<boolean> {
const gitCommand = await getGitCloneCommand(source.strategy);
return (await runCommand(gitCommand, [source.url, dest])) === 0;
}
53 changes: 53 additions & 0 deletions packages/create-docusaurus/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 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.
*/

// Only used in the rare, rare case of running globally installed create +
// using --skip-install. We need a default name to show the tip text
export const DefaultPackageManager = 'npm';

// Order matters
export const LockfileNames = {
npm: 'package-lock.json',
yarn: 'yarn.lock',
pnpm: 'pnpm-lock.yaml',
bun: 'bun.lockb',
};

export type PackageManager = keyof typeof LockfileNames;

export const PackageManagers = Object.keys(LockfileNames) as PackageManager[];

export const GitCloneStrategies = [
'deep',
'shallow',
'copy',
'custom',
] as const;

export type GitCloneStrategy = (typeof GitCloneStrategies)[number];

export type Template = {
name: string;
path: string;
tsVariantPath: string | undefined;
};

export type Source =
| {
type: 'template';
template: Template;
language: 'javascript' | 'typescript';
}
| {
type: 'git';
url: string;
strategy: GitCloneStrategy;
}
| {
type: 'local';
path: string;
};
Loading
Loading