UNPKG

2.11 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 pkg = require('../package.json');
16const urlUtils = require('./utils/url');
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
31function read () {
32 if (process.env.BMP_SCOPE) {
33 if (!urlUtils.isURL(process.env.BMP_SCOPE)) {
34 return Promise.reject(new TypeError(`"${process.env.BMP_SCOPE}" is not a valid URL`));
35 }
36 return Promise.resolve(process.env.BMP_SCOPE);
37 }
38
39 return findConfig()
40 .then((filePath) => {
41 const config = projectConfig({
42 cwd: path.dirname(filePath),
43 name: pkg.name
44 });
45 return config.load();
46 })
47 .then((cfg) => {
48 if (!cfg.bmp || !cfg.bmp.scope) {
49 return Promise.reject(new Error(`bmp.scope not found in ${CONFIG_FILE}`));
50 }
51 return cfg.bmp.scope;
52 });
53}
54
55function getUID () {
56 return read()
57 .then((s) => urlUtils.getPathSegment(s, 1));
58}
59
60module.exports = {
61
62 CONFIG_FILE,
63 getUID,
64 read,
65
66 write (options) {
67 if (!urlUtils.isURL(options.scope)) {
68 return Promise.reject(new TypeError(`"${options.scope}" is not a valid URL`));
69 }
70
71 return findConfig()
72 .catch(() => {
73 const cwd = process.env.BMP_WORKING_DIR || process.cwd();
74 // not found, so assume we want to make one in cwd
75 return path.join(cwd, CONFIG_FILE);
76 })
77 .then((filePath) => {
78 const config = projectConfig({
79 cwd: path.dirname(filePath),
80 name: pkg.name
81 });
82 return config.update((cfg) => {
83 cfg.bmp = cfg.bmp || {};
84 cfg.bmp.scope = options.scope;
85 return cfg;
86 });
87 });
88 }
89
90};