1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const fs = require('fs')
|
9 | const path = require('path')
|
10 | const { promisify } = require('util')
|
11 |
|
12 | const rc = require('rc')
|
13 | const fullname = require('fullname')
|
14 | const username = require('username')
|
15 |
|
16 | const { util } = require('../common')
|
17 |
|
18 | const readFile = promisify(fs.readFile)
|
19 | const writeFile = promisify(fs.writeFile)
|
20 |
|
21 | const npmrc = rc('npm')
|
22 | const yarnrc = rc('yarn')
|
23 |
|
24 | const getLatestAnswers = async () => {
|
25 | try {
|
26 | const filename = util.getDataPath('generator', 'answers.json')
|
27 | const json = await readFile(filename, 'utf8')
|
28 | return JSON.parse(json)
|
29 | } catch (e) {
|
30 |
|
31 | return {}
|
32 | }
|
33 | }
|
34 |
|
35 | class Defaults {
|
36 | constructor (dest, defaults) {
|
37 | this.dest = dest
|
38 |
|
39 | Object.assign(this, defaults)
|
40 | }
|
41 |
|
42 | name (answers) {
|
43 | return path.basename(this.dest)
|
44 | }
|
45 |
|
46 | async username (answers) {
|
47 |
|
48 |
|
49 | const name = npmrc['init-author-name'] || yarnrc['init-author-name'] || await util.execute('git config --get user.name') || await username()
|
50 | return name
|
51 | }
|
52 |
|
53 | async fullname (answers) {
|
54 | const name = await fullname()
|
55 | return name
|
56 | }
|
57 |
|
58 | async email (answers) {
|
59 |
|
60 |
|
61 | const email = npmrc['init-author-email'] || yarnrc['init-author-email'] || await util.execute('git config --get user.email')
|
62 | return email
|
63 | }
|
64 |
|
65 | async url (answers) {
|
66 |
|
67 |
|
68 | const url = npmrc['init-author-url'] || yarnrc['init-author-url'] || await util.execute('git config --get user.url')
|
69 | return url
|
70 | }
|
71 |
|
72 | async author (answers) {
|
73 |
|
74 | const name = await this.username() || await this.fullname()
|
75 | const email = await this.email()
|
76 | const url = await this.url()
|
77 |
|
78 |
|
79 | return `${name}${email ? ` <${email}>` : ''}${url ? ` (${url})` : ''}`
|
80 | }
|
81 |
|
82 | version (answers) {
|
83 |
|
84 | return npmrc['init-version'] || yarnrc['init-version'] || '0.1.0'
|
85 | }
|
86 |
|
87 | license (answers) {
|
88 |
|
89 | return npmrc['init-license'] || yarnrc['init-license'] || 'MIT'
|
90 | }
|
91 |
|
92 | repository (answers) {
|
93 | return util.execute('git config --local --get remote.origin.url', this.dest)
|
94 | }
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 | static async init (dest) {
|
101 | if (typeof dest !== 'string') {
|
102 | throw new TypeError(`Expected a string, got ${typeof dest}`)
|
103 | }
|
104 |
|
105 | const defaults = await getLatestAnswers()
|
106 |
|
107 | return new Defaults(dest, defaults)
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 | static async save (answers) {
|
115 |
|
116 | const defaults = await getLatestAnswers()
|
117 | Object.assign(defaults, answers)
|
118 |
|
119 |
|
120 | delete defaults.name
|
121 | delete defaults.description
|
122 | delete defaults.repo
|
123 | delete defaults.repository
|
124 |
|
125 |
|
126 | const json = JSON.stringify(defaults, null, 2)
|
127 | await util.mkdirp(util.getDataPath('generator'))
|
128 | await writeFile(util.getDataPath('generator', 'answers.json'), json)
|
129 | }
|
130 | }
|
131 |
|
132 |
|
133 | Defaults.prototype.repo = Defaults.prototype.repository
|
134 |
|
135 |
|
136 | module.exports = Defaults
|