Skip to content

Commit

Permalink
chore(scrips): 添加 init 脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Mar 1, 2024
1 parent da2be1e commit 8a21bda
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"internal/*"
],
"scripts": {
"project:init": "tsx scripts/init.ts",
"play:dev": "pnpm -C internal/playground start",
"build:rollup": "tsx scripts/build.ts -t",
"eject": "react-scripts eject",
Expand Down
57 changes: 57 additions & 0 deletions scripts/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getGitUrl, cmdGet } from './utils';
import rootPkg from '../package.json';
import { prompt } from 'enquirer';
import * as Path from 'path';
import Fse from 'fs-extra';
import chalk from 'chalk';

const rootDir = Path.resolve(__dirname, '../');

const config = {
projectName: '',
mainPkgName: '',
};

async function start() {
await Steps.query();
Steps.initPackageJson();
Steps.initDumirc();
}

const Steps = {
async query() {
const dirName = Path.basename(rootDir);
({ name: config.projectName } = await prompt<{ name: string }>({
message: '项目名称: ',
initial: dirName,
type: 'input',
name: 'name',
}));
({ name: config.mainPkgName } = await prompt<{ name: string }>({
initial: dirName,
message: '主包名: ',
type: 'input',
name: 'name',
}));
},
initPackageJson() {
rootPkg.name = config.projectName;
rootPkg.repository.url = getGitUrl();
rootPkg.author = cmdGet('git config user.name');
Fse.writeFileSync(
Path.resolve(rootDir, 'package.json'),
JSON.stringify(rootPkg, null, 2) + '\n',
);
},
initDumirc() {
const p = Path.resolve(rootDir, '.dumirc.ts');
let content = Fse.readFileSync(p, 'utf-8');
content = content.replace(
/\s{4}name: '([\w\-@\/]+)'/,
` name: '${config.mainPkgName}'`,
);
Fse.writeFileSync(p, content);
},
};

start().then(() => console.log(chalk.cyan`pkg init success!`));

0 comments on commit 8a21bda

Please sign in to comment.