This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
config.js
63 lines (56 loc) · 1.61 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict'
const path = require('path')
const fs = require('fs-extra')
const co = require('co')
const cac = require('cac')
const home = require('user-home')
const configDir = path.join(home, '.sao')
const packagesDir = path.join(configDir, 'packages')
const reposDir = path.join(configDir, 'repos')
const getConfigOptions = options => {
const { flags } = cac.parse(process.argv.slice(2))
return Object.assign(
{
flags
},
options
)
}
const ensurePackages = co.wrap(function*() {
const packagesDirExists = yield fs.pathExists(packagesDir)
if (!packagesDirExists) {
yield fs.ensureDir(packagesDir)
yield fs.writeFile(
path.join(packagesDir, 'package.json'),
JSON.stringify({
name: 'sao-package-templates',
version: '0.0.0',
private: true,
license: 'MIT',
dependencies: {}
}),
'utf8'
)
}
})
const ensureRepos = () => fs.ensureDir(reposDir)
function getConfig(templateDir, configFileName, context) {
const configPath = path.join(templateDir, configFileName)
return fs.pathExists(configPath).then(exists => {
if (!exists) return null
const config = require(configPath)
return typeof config === 'function'
? config(getConfigOptions(context))
: config
})
}
function getConfigFileName(configFileName, pkg) {
return configFileName || (pkg && pkg.sao) || 'sao.js'
}
exports.configDir = configDir
exports.packagesDir = packagesDir
exports.reposDir = reposDir
exports.getConfig = getConfig
exports.getConfigFileName = getConfigFileName
exports.ensurePackages = ensurePackages
exports.ensureRepos = ensureRepos