Skip to content

Commit f3af049

Browse files
2nthonylevy9527
authored andcommitted
feat: 初始化时询问输入owner
1 parent de25fbb commit f3af049

File tree

9 files changed

+48
-25
lines changed

9 files changed

+48
-25
lines changed

init.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ let pkg = {}
1515
* Prompt user for input to populate template files
1616
*/
1717
let npmName
18+
let ownerName
19+
const OWNER_NAME = 'femessage'
1820

1921
function isUpgrade() {
2022
return argv.has('u') || argv.has('upgrade')
@@ -24,17 +26,32 @@ if (isUpgrade()) {
2426
try {
2527
pkg = require(path.join(process.cwd(), 'package.json'))
2628
npmName = pkg.name.replace(/^@[\w]*\//, '')
29+
ownerName = pkg.name.replace(/^@([\w]*)\/[\w]*/, '$1')
2730
} catch {}
2831
}
2932

3033
if (argv.has('test')) {
3134
npmName = 'v-test'
35+
ownerName = OWNER_NAME
3236
}
3337

38+
const promptAngle = kleur.dim('> ')
3439
if (!npmName) {
35-
npmName = readline.question(
36-
'✍️ What is the npm name of your component? '
40+
console.log(
41+
'The component name:'
3742
)
43+
npmName = readline.prompt({
44+
prompt: promptAngle
45+
})
46+
}
47+
if (!ownerName) {
48+
console.log(
49+
`The owner: ${kleur.dim(`(${OWNER_NAME})`)}`
50+
)
51+
ownerName = readline.prompt({
52+
prompt: promptAngle,
53+
defaultInput: OWNER_NAME
54+
})
3855
}
3956

4057
const componentName = kebabcasify(npmName)
@@ -44,6 +61,7 @@ const fileActions = new FileActions({
4461
argv,
4562
pkg,
4663
componentName,
64+
ownerName,
4765
outDir,
4866
templatesDir: path.join(__dirname, 'templates')
4967
})

lib/fileActions.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = class FileActions {
1717
this.templates.forEach(filepath => {
1818
const fileName = path.relative(this.opts.templatesDir, filepath)
1919
const target = path.join(this.opts.outDir, fileName)
20-
const content = parseContent(fs.readFileSync(filepath, 'utf8'), this.opts.componentName)
20+
const content = parseContent(fs.readFileSync(filepath, 'utf8'), this.opts)
2121

2222
fs.outputFileSync(target, content)
2323

@@ -47,6 +47,7 @@ module.exports = class FileActions {
4747
upgrade(extraFiles = []) {
4848
const filesFromCli = this.opts.argv.get('files') || ''
4949

50+
// TODO: 提取
5051
const files = [
5152
'.grenrc.js',
5253
'.prettierrc',
@@ -68,7 +69,7 @@ module.exports = class FileActions {
6869
upgradeFiles.forEach(filepath => {
6970
const fileName = path.relative(this.opts.templatesDir, filepath)
7071
const target = path.join(process.cwd(), fileName)
71-
const content = parseContent(fs.readFileSync(filepath, 'utf8'), this.opts.componentName)
72+
const content = parseContent(fs.readFileSync(filepath, 'utf8'), this.opts)
7273

7374
fs.outputFileSync(target, content)
7475

@@ -78,19 +79,22 @@ module.exports = class FileActions {
7879
fs.chmodSync(path.join(process.cwd(), 'build.sh'), '755')
7980
fs.chmodSync(path.join(process.cwd(), 'notify.sh'), '755')
8081

81-
upgradePackageJson({
82-
pkg: this.opts.pkg,
83-
source: fs.readFileSync(path.join(this.opts.templatesDir, 'package-json'), 'utf8'),
84-
componentName: this.opts.componentName
85-
})
82+
upgradePackageJson(
83+
Object.assign(
84+
this.opts,
85+
{
86+
source: fs.readFileSync(path.join(this.opts.templatesDir, 'package-json'), 'utf8'),
87+
}
88+
)
89+
)
8690
}
8791
}
8892

89-
function upgradePackageJson({ pkg, source, componentName }) {
93+
function upgradePackageJson({ pkg, source, componentName, ownerName }) {
9094
const properties = ['scripts', 'devDependencies']
9195
const cliVersion = require('../package.json').version
9296

93-
const templatePkg = JSON.parse(parseContent(source, componentName))
97+
const templatePkg = JSON.parse(parseContent(source, { componentName, ownerName }))
9498
const currentPkg = pkg
9599

96100
properties.forEach(key => {

templates/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) {{licenseYear}} FEMessage
3+
Copyright (c) {{licenseYear}} {{ownerName}}
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

templates/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# {{componentName}}
22

3-
[![Build Status](https://travis-ci.com/FEMessage/{{componentName}}.svg?branch=master)](https://travis-ci.com/FEMessage/{{componentName}})
4-
[![NPM Download](https://img.shields.io/npm/dm/@femessage/{{componentName}}.svg)](https://www.npmjs.com/package/@femessage/{{componentName}})
5-
[![NPM Version](https://img.shields.io/npm/v/@femessage/{{componentName}}.svg)](https://www.npmjs.com/package/@femessage/{{componentName}})
6-
[![NPM License](https://img.shields.io/npm/l/@femessage/{{componentName}}.svg)](https://github.com/FEMessage/{{componentName}}/blob/master/LICENSE)
7-
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/FEMessage/{{componentName}}/pulls)
3+
[![Build Status](https://travis-ci.com/{{ownerName}}/{{componentName}}.svg?branch=master)](https://travis-ci.com/{{ownerName}}/{{componentName}})
4+
[![NPM Download](https://img.shields.io/npm/dm/@{{ownerName}}/{{componentName}}.svg)](https://www.npmjs.com/package/@{{ownerName}}/{{componentName}})
5+
[![NPM Version](https://img.shields.io/npm/v/@{{ownerName}}/{{componentName}}.svg)](https://www.npmjs.com/package/@{{ownerName}}/{{componentName}})
6+
[![NPM License](https://img.shields.io/npm/l/@{{ownerName}}/{{componentName}}.svg)](https://github.com/{{ownerName}}/{{componentName}}/blob/master/LICENSE)
7+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/{{ownerName}}/{{componentName}}/pulls)
88
[![Automated Release Notes by gren](https://img.shields.io/badge/%F0%9F%A4%96-release%20notes-00B2EE.svg)](https://github-tools.github.io/github-release-notes/)
99

1010
项目简要说明 + 示例图
@@ -29,7 +29,7 @@
2929

3030
## Demo
3131

32-
* [doc and online demo](https://femessage.github.io/{{componentName}}/)
32+
* [doc and online demo](https://{{ownerName}}.github.io/{{componentName}}/)
3333

3434
[⬆ Back to Top](#table-of-contents)
3535

templates/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ yarn stdver
33

44
yarn build
55

6-
git remote add github https://$GITHUB_TOKEN@github.com/FEMessage/{{componentName}}.git > /dev/null 2>&1
6+
git remote add github https://$GITHUB_TOKEN@github.com/{{ownerName}}/{{componentName}}.git > /dev/null 2>&1
77
git push github HEAD:master --follow-tags
88

templates/notify.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
url=https://api.github.com/repos/FEMessage/{{componentName}}/releases/latest
2+
url=https://api.github.com/repos/{{ownerName}}/{{componentName}}/releases/latest
33
html_url=`curl $url | sed -n 5p | sed 's/\"html_url\"://g' | awk -F '"' '{print $2}'`
44
body=`curl $url | grep body | sed 's/\"body\"://g;s/\"//g'`
55

templates/package-json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "@femessage/{{componentName}}",
2+
"name": "@{{ownerName}}/{{componentName}}",
33
"version": "1.0.0",
44
"description": "",
5-
"author": "https://github.com/FEMessage",
5+
"author": "https://github.com/{{ownerName}}",
66
"license": "MIT",
77
"repository": {
88
"type": "git",
9-
"url": "https://github.com/FEMessage/{{componentName}}.git"
9+
"url": "https://github.com/{{ownerName}}/{{componentName}}.git"
1010
},
1111
"keywords": ["vue", "sfc", "component"],
1212
"files": ["src", "dist", "yarn.lock"],

templates/styleguide.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
styleguideDir: 'docs',
2020
pagePerSection: true,
2121
ribbon: {
22-
url: 'https://github.com/FEMessage/{{componentName}}'
22+
url: 'https://github.com/{{ownerName}}/{{componentName}}'
2323
},
2424
sections: [
2525
{

utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ exports.pascalify = pascalify
66

77
exports.kebabcasify = kebabcasify
88

9-
exports.parseContent = (content, componentName) => {
9+
exports.parseContent = (content, { componentName, ownerName }) => {
1010
return content
1111
.replace(createRegExp('componentNamePascal'), pascalify(componentName))
1212
.replace(createRegExp('componentName'), kebabcasify(componentName))
13+
.replace(createRegExp('ownerName'), ownerName)
1314
.replace(createRegExp('cliVersion'), require('../package.json').version)
1415
.replace(createRegExp('licenseYear'), new Date().getFullYear())
1516
}

0 commit comments

Comments
 (0)