Skip to content

Commit

Permalink
update sub generator
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jul 19, 2020
1 parent 6b47059 commit 6c3f3b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
43 changes: 26 additions & 17 deletions generators/donation/saofile.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
// @ts-check

const fs = require('fs')
const path = require('path')

/** @type {import('sao').GeneratorConfig} */
module.exports = {
description: 'Add a "postinstall" script to show donation URL',
prepare() {
if (!fs.existsSync(path.join(this.outPath, 'package.json'))) {
throw this.createError(`Cannot find package.json in ${this.outPath}`)
async prepare() {
if (!(await this.hasOutputFile('package.json'))) {
throw this.createError(`Cannot find package.json in ${this.outFolder}`)
}
},
prompts() {
return [
{
type: 'input',
name: 'name',
default: this.pkg.name || this.outFolder,
message: 'Your project name',
required: true
required: true,
},
{
type: 'input',
name: 'url',
message: 'The URL where users can donate to your project',
store: true,
required: true,
validate: v => /^https?:\/\//.test(v)
}
validate: (v) =>
/^https?:\/\//.test(v)
? false
: `Invalid URL, must start with http(s)://`,
},
]
},
actions() {
return [
this.answers.url !== 'none' && {
type: 'modify',
files: 'package.json',
handler: data => {
handler: (data) => {
data.scripts = Object.assign({}, data.scripts, {
postinstall: `node -e \"console.log('\\u001b[35m\\u001b[1mLove ${this.answers.name}? You can now donate to support the author:\\u001b[22m\\u001b[39m\\n> \\u001b[36m${this.answers.url}\\u001b[39m')\"`
postinstall: `node -e \"console.log('\\u001b[35m\\u001b[1mLove ${this.answers.name}? You can now donate to support the author:\\u001b[22m\\u001b[39m\\n> \\u001b[36m${this.answers.url}\\u001b[39m')\"`,
})
return data
}
}
},
},
].filter(Boolean)
},
async completed() {
this.logger.success('Added "postinstall" script in package.json!')
const commands = [process.cwd() !== this.outPath &&`cd ${this.outPath}`, `${this.npmClient} run postinstall`].filter(Boolean)
this.logger.tip(`Run ${this.chalk.cyan(commands.join(' && '))} to see the effect.`)
}
}
const commands = [
process.cwd() !== this.outDir && `cd ${this.outDir}`,
`${this.npmClient} run postinstall`,
].filter(Boolean)
this.logger.tip(
`Run ${this.colors.cyan(commands.join(' && '))} to see the effect.`
)
},
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"type": "git"
},
"scripts": {
"test": "jest"
"test": "jest",
"postinstall": "node -e \"console.log('\\u001b[35m\\u001b[1mLove undefined? You can now donate to support the author:\\u001b[22m\\u001b[39m\\n> \\u001b[36mundefined\\u001b[39m')\""
},
"jest": {
"modulePathIgnorePatterns": [
Expand Down Expand Up @@ -41,4 +42,4 @@
"prettier": "^2.0.5",
"sao": "1.7.2-canary.145.30b2bdb.0"
}
}
}

0 comments on commit 6c3f3b8

Please sign in to comment.