Skip to content

Commit

Permalink
Merge pull request #42 from FEMessage/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
levy9527 authored May 17, 2019
2 parents 65732dd + bc88af5 commit 8937fa3
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 63 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ yarn build
# Ready to publish!
```

## Cli options

### `--test`

用于生成一个测试的组件模板

### `-u, --upgrade`

更新当前组件配置

## Notice

不建议在Windows下生成组件,因为`.sh`可能没有执行权限

## docs

You can write *.md files host in `docs/` as code example.
Expand Down
59 changes: 43 additions & 16 deletions init.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,69 @@

const { logger, kebabcasify } = require('./utils')
const FileActions = require('./lib/fileActions')
const parseArgs = require('./lib/parseArgs')
const kleur = require('kleur')
const path = require('path')
const readline = require('readline-sync')
const fs = require('fs')
const fs = require('fs-extra')

const argv = process.argv.slice(2)
const argv = parseArgs(process.argv.slice(2))

let pkg = {}
/**
* Prompt user for input to populate template files
*/
const npmName = argv.includes('--test')
? 'v-test'
: readline.question(
let npmName

function isUpgrade() {
return argv.has('u') || argv.has('upgrade')
}

if (isUpgrade()) {
try {
pkg = require(path.join(process.cwd(), 'package.json'))
npmName = pkg.name.replace(/^@[\w]*\//, '')
} catch {}
}

if (argv.has('test')) {
npmName = 'v-test'
}

if (!npmName) {
npmName = readline.question(
'✍️ What is the npm name of your component? '
)
}

const componentName = kebabcasify(npmName)
const outDir = path.join(process.cwd(), componentName)

const fileActions = new FileActions({
argv,
pkg,
componentName,
outDir,
templatesDir: path.join(__dirname, 'templates')
})

fileActions.create()
if (!isUpgrade()) {
fileActions.create()

fileActions.move({
patterns: {
gitignore: '.gitignore',
'package-json': 'package.json',
'src/component.vue': `src/${componentName}.vue`
}
})
fileActions.move({
patterns: {
gitignore: '.gitignore',
'package-json': 'package.json',
'src/component.vue': `src/${componentName}.vue`
}
})

fs.chmodSync(path.join(outDir, 'build.sh'), '755')
fs.chmodSync(path.join(outDir, 'notify.sh'), '755')

fs.chmodSync(path.join(outDir, 'build.sh'), '755')
fs.chmodSync(path.join(outDir, 'notify.sh'), '755')
logger.success(`Generated into ${kleur.underline(outDir)}`)
}

logger.success(`Generated into ${kleur.underline(outDir)}`)
if (isUpgrade()) {
fileActions.upgrade()
}
58 changes: 58 additions & 0 deletions lib/fileActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,62 @@ module.exports = class FileActions {
logger.fileMoveAction(from, to)
})
}

upgrade(extraFiles = []) {
const filesFromCli = this.opts.argv.get('files') || ''

const files = [
'.grenrc.js',
'.prettierrc',
'.travis.yml',
'build.sh',
'notify.sh'
].concat(extraFiles, filesFromCli.split(','))

const upgradeFiles = glob.sync(
`*(${files.join('|')})`,
{
cwd: this.opts.templatesDir,
nodir: true,
dot: true,
absolute: true
}
)

upgradeFiles.forEach(filepath => {
const fileName = path.relative(this.opts.templatesDir, filepath)
const target = path.join(process.cwd(), fileName)
const content = parseContent(fs.readFileSync(filepath, 'utf8'), this.opts.componentName)

fs.outputFileSync(target, content)

logger.fileAction('yellow', 'Upgraded', path.relative(process.cwd(), target))
})

fs.chmodSync(path.join(process.cwd(), 'build.sh'), '755')
fs.chmodSync(path.join(process.cwd(), 'notify.sh'), '755')

upgradePackageJson({
pkg: this.opts.pkg,
source: fs.readFileSync(path.join(this.opts.templatesDir, 'package-json'), 'utf8'),
componentName: this.opts.componentName
})
}
}

function upgradePackageJson({ pkg, source, componentName }) {
const properties = ['scripts', 'devDependencies']

const templatePkg = JSON.parse(parseContent(source, componentName))
const currentPkg = pkg

properties.forEach(key => {
currentPkg[key] = Object.assign(pkg[key], templatePkg[key])
})

fs.outputJSONSync(path.join(process.cwd(), 'package.json'), currentPkg, {
spaces: 2
})

logger.fileAction('yellow', 'Upgraded', 'package.json')
}
24 changes: 24 additions & 0 deletions lib/parseArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mri = require('mri')

module.exports = (_args = process.argv) => {
const parsed = mri(_args, {
alias: {
u: 'upgrade'
},
string: ['files']
})

const args = parsed._
delete parsed._

return {
has(name) {
return parsed[name] !== undefined
},
get(name) {
return parsed[name]
},
options: parsed,
args
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"kleur": "^3.0.3",
"mri": "^1.1.4",
"readline-sync": "^1.4.9"
},
"engines": {
Expand Down
32 changes: 19 additions & 13 deletions templates/.grenrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
module.exports = {
"dataSource": "prs",
"prefix": "",
"ignoreLabels": ["duplicate", "help wanted", "invalid", "question", "wontfix"],
"ignoreIssuesWith": ["duplicate", "help wanted", "invalid", "question", "wontfix"],
"onlyMilestones": false,
"changelogFilename": "CHANGELOG.md",
"template": {
"issue": "- {{name}} [{{text}}]({{url}})"
},
"groupBy": {
"✨ New Features:": ["enhancement"],
"🐛 Bug Fixes:": ["bug"]
}
dataSource: 'prs',
prefix: '',
ignoreLabels: ['duplicate', 'help wanted', 'invalid', 'question', 'wontfix'],
ignoreIssuesWith: [
'duplicate',
'help wanted',
'invalid',
'question',
'wontfix'
],
onlyMilestones: false,
changelogFilename: 'CHANGELOG.md',
template: {
issue: '- {{name}} [{{text}}]({{url}})'
},
groupBy: {
'✨ New Features:': ['enhancement'],
'🐛 Bug Fixes:': ['bug']
}
}
47 changes: 13 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ debug@^3.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.npm.taobao.org/debuglog/download/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"

Expand Down Expand Up @@ -1298,7 +1298,7 @@ import-lazy@^2.1.0:
version "2.1.0"
resolved "https://registry.npm.taobao.org/import-lazy/download/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.npm.taobao.org/imurmurhash/download/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"

Expand Down Expand Up @@ -1619,7 +1619,7 @@ libnpm@^2.0.1:
read-package-json "^2.0.13"
stringify-package "^1.0.0"

libnpmaccess@*, libnpmaccess@^3.0.1:
libnpmaccess@^3.0.1:
version "3.0.1"
resolved "https://registry.npm.taobao.org/libnpmaccess/download/libnpmaccess-3.0.1.tgz#5b3a9de621f293d425191aa2e779102f84167fa8"
dependencies:
Expand All @@ -1645,7 +1645,7 @@ libnpmhook@^5.0.2:
get-stream "^4.0.0"
npm-registry-fetch "^3.8.0"

libnpmorg@*, libnpmorg@^1.0.0:
libnpmorg@^1.0.0:
version "1.0.0"
resolved "https://registry.npm.taobao.org/libnpmorg/download/libnpmorg-1.0.0.tgz#979b868c48ba28c5820e3bb9d9e73c883c16a232"
dependencies:
Expand All @@ -1668,15 +1668,15 @@ libnpmpublish@^1.1.0:
semver "^5.5.1"
ssri "^6.0.1"

libnpmsearch@*, libnpmsearch@^2.0.0:
libnpmsearch@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/libnpmsearch/download/libnpmsearch-2.0.0.tgz#de05af47ada81554a5f64276a69599070d4a5685"
dependencies:
figgy-pudding "^3.5.1"
get-stream "^4.0.0"
npm-registry-fetch "^3.8.0"

libnpmteam@*, libnpmteam@^1.0.1:
libnpmteam@^1.0.1:
version "1.0.1"
resolved "https://registry.npm.taobao.org/libnpmteam/download/libnpmteam-1.0.1.tgz#ff704b1b6c06ea674b3b1101ac3e305f5114f213"
dependencies:
Expand Down Expand Up @@ -1744,39 +1744,17 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.npm.taobao.org/lodash._baseindexof/download/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.npm.taobao.org/lodash._baseuniq/download/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
dependencies:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.npm.taobao.org/lodash._bindcallback/download/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.npm.taobao.org/lodash._cacheindexof/download/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.npm.taobao.org/lodash._createcache/download/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.npm.taobao.org/lodash._createset/download/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.npm.taobao.org/lodash._getnative/download/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"

lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/lodash._reinterpolate/download/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand All @@ -1793,10 +1771,6 @@ lodash.ismatch@^4.4.0:
version "4.4.0"
resolved "https://registry.npm.taobao.org/lodash.ismatch/download/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.npm.taobao.org/lodash.restparam/download/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"

lodash.template@^4.0.2:
version "4.4.0"
resolved "https://registry.npm.taobao.org/lodash.template/download/lodash.template-4.4.0.tgz#e73a0385c8355591746e020b99679c690e68fba0"
Expand Down Expand Up @@ -2031,6 +2005,11 @@ move-concurrently@^1.0.1:
rimraf "^2.5.4"
run-queue "^1.0.3"

mri@^1.1.4:
version "1.1.4"
resolved "https://registry.npmjs.org/mri/-/mri-1.1.4.tgz#7cb1dd1b9b40905f1fac053abe25b6720f44744a"
integrity sha512-6y7IjGPm8AzlvoUrwAaw1tLnUBudaS3752vcd8JtrpGGQn+rXIe63LFVHm/YMwtqAuh+LJPCFdlLYPWM1nYn6w==

[email protected]:
version "2.0.0"
resolved "https://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down Expand Up @@ -2187,7 +2166,7 @@ npm-pick-manifest@^2.2.3:
npm-package-arg "^6.0.0"
semver "^5.4.1"

npm-profile@*, npm-profile@^4.0.1:
npm-profile@^4.0.1:
version "4.0.1"
resolved "https://registry.npm.taobao.org/npm-profile/download/npm-profile-4.0.1.tgz#d350f7a5e6b60691c7168fbb8392c3603583f5aa"
dependencies:
Expand Down Expand Up @@ -2812,7 +2791,7 @@ readable-stream@~1.1.10:
isarray "0.0.1"
string_decoder "~0.10.x"

readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
readdir-scoped-modules@^1.0.0:
version "1.0.2"
resolved "https://registry.npm.taobao.org/readdir-scoped-modules/download/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
dependencies:
Expand Down

0 comments on commit 8937fa3

Please sign in to comment.