Skip to content

Commit

Permalink
feat: add create-rspress
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Aug 24, 2023
1 parent a019959 commit a3e7d09
Show file tree
Hide file tree
Showing 24 changed files with 414 additions and 5 deletions.
2 changes: 1 addition & 1 deletion e2e/fixtures/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-doc-template",
"name": "rspress-doc-template",
"version": "1.0.0",
"private": true,
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions packages/create-rspress/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line import/no-commonjs
module.exports = {
root: true,
extends: ['@modern-js-app'],
ignorePatterns: ['**/template/**'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
};
48 changes: 48 additions & 0 deletions packages/create-rspress/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.DS_Store

.pnp
.pnp.js
.env.local
.env.*.local
.history
.rts*
*.log*
*.pid
*.pid.*
*.report
*.lcov
lib-cov

node_modules/
.npm
.lock-wscript
.yarn-integrity
.node_repl_history
.nyc_output
*.tsbuildinfo
.eslintcache
.sonarlint

dist/
coverage/
release/
output/
output_resource/
log/

.vscode/**/*
!.vscode/settings.json
!.vscode/extensions.json
.idea/

**/*/typings/auto-generated

.changeset/pre.json

.pnpm-store/

modern.config.local.ts
modern.config.local.js
modern.config.local.mjs

doc_build/
37 changes: 37 additions & 0 deletions packages/create-rspress/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.DS_Store

.pnp
.pnp.js
.env.local
.env.*.local
.history
.rts*
*.log*
*.pid
*.pid.*
*.report
*.lcov
lib-cov

node_modules/
.npm
.lock-wscript
.yarn-integrity
.node_repl_history
.nyc_output
*.tsbuildinfo
.eslintcache
.sonarlint

coverage/
release/
output/
output_resource/
tests/

.vscode/**/*
!.vscode/settings.json
!.vscode/extensions.json
.idea/

**/*/api/typings/auto-generated
1 change: 1 addition & 0 deletions packages/create-rspress/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
strict-peer-dependencies=false
1 change: 1 addition & 0 deletions packages/create-rspress/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/gallium
5 changes: 5 additions & 0 deletions packages/create-rspress/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "avoid"
}
21 changes: 21 additions & 0 deletions packages/create-rspress/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present Bytedance, Inc. and its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/create-rspress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# create-rspress

The scaffolding tool to create a new rspress project.
8 changes: 8 additions & 0 deletions packages/create-rspress/modern.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { moduleTools, defineConfig } from '@modern-js/module-tools';

export default defineConfig({
plugins: [moduleTools()],
buildConfig: {
target: 'esnext',
},
});
48 changes: 48 additions & 0 deletions packages/create-rspress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "create-rspress",
"version": "0.1.0",
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
"module": "./dist/index.js",
"bin": {
"create-rspress": "./dist/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/web-infra-dev/rspress",
"directory": "packages/create-rspress"
},
"scripts": {
"dev": "modern build -w",
"build": "modern build",
"build:watch": "modern build -w",
"test": "vitest run",
"reset": "rimraf ./**/node_modules",
"new": "modern new",
"upgrade": "modern upgrade"
},
"devDependencies": {
"@types/jest": "~29.2.4",
"@types/node": "~16.11.7",
"@types/prompts": "^2.4.4",
"@types/react": "~18.0.26",
"@types/fs-extra": "11.0.1",
"rimraf": "~3.0.2",
"ts-node": "^10.9.1",
"typescript": "~5.0.4",
"cac": "6.7.14",
"chalk": "5.3.0",
"fs-extra": "11.1.1",
"prompts": "2.4.1"
},
"sideEffects": [],
"publishConfig": {
"access": "public",
"provenance": true,
"registry": "https://registry.npmjs.org/"
},
"files": [
"dist",
"template"
]
}
9 changes: 9 additions & 0 deletions packages/create-rspress/src/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// eslint-disable-next-line import/no-commonjs
module.exports = {
root: true,
extends: ['@modern-js-app'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['../tsconfig.json'],
},
};
46 changes: 46 additions & 0 deletions packages/create-rspress/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node
import path from 'path';
import { cac } from 'cac';
import prompts from 'prompts';
import fs from 'fs-extra';
import { copyFolder, formatTargetDir, getPkgManager } from './utils';

