1 | 'use strict'
|
2 |
|
3 | path = require 'path'
|
4 | async = require 'async'
|
5 | fs = require 'fs-extra'
|
6 | existsFile = require 'exists-file'
|
7 | jsonFuture = require 'json-future'
|
8 | util = require './Bumped.util'
|
9 | DEFAULT = require './Bumped.default'
|
10 | MSG = require './Bumped.messages'
|
11 |
|
12 | module.exports = class Config
|
13 |
|
14 | constructor: (bumped) ->
|
15 | @bumped = bumped
|
16 | @init()
|
17 |
|
18 | init: (files) =>
|
19 | @rc = util.initConfig
|
20 | appname: @bumped.pkg.name
|
21 | default: DEFAULT.scaffold()
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | autodetect: ->
|
29 | [opts, cb] = DEFAULT.args arguments
|
30 |
|
31 | tasks = [
|
32 | removePreviousConfigFile = (next) =>
|
33 | return next() unless @rc.config
|
34 |
|
35 | fs.remove @rc.config, (err) =>
|
36 | return next err if err
|
37 | @init()
|
38 | next()
|
39 |
|
40 | detectCommonFiles = (next) =>
|
41 | async.each DEFAULT.detectFileNames, (file, done) =>
|
42 | @add file:file, output:false, (err) -> done()
|
43 | , next
|
44 | fallbackUnderNotDetect = (next) =>
|
45 | return next() if @rc.files.length isnt 0
|
46 | @addFallback next
|
47 | generateDefaultPlugins = (next) =>
|
48 | @rc.plugins = DEFAULT.plugins @rc.files
|
49 | next()
|
50 | ]
|
51 |
|
52 | async.waterfall tasks, cb
|
53 |
|
54 | |
55 |
|
56 |
|
57 | addFallback: (cb) ->
|
58 | opts =
|
59 | file: DEFAULT.fallbackFileName
|
60 | data: version:'0.0.0'
|
61 |
|
62 | tasks = [
|
63 | createFallbackFile = (next) -> util.createJSON opts, next
|
64 | addFallbackFile = (next) => @addFile opts, next
|
65 | ]
|
66 |
|
67 | async.waterfall tasks, cb
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | add: =>
|
76 | [opts, cb] = DEFAULT.args arguments
|
77 |
|
78 | loggerOptions =
|
79 | lineBreak: false
|
80 | output: opts.output
|
81 |
|
82 | if @hasFile opts.file
|
83 | message = MSG.NOT_ALREADY_ADD_FILE opts.file
|
84 | return @bumped.logger.errorHandler message, loggerOptions, cb
|
85 |
|
86 | tasks = [
|
87 | (next) =>
|
88 | @detect opts, next,
|
89 | (exists, next) =>
|
90 | return @addFile opts, next if exists
|
91 | message = MSG.NOT_ADD_FILE opts.file
|
92 | return @bumped.logger.errorHandler message, loggerOptions, cb
|
93 | (next) =>
|
94 | unless opts.save then next() else @save opts, next
|
95 | ]
|
96 |
|
97 | async.waterfall tasks, (err, result) =>
|
98 | cb err, @rc.files
|
99 |
|
100 | |
101 |
|
102 |
|
103 | detect: ->
|
104 | [opts, cb] = DEFAULT.args arguments
|
105 |
|
106 | filePath = path.join(process.cwd(), opts.file)
|
107 | existsFile filePath, cb
|
108 |
|
109 | remove: =>
|
110 | [opts, cb] = DEFAULT.args arguments
|
111 |
|
112 | unless @hasFile opts.file
|
113 | message = MSG.NOT_REMOVE_FILE opts.file
|
114 | return @bumped.logger.errorHandler message, lineBreak:false, cb
|
115 |
|
116 | tasks = [
|
117 | (next) => @removeFile opts, next
|
118 | (next) => unless opts.save then next() else @save opts, next
|
119 | ]
|
120 |
|
121 | async.waterfall tasks, (err, result) =>
|
122 | cb(err, @rc.files)
|
123 |
|
124 | |
125 |
|
126 |
|
127 | save: =>
|
128 | [opts, cb] = DEFAULT.args arguments
|
129 |
|
130 | util.saveConfig
|
131 | path : ".#{@bumped.pkg.name}rc"
|
132 | data :
|
133 | files: @rc.files
|
134 | plugins: @rc.plugins
|
135 | , cb
|
136 |
|
137 | load: =>
|
138 | [opts, cb] = DEFAULT.args arguments
|
139 |
|
140 | util.loadConfig
|
141 | path: @bumped.config.rc.config
|
142 | , (err, filedata) =>
|
143 | throw err if err
|
144 | @loadScaffold filedata
|
145 | cb()
|
146 |
|
147 | addFile: ->
|
148 | [opts, cb] = DEFAULT.args arguments
|
149 |
|
150 | @rc.files.push opts.file
|
151 | @bumped.logger.success MSG.ADD_FILE opts.file
|
152 | cb()
|
153 |
|
154 | removeFile: ->
|
155 | [opts, cb] = DEFAULT.args arguments
|
156 |
|
157 | index = @rc.files.indexOf opts.file
|
158 | @rc.files.splice index, 1
|
159 | @bumped.logger.success MSG.REMOVE_FILE opts.file
|
160 | cb()
|
161 |
|
162 | set: =>
|
163 | [opts, cb] = DEFAULT.args arguments
|
164 |
|
165 | setProperty = (file, done) ->
|
166 | util.updateJSON
|
167 | filename : file
|
168 | property : opts.property
|
169 | value : opts.value
|
170 | force : true
|
171 | , done
|
172 |
|
173 | message = null
|
174 | message = MSG.NOT_SET_PROPERTY() if util.size(opts.value) is 0
|
175 | message = MSG.NOT_SET_PROPERTY() if util.size(opts.property) is 0
|
176 | message = MSG.NOT_SET_VERSION() if opts.property is 'version'
|
177 | return @bumped.logger.errorHandler message, lineBreak:false, cb if message
|
178 |
|
179 | async.each @bumped.config.rc.files, setProperty, (err) =>
|
180 | return @bumped.logger.errorHandler err, cb if err
|
181 | @bumped.logger.success MSG.SET_PROPERTY opts.property, opts.value
|
182 | cb null, opts
|
183 |
|
184 | hasFile: (file) ->
|
185 | @rc.files.indexOf(file) isnt -1
|
186 |
|
187 | loadScaffold: (filedata) ->
|
188 | @bumped.config.rc.files = filedata.files
|
189 | @bumped.config.rc.plugins = filedata.plugins
|