UNPKG

2.03 kBJavaScriptView Raw
1'use strict';
2
3// Node.js built-ins
4
5const path = require('path');
6
7// foreign modules
8
9const findUp = require('find-up');
10const projectConfig = require('@blinkmobile/blinkmrc').projectConfig;
11
12// local modules
13
14const CONFIG_FILE = require('./values').CONFIG_FILE;
15const isURL = require('./utils/url').isURL;
16const pkg = require('../package.json');
17
18// this module
19
20function findConfig () {
21 const cwd = process.env.BMP_WORKING_DIR || process.cwd();
22 return findUp(CONFIG_FILE, { cwd })
23 .then((filePath) => {
24 if (!filePath) {
25 return Promise.reject(new Error(`${CONFIG_FILE} not found`));
26 }
27 return filePath;
28 });
29}
30
31module.exports = {
32
33 CONFIG_FILE,
34
35 read () {
36 if (process.env.BMP_SCOPE) {
37 if (!isURL(process.env.BMP_SCOPE)) {
38 return Promise.reject(new TypeError(`"${process.env.BMP_SCOPE}" is not a valid URL`));
39 }
40 return Promise.resolve(process.env.BMP_SCOPE);
41 }
42
43 return findConfig()
44 .then((filePath) => {
45 const config = projectConfig({
46 cwd: path.dirname(filePath),
47 name: pkg.name
48 });
49 return config.load();
50 })
51 .then((cfg) => {
52 if (!cfg.bmp || !cfg.bmp.scope) {
53 return Promise.reject(new Error(`bmp.scope not found in ${CONFIG_FILE}`));
54 }
55 return cfg.bmp.scope;
56 });
57 },
58
59 write (options) {
60 if (!isURL(options.scope)) {
61 return Promise.reject(new TypeError(`"${options.scope}" is not a valid URL`));
62 }
63
64 return findConfig()
65 .catch(() => {
66 const cwd = process.env.BMP_WORKING_DIR || process.cwd();
67 // not found, so assume we want to make one in cwd
68 return path.join(cwd, CONFIG_FILE);
69 })
70 .then((filePath) => {
71 const config = projectConfig({
72 cwd: path.dirname(filePath),
73 name: pkg.name
74 });
75 return config.update((cfg) => {
76 cfg.bmp = cfg.bmp || {};
77 cfg.bmp.scope = options.scope;
78 return cfg;
79 });
80 });
81 }
82
83};