const cli = cac('create-rspress').help();

cli.command('', 'Create a new rspress site').action(async () => {
const defaultProjectName = 'rspress-site';
let targetDir = defaultProjectName;
const promptProjectDir = async () =>
await prompts([
{
type: 'text',
name: 'projectDir',
initial: defaultProjectName,
message: 'Project folder',
onState: state => {
targetDir = formatTargetDir(state.value) || defaultProjectName;
},
},
]);

await promptProjectDir();
let root = path.resolve(process.cwd(), targetDir);
while (fs.existsSync(root)) {
console.log(
`${targetDir} is not empty, please choose another project name`,
);
await promptProjectDir();
root = path.resolve(process.cwd(), targetDir);
}

await fs.mkdir(root, { recursive: true });
const srcFolder = path.resolve(__dirname, '../template');
await copyFolder(srcFolder, targetDir);
const pkgManager = getPkgManager();
console.log('\nDone. Now run:\n');
console.log(`cd ${targetDir}\n`);
console.log(`${pkgManager} install\n`);
console.log(`${pkgManager} run dev\n`);
});

cli.parse();
2 changes: 2 additions & 0 deletions packages/create-rspress/src/modern-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types='@modern-js/module-tools/types' />
/// <reference types='@modern-js/plugin-testing/types' />
39 changes: 39 additions & 0 deletions packages/create-rspress/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import path from 'path';
import fs from 'fs-extra';

export async function copyFolder(src: string, dest: string) {
const renameFiles: Record<string, string> = {
_gitignore: '.gitignore',
};

await fs.mkdir(dest, { recursive: true });
for (const file of await fs.readdir(src)) {
const srcFile = path.resolve(src, file);
const dstFile = renameFiles[file]
? path.resolve(dest, renameFiles[file])
: path.resolve(dest, file);
const stat = await fs.stat(srcFile);
if (stat.isDirectory()) {
await copyFolder(srcFile, dstFile);
} else {
await fs.copyFile(srcFile, dstFile);
}
}
}

export function getPkgManager() {
const ua = process.env.npm_config_user_agent;
if (!ua) {
return 'npm';
}
const [pkgInfo] = ua.split(' ');
const [name] = pkgInfo.split('/');
return name || 'npm';
}

/**
* format the targetDir
*/
export function formatTargetDir(targetDir: string) {
return targetDir.trim().replace(/\/+$/g, '');
}
25 changes: 25 additions & 0 deletions packages/create-rspress/template/_gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
doc_build
1 change: 1 addition & 0 deletions packages/create-rspress/template/doc/guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Guide
1 change: 1 addition & 0 deletions packages/create-rspress/template/doc/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello World
18 changes: 18 additions & 0 deletions packages/create-rspress/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "rspress-doc-template",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "rspress dev",
"build": "rspress build",
"preview": "rspress preview"
},
"dependencies": {
"rspress": "latest"
},
"devDependencies": {
"@modern-js/tsconfig": "2.30.0",
"@types/jest": "^29",
"@types/node": "^14"
}
}
6 changes: 6 additions & 0 deletions packages/create-rspress/template/rspress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as path from 'path';
import { defineConfig } from 'rspress/config';

export default defineConfig({
root: path.join(__dirname, 'doc'),
});
6 changes: 6 additions & 0 deletions packages/create-rspress/template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"esModuleInterop": true
}
}
15 changes: 15 additions & 0 deletions packages/create-rspress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "@modern-js/tsconfig/base",
"declaration": true,
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
},
"isolatedModules": true
},
"include": ["src", "./modern.config.ts"],
"exclude": ["**/node_modules", "**/template/**"]
}
Loading

0 comments on commit a3e7d09

Please sign in to comment.