-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da2be1e
commit 8a21bda
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!`)); |