-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
2 changed files
with
29 additions
and
19 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
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.` | ||
) | ||
}, | ||
} |
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