UNPKG

2.42 kBJavaScriptView Raw
1'use strict'
2
3const debug = require('debug')('gen')
4const Generator = require('yeoman-generator')
5const fs = require('fs')
6const exists = fs.existsSync
7const join = require('path').join
8const la = require('lazy-ass')
9const is = require('check-more-types')
10
11// generator that sets up semantic release
12const g = class extends Generator {
13 constructor (args, opts) {
14 super(args, opts)
15
16 this.pkgFilename = join(process.cwd(), 'package.json')
17 if (!exists(this.pkgFilename)) {
18 const msg = `Cannot find ${this.pkgFilename}
19 Run "yo node-bahmutov" first!`
20 console.error(msg)
21 throw new Error(msg)
22 }
23 }
24
25 initializing () {
26 console.log('📝 Setting up semantic release')
27 console.log('using these semantic-release plugins:')
28 console.log('* https://github.com/bahmutov/simple-commit-message')
29 console.log('* https://github.com/bahmutov/github-post-release')
30 console.log('* https://github.com/bahmutov/dont-crack')
31 }
32
33 setupSemanticRelease () {
34 debug('setting up semantic-release')
35 const done = this.async()
36 const child = this.spawnCommand('semantic-release-cli', ['setup'])
37 child.on('close', exitCode => {
38 if (exitCode) {
39 const msg = 'Could not setup semantic-release'
40 console.error(msg)
41 console.error('exit code', exitCode)
42 return done(new Error(msg))
43 }
44 done()
45 })
46 }
47
48 configureRelease () {
49 console.log('Writing "release" object in package.json')
50 la(is.unemptyString(this.pkgFilename), 'missing pkgFilename')
51 const pkg = require(this.pkgFilename)
52 pkg.release = {
53 analyzeCommits: 'simple-commit-message',
54 generateNotes: 'github-post-release',
55 verifyRelease: {
56 path: 'dont-crack',
57 'test-against': []
58 }
59 }
60 const text = JSON.stringify(pkg, null, 2) + '\n'
61 fs.writeFileSync(this.pkgFilename, text, 'utf8')
62 console.log('saved package.json file')
63 }
64
65 install () {
66 const deps = [
67 'semantic-release',
68 'simple-commit-message',
69 'github-post-release',
70 'dont-crack'
71 ]
72 console.log('Installing useful plugin modules', deps)
73
74 this.npmInstall(deps, {
75 saveDev: true
76 })
77 }
78
79 end () {
80 console.log('✅ All done')
81 console.log(`You should add dependent projects to test
82 before each release to package.json
83 release.verifyRelease.test-against list`)
84 }
85}
86
87module.exports = g