1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const path = require('path');
|
6 |
|
7 |
|
8 |
|
9 | const findUp = require('find-up');
|
10 | const projectConfig = require('@blinkmobile/blinkmrc').projectConfig;
|
11 |
|
12 |
|
13 |
|
14 | const CONFIG_FILE = require('./values').CONFIG_FILE;
|
15 | const isURL = require('./utils/url').isURL;
|
16 | const pkg = require('../package.json');
|
17 |
|
18 |
|
19 |
|
20 | function 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 |
|
31 | module.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 |
|
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 | };
